| 1891 |
ankur.sing |
1 |
package in.shop2020.support.interceptors;
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
import javax.servlet.ServletContext;
|
|
|
5 |
import javax.servlet.http.HttpServletRequest;
|
|
|
6 |
import javax.servlet.http.HttpServletResponse;
|
|
|
7 |
import javax.servlet.http.HttpSession;
|
|
|
8 |
|
|
|
9 |
import org.apache.struts2.ServletActionContext;
|
|
|
10 |
|
|
|
11 |
import com.opensymphony.xwork2.ActionInvocation;
|
|
|
12 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
|
|
13 |
|
|
|
14 |
public class LoginInterceptor extends AbstractInterceptor {
|
|
|
15 |
|
|
|
16 |
private static final long serialVersionUID = 1L;
|
|
|
17 |
private static final String USER_NAME = "username";
|
|
|
18 |
|
|
|
19 |
@Override
|
|
|
20 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
21 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
22 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
23 |
HttpSession session = request.getSession(); // Get the existing session or create a new one
|
|
|
24 |
ServletContext context = ServletActionContext.getServletContext();
|
|
|
25 |
String username = (String) session.getAttribute(LoginInterceptor.USER_NAME);
|
|
|
26 |
if(username == null){
|
|
|
27 |
response.sendRedirect(context.getContextPath() + "/reports");
|
|
|
28 |
return null;
|
|
|
29 |
}
|
|
|
30 |
return invocation.invoke();
|
|
|
31 |
}
|
|
|
32 |
}
|