简体   繁体   English

从 Struts 2 中的登录拦截器重定向

[英]Redirect from login interceptor in Struts 2

Our application requires users to be logged in to view any content.我们的应用程序要求用户登录才能查看任何内容。 Access to all pages is intercepted by LoginInterceptor which brings up the login form if there's no valid session for the user. LoginInterceptor会拦截对所有页面的访问,如果用户没有有效的会话,它会显示登录表单。

I'd like the interceptor to remember the original request URI before displaying the login form and redirect to it if the login form validation is successful.我希望拦截器在显示登录表单之前记住原始请求 URI,如果登录表单验证成功则重定向到它。

I tried to follow Struts 2 Redirect to correct action after authentication interceptor .我试图按照Struts 2 Redirect 在身份验证拦截器之后进行正确的操作

@Service
@Results({
    @Result(name = "redirect", type = "redirect", location = "${savedUrl}")
})
public class LoginInterceptor extends AbstractInterceptor {
    //...
    private String savedUrl;
    //...
    @Override
    public final String intercept(final ActionInvocation invocation) throws Exception {
       // ...
       savedUrl = (String) session.getAttribute("savedUrl");
       // ...
       if (processLogin(request, session)) { // validate login form
           if (!StringUtils.isEmpty(savedUrl)) {
              return "redirect";
           }
           return LOGIN_SUCCESS_RESULT;
       }
       // if there's no loginData in sesssion, remeber the URI and display a login form
       String queryString = request.getQueryString();
       session.setAttribute("savedUrl", request.getRequestURI() + (queryString==null ? "" : ("?" + queryString)));
       return "login";
    }
    // ...
    public String getSavedUrl(){
       return savedUrl;
    }
}

However I get a blank page as a result of return "redirect" .但是,由于return "redirect"我得到了一个空白页。 getSavedUrl() is never called. getSavedUrl()永远不会被调用。

Solution:解决方法:

Scratch the @Results annotation completely and instead of return "redirect";完全刮掉@Results注释,而不是return "redirect"; call打电话

response.sendRedirect(savedUrl); return null;

If not logged in then redirect to LOGIN result.如果未登录,则重定向到LOGIN结果。 Then you should rewrite your interceptor something like然后你应该重写你的拦截器

public final String intercept(final ActionInvocation invocation) throws Exception {
   // before save original url
   Map session = invocation.getInvocationContext().getSession();
   Object action = invocation.getAction();
   if (!(action instanceof LoginAction)){ 
     String queryString = request.getQueryString();
     session.put("savedUrl", request.getRequestURI()+(queryString==null?"":("?"+queryString)));
   } else {
     return invocation.invoke();
   }

   if (!processLogin(request, session)) { //return false if not authenticated
     session.put("isLogin", true);
     return Action.LOGIN;
   } else {
       savedUrl = (String) session.get("savedUrl");
       boolean isLogin = (boolean)session.get("isLogin");
       if (!StringUtils.isEmpty(savedUrl) && isLogin) {
          session.put("isLogin", false); 
          return "redirect";
       }
       return invocation.invoke();
   }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM