Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like you want to check in the constructor of the base action class, but you are mistaken. The constructor is used by the object factory to instantiate your action instance. In this stage a few things is available to you. In your case it's wrong. Another approach is if you move the logic into the method say <code>execute()</code> and call <code>super.execute()</code> before any method call would work, but if you forget to put the super call in the action then you may end up the action code running not authenticated. To prevent it you should run the code before any action is executed and be able to access the action instance or action context to be more Struts2. I guess you've never read the book "Struts 2 in Action" so I will give you some my own thoughts. It's about creating <code>AuthenticationInterceptor</code> and the action that implements <code>UserAware</code> that injects the user logged in into the action that implement this interface. The interceptor is looks like </p> <pre><code>public class AuthenticationInterceptor implements Interceptor { public void destroy() { } public void init() { } public String intercept(ActionInvocation actionInvocation) throws Exception { Map session = actionInvocation.getInvocationContext().getSession(); User user = (User) session.get(Struts2MyConstants.USER); if (user == null) { return Action.LOGIN; //login required result } else { Action action = (Action)actionInvocation.getAction(); if (action instanceof UserAware) { User freshUser = myService.getUser(user.getId()); ((UserAware)action).setUser(freshUser); } System.out.println("Logged in: interceptor"); return actionInvocation.invoke(); } } </code></pre> <p>The <code>UserAware</code> is looks like </p> <pre><code>public interface UserAware { public void setUser( User user ); } </code></pre> <p>and make a secure default stack that will reference any action</p> <pre><code>&lt;interceptors&gt; &lt;interceptor name="authenticationInterceptor" class="org.yourapp.struts.interceptor.AuthenticationInterceptor"/&gt; &lt;interceptor-stack name="secureStack"&gt; &lt;interceptor-ref name="authenticationInterceptor"/&gt; &lt;interceptor-ref name="defaultStack"/&gt; &lt;/interceptor-stack&gt; &lt;/interceptors&gt; &lt;default-interceptor-ref name="secureStack"/&gt; </code></pre> <p>If you make your base action to implement <code>UserAware</code> then the user object that is logged in will be available not only from session but in action too if you define getter for the user or make it protected. You have to make the <code>User</code> object immutable so that not compromise the security feature. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload