title: How do I access the HttpSession inside a Struts 2 interceptor? Should I?
body: <p>I am seeing the concept of Interceptors in Struts2. I have seen the below example from a tutorial on Struts2.</p>
  
  <p>In this example the author uses an interceptor to check the credentials by accessing the <code>HttpSession</code> and checks for <code>USER_KEY</code> in it.</p>
  
  <p>Is it a bad idea to access to <code>HttpSession</code> in the interceptor itself? I guess interceptors are simple servlet filters in Java EE programming.</p>
  
  <p>I feel the best place to get access to <code>HttpSession</code> is inside the action class in Struts2.</p>
  
  <p>Please correct me in case I am wrong.</p>
  
  <pre><code>public class AuthorizationInterceptor extends AbstractInterceptor {
  
      private static final String USER_KEY = "user";
  
      public String intercept(ActionInvocation invocation) throws Exception {
          Map session = invocation.getInvocationContext().getSession();
          if (session.get(USER_KEY) == null) {
              addActionError(invocation, "You must be authenticated to access this page");
              return Action.ERROR;
          }
  
          return invocation.invoke();
      }
  
      private void addActionError(ActionInvocation invocation, String message) {
          Object action = invocation.getAction();
          if (action instanceof ValidationAware) {
              ((ValidationAware) action).addActionError(message);
          }
      }
  }
  </code></pre>