Spring Security redirect to previous page after successful login

前端 未结 9 1183
孤独总比滥情好
孤独总比滥情好 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:45

    I want to extend Olcay's nice answer. His approach is good, your login page controller should be like this to put the referrer url into session:

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(HttpServletRequest request, Model model) {
        String referrer = request.getHeader("Referer");
        request.getSession().setAttribute("url_prior_login", referrer);
        // some other stuff
        return "login";
    }
    

    And you should extend SavedRequestAwareAuthenticationSuccessHandler and override its onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) method. Something like this:

    public class MyCustomLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
        public MyCustomLoginSuccessHandler(String defaultTargetUrl) {
            setDefaultTargetUrl(defaultTargetUrl);
        }
    
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
            HttpSession session = request.getSession();
            if (session != null) {
                String redirectUrl = (String) session.getAttribute("url_prior_login");
                if (redirectUrl != null) {
                    // we do not forget to clean this attribute from session
                    session.removeAttribute("url_prior_login");
                    // then we redirect
                    getRedirectStrategy().sendRedirect(request, response, redirectUrl);
                } else {
                    super.onAuthenticationSuccess(request, response, authentication);
                }
            } else {
                super.onAuthenticationSuccess(request, response, authentication);
            }
        }
    }
    

    Then, in your spring configuration, you should define this custom class as a bean and use it on your security configuration. If you are using annotation config, it should look like this (the class you extend from WebSecurityConfigurerAdapter):

    @Bean
    public AuthenticationSuccessHandler successHandler() {
        return new MyCustomLoginSuccessHandler("/yourdefaultsuccessurl");
    }
    

    In configure method:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                // bla bla
                .formLogin()
                    .loginPage("/login")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .successHandler(successHandler())
                    .permitAll()
                // etc etc
        ;
    }
    

提交回复
热议问题