Spring Security redirect to previous page after successful login

前端 未结 9 1187
孤独总比滥情好
孤独总比滥情好 2020-11-27 10:09

I know this question has been asked before, however I\'m facing a particular issue here.

I use spring security 3.1.3.

I have 3 possible login cases in my web

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 10:48

    I found Utku Özdemir's solution works to some extent, but kind of defeats the purpose of the saved request since the session attribute will take precedence over it. This means that redirects to secure pages will not work as intended - after login you will be sent to the page you were on instead of the redirect target. So as an alternative you could use a modified version of SavedRequestAwareAuthenticationSuccessHandler instead of extending it. This will allow you to have better control over when to use the session attribute.

    Here is an example:

    private static class MyCustomLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    
        private RequestCache requestCache = new HttpSessionRequestCache();
    
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                Authentication authentication) throws ServletException, IOException {
            SavedRequest savedRequest = requestCache.getRequest(request, response);
    
            if (savedRequest == null) {
                HttpSession session = request.getSession();
                if (session != null) {
                    String redirectUrl = (String) session.getAttribute("url_prior_login");
                    if (redirectUrl != null) {
                        session.removeAttribute("url_prior_login");
                        getRedirectStrategy().sendRedirect(request, response, redirectUrl);
                    } else {
                        super.onAuthenticationSuccess(request, response, authentication);
                    }
                } else {
                    super.onAuthenticationSuccess(request, response, authentication);
                }
    
                return;
            }
    
            String targetUrlParameter = getTargetUrlParameter();
            if (isAlwaysUseDefaultTargetUrl()
                    || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
                requestCache.removeRequest(request, response);
                super.onAuthenticationSuccess(request, response, authentication);
    
                return;
            }
    
            clearAuthenticationAttributes(request);
    
            // Use the DefaultSavedRequest URL
            String targetUrl = savedRequest.getRedirectUrl();
            logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
            getRedirectStrategy().sendRedirect(request, response, targetUrl);
        }
    }
    

    Also, you don't want to save the referrer when authentication has failed, since the referrer will then be the login page itself. So check for the error param manually or provide a separate RequestMapping like below.

    @RequestMapping(value = "/login", params = "error")
    public String loginError() {
        // Don't save referrer here!
    }
    

提交回复
热议问题