title: Struts2 login interceptor is not working
body: <p>I am not able to stop user accessing a page say welcome.jsp when he is not in session, please help me in implementing login interceptor. here is my code. All I want to do is when user logs in with his user id, check wether he is in session, if he is in session, let him access any resouce else, redirect user to "somePage". TIA</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  </code></pre>
  
  <p></p>
  
  <p>
    </p>
  
  <pre><code>&lt;constant name="struts.convention.default.parent.package"
      value="default" /&gt;
  &lt;constant name="struts.enable.DynamicMethodInvocation" value="false" /&gt;
  &lt;constant name="struts.custom.i18n.resources" value="global" /&gt;
  &lt;constant name="struts.objectFactory"
      value="org.apache.struts2.spring.StrutsSpringObjectFactory" /&gt;
  &lt;constant name="struts.i18n.reload" value="false" /&gt;
  &lt;constant name="struts.configuration.xml.reload" value="false" /&gt;
  
  &lt;package name="default" namespace="/default" extends="json-default,struts-default"&gt;
  
      &lt;interceptors&gt;
          &lt;interceptor name="authentication"
              class="com.mycompany.abc.webapp.action.AuthenticationInterceptor" /&gt;
          &lt;interceptor-stack name="authStack"&gt;
              &lt;interceptor-ref name="authentication"&gt;&lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="defaultStack"&gt;&lt;/interceptor-ref&gt;
          &lt;/interceptor-stack&gt;
  
              /&gt; --&gt;
          &lt;interceptor-stack name="acc-stack"&gt;
              &lt;!-- &lt;interceptor-ref name="sessionCheck" /&gt; --&gt;
  
              &lt;interceptor-ref name="json"&gt;
                  &lt;param name="enableSMD"&gt;true&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="exception" /&gt;
              &lt;interceptor-ref name="alias" /&gt;
              &lt;interceptor-ref name="servletConfig" /&gt;
              &lt;interceptor-ref name="i18n" /&gt;
              &lt;interceptor-ref name="prepare" /&gt;
              &lt;interceptor-ref name="chain" /&gt;
              &lt;interceptor-ref name="debugging" /&gt;
              &lt;interceptor-ref name="scopedModelDriven" /&gt;
              &lt;interceptor-ref name="modelDriven" /&gt;
              &lt;interceptor-ref name="fileUpload" /&gt;
              &lt;interceptor-ref name="checkbox" /&gt;
              &lt;interceptor-ref name="multiselect" /&gt;
              &lt;interceptor-ref name="staticParams" /&gt;
              &lt;interceptor-ref name="actionMappingParams" /&gt;
              &lt;interceptor-ref name="params"&gt;
                  &lt;param name="excludeParams"&gt;dojo\..*,^struts\..*&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="conversionError" /&gt;
              &lt;interceptor-ref name="workflow"&gt;
                  &lt;param name="excludeMethods"&gt;input,back,cancel,browse&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;interceptor-ref name="timer" /&gt;
          &lt;/interceptor-stack&gt;
      &lt;/interceptors&gt;
  
      &lt;default-interceptor-ref name="authStack"&gt;&lt;/default-interceptor-ref&gt;
  
          &lt;global-results&gt;
           &lt;result name="login" type="redirect"&gt;/home.action&lt;/result&gt;
          &lt;/global-results&gt;
      &lt;action name="home"&gt;
          &lt;interceptor-ref name="defaultStack"&gt;&lt;/interceptor-ref&gt;
          &lt;result name="somePage"&gt;/jsp/somePage.jsp&lt;/result&gt;
           &lt;result name="success"&gt;/jsp/xyz.jsp&lt;/result&gt;
            &lt;result name="homePage"&gt;/jsp/homePage.jsp&lt;/result&gt;
      &lt;/action&gt;
   &lt;!-- &lt;action class="com.mycompany.abc.webapp.action.LoginAction" name="login"&gt;
          &lt;interceptor-ref name="defaultStack"&gt;&lt;/interceptor-ref&gt;
          &lt;result name="success"&gt;/jsp/welcome.jsp&lt;/result&gt;
          &lt;result name="somePage"&gt;/jsp/somePage.jsp&lt;/result&gt;
      &lt;/action&gt;
  
      &lt;action name="welcome" class="com.mycompany.abc.webapp.action.WelcomeAction"&gt;
      &lt;interceptor-ref name="defaultStack"&gt;&lt;/interceptor-ref&gt;
          &lt;result name="success"&gt;/jsp/welcome.jsp&lt;/result&gt;
      &lt;/action&gt; --&gt;
  
  &lt;/package&gt;
  </code></pre>
  
  <p></p>
  
  <p>LoginAction:</p>
  
  <pre><code>@InterceptorRef(value = "defaultStack")
  @ParentPackage("struts-default")
  
  @Results({ @Result(name = "success", location = "/jsp/xyz.jsp"),
          @Result(name = "error", location = "/jsp/error.jsp"),
          @Result(name = "noAccess", location = "/jsp/abc.jsp"),
          @Result(name = "somePage", location = "/jsp/somePage.jsp"),
          @Result(name = "input", location = "/jsp/login.jsp"), })
  public class LoginAction extends ActionSupport implements SessionAware,
          ModelDriven&lt;MySession&gt; {
  private static final long serialVersionUID = -3369875299120377549L;
  private String userId;
  private String result = null;
  @Autowired
  CompService CompService;
  
  MySession MySession = new MySession();
  @Autowired
  MyServices MyServices;
  
  private Map&lt;String, Object&gt; sessionAttributes = null;
  /*private User user = new User();*/
  
  @Override
  public String execute() {
      System.out.println("inside execute");
      System.out.println("userid************" + this.userid);
      if (this.userid != null) {
  
          HttpSession session = ServletActionContext.getRequest()
          .getSession();
          useridProfile profile = MyServices.getuseridProfile(this.userid);
          if (profile != null) {
              //here i am getting  a collection say my Coll
              if (myColl.isEmpty()) {
                  result = "noAcess";
              }
              else{
                  sessionAttributes.put("userId", userId);
                  result = "success";
              }
  
          }
  
          return result;
      } else if (sessionAttributes.get("userid") == null) {
  
          System.out.println("Not logged in");
          System.out.println("userid************" + this.userid);
          result = "somePage";
      } 
      return result;
  }
  
  @Override
  public void setSession(Map&lt;String, Object&gt; sessionAttributes) {
      this.sessionAttributes = sessionAttributes;
  }
  
  public String getuserId() {
      return userid;
  }
  
  public void setuserId(String userid) {
      this.userid = userid;
  }
  
  @Override
  public MySession getModel() {
      // TODO Auto-generated method stub
      return MySession;
  }
  
  }
  </code></pre>
  
  <p>AuthenticationInterceptor</p>
  
  <pre><code>public class AuthenticationInterceptor implements Interceptor{
  
      private static final long serialVersionUID = -5011962009065225959L;
  
       String result=null;
  @Override
  public void destroy() {
      //release resources here
  }
  
  @Override
  public void init() {
      // create resources here
  }
  
  @Override
  public String intercept(ActionInvocation actionInvocation)
          throws Exception {
  
      ActionContext sessionAttributes = actionInvocation.getInvocationContext();
      System.out.println("inside auth interceptor");
      Object sess = sessionAttributes.get("userid");
  
      System.out.println("inside auth interceptor"+sess);
     // User user = (User) sessionAttributes.get("USER");
  
      if(sess == null){
  
          if(sessionAttributes.get("userId") != null){
               result = actionInvocation.invoke();
  
  
      }
          return result;
      }
          else{
  
          return actionInvocation.invoke();
  
      }
  
  
  }
  }
  </code></pre>
  
  <p>login jsp</p>
  
  <pre><code>    &lt;%@ page language="java" contentType="text/html; charset=US-ASCII"
      pageEncoding="US-ASCII"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%-- Using Struts2 Tags in JSP --%&gt;
  &lt;%@ taglib uri="/struts-tags" prefix="s"%&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"&gt;
  &lt;title&gt;Login Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h3&gt;Welcome User, please login below&lt;/h3&gt;
  &lt;s:form action="login"&gt;
      &lt;s:textfield name="userId" label="userId"&gt;&lt;/s:textfield&gt;
      &lt;s:submit value="Login"&gt;&lt;/s:submit&gt;
  &lt;/s:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>