简体   繁体   English

在 Struts 2 中的身份验证拦截器之后重定向到正确的操作

[英]Redirecting to a correct action after authentication interceptor in Struts 2

I'm using Struts 2 and I have a private package of actions, because certain actions require login to be accessed.我正在使用 Struts 2 并且我有一个私有的操作包,因为某些操作需要登录才能访问。 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).他点击personalArea链接,他将被自动重定向到登录页面(因为personalArea是一个安全操作)。 What I want is: after login user is automatically redirect (to continue the action) to personalArea and not home page.我想要的是:登录后用户自动重定向(以继续操作)到personalArea而不是主页。

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?我怎样才能做到这一点?

One of the possible solutions is to keep track of the user intercepting their URLs.一种可能的解决方案是跟踪用户拦截他们的 URL。 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" .登录后检查会话的savedUrl并返回结果"return" Assumed providing getter for the dynamic parameter.假设为动态参数提供 getter。

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).基本上我从 Roman C. 提出的解决方案中错过的是我需要保存一个类变量(所以不仅在会话中保存它)。

private String savedUrl;私人字符串savedUrl;

public String getSavedUrl(){
    return savedUrl;
}

That did the trick.这就是诀窍。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 struts2拦截器重定向两次以执行操作 - struts2 interceptor redirecting twice to action 该页面没有被 Struts 2 中的拦截器正确重定向 - The page is not redirecting properly by interceptor in Struts 2 拦截器的invocation.invoke()之后没有填充Struts 2动作变量 - Struts 2 action variables not populated after interceptor's invocation.invoke() 如果拦截器中的身份验证失败,Struts2将返回json - Struts2 return json if authentication failed in interceptor 在拦截器中访问struts动作结果的参数 - Accessing struts action result's parameters in an interceptor Struts2拦截器-如何读取动作参数 - Struts2 interceptor - how to read action parameters 更改控制器后,Struts操作未呈现正确的路径 - Struts action not rendering correct path after change in controller 调用execAndWait拦截器后,重定向到struts.xml操作中未定义的页面 - Getting redirected to page which is not defined in struts.xml action after calling execAndWait interceptor 在struts中使用单个拦截器作为default-interceptor-ref是否正确? - Is it correct for using a single interceptor as default-interceptor-ref in struts? Struts 2.3 Store Interceptor:如何将ActionErrors从Interceptor传递给Action? - Struts 2.3 Store Interceptor : How to pass ActionErrors from Interceptor to Action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM