Struts 2 - Redirecting to a correct action after authentication interceptor

拜拜、爱过 提交于 2019-11-26 21:44:29

问题


I couldn't find anything online so I'll ask here. I'm using strut2 and I have a private package of actions, cause certain actions require login to be access. So I have this in my secure package:

<interceptors>
    <interceptor name="authenticationInterceptor" class="com.koorde.interceptor.AuthenticationInterceptor"/>

    <interceptor-stack name="secureStack">
      <interceptor-ref name="authenticationInterceptor"/>
      <interceptor-ref name="defaultStack"/>    
    </interceptor-stack>
</interceptors>

<default-interceptor-ref name="secureStack"/>

<global-results>
    <result name="login" type="redirect">dologin</result>
    <result name="session_error" type="redirect"> html/error/hibernate_session.jsp</result>
    <result name="error" type="redirect"> html/error/hibernate_session.jsp</result>
</global-results>

And of course other actions definition.

My problem is the following:

Let's say a user want to access his personal area. He clicks on personalArea link and he will be automatically redirected on login page (cause personalArea is a secure action). What I want is: after login user is automatically redirect (to continue the action) to personalArea and not home page.

So, what I want is: when user log in into the system because of a secure actions, after login the execution of the action (secured) continues.

How can I do that?


回答1:


One of the possible solutions is to keep track of the user intercepting their URLs. You might do it in the authentication interceptor.

String queryString = request.getQueryString();
session.put("savedUrl", request.getRequestURI()+(queryString==null?"":("?"+queryString))); 

use the global result with dynamic parameter

@Results({
  @Result(name = "return", type = "redirect", location = "${savedUrl}")
})

after login check the session for savedUrl and return result "return". Assumed providing getter for the dynamic parameter.




回答2:


At the end I was able to make it work. Basically what I missed from the solution proposed by Roman C. was that I need to have a class variable saved (so not only have it in the session).

private String savedUrl;

public String getSavedUrl(){
    return savedUrl;
}

That did the trick.

Thanks



来源:https://stackoverflow.com/questions/18410407/struts-2-redirecting-to-a-correct-action-after-authentication-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!