Struts2 redirect from login interceptor

后端 未结 1 972
广开言路
广开言路 2021-01-01 05:20

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 the

相关标签:
1条回答
  • 2021-01-01 05:44

    If not logged in then redirect to LOGIN result. 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();
       }
    }
    
    0 讨论(0)
提交回复
热议问题