Java Spring referer在登录时为空,并且赢得';t重定向

Java Spring referer在登录时为空,并且赢得';t重定向,java,spring-boot,spring-mvc,Java,Spring Boot,Spring Mvc,我有一个使用安全性的Spring boot应用程序。我想要的是重定向到请求的原始URL。我需要实现一个自定义的SimpleRuthenticationSuccessHandler,因此我有以下代码: public class MySavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { public MySavedRequestAwareAuthentication

我有一个使用安全性的Spring boot应用程序。我想要的是重定向到请求的原始URL。我需要实现一个自定义的SimpleRuthenticationSuccessHandler,因此我有以下代码:

public class MySavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

public MySavedRequestAwareAuthenticationSuccessHandler() {
    setUseReferer(true);
}

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {

// do my stuff

            logger.info(getTargetUrlParameter()); // this is = '/'
            //logger.info(getDefaultTargetUrl()); // this is null
            redirectStrategy.sendRedirect(request, response, "/");
}
}
问题是,登录后永远不会重定向到referer。我不得不添加重定向策略。sendRedirect(请求,响应,“/”部分

在LoginController中,当从spring security重定向时,标头中的“Referer”始终为空。如果我输入url“xxxx/login”,则设置该值。不是很有用:

@RequestMapping(value = {"/login"}, method = RequestMethod.GET)
public ModelAndView login(HttpServletRequest request) {       

    String referer = request.getHeader("Referer");
    modelAndView.addObject ("referer", referer); // this is NULL
    //
    modelAndView.setViewName("login");
    return modelAndView;
}
这是我的安全配置:

http.authorizeRequests()
                .antMatchers("/login").permitAll()
                .antMatchers("/").authenticated()               
                .and()
                .csrf().disable()
                .formLogin()
                .loginPage("/login")
                .failureHandler(customAuthenticationFailureHandler)
                .usernameParameter("email")
                .passwordParameter("password")
                .successHandler(authenticationSuccessHandler)
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login?logged-out")
                .and()
                .exceptionHandling()
                .accessDeniedPage("/access-denied")

Referer(或规范中的Referer)通常为空。用户可以将浏览器设置为从不发送推荐人。另一个例子是,当从https重定向到http时,假定浏览器会删除引用者。您是试图重定向到应用程序中的某个特定位置,还是试图将用户重定向到另一个域?如果是本地的,.Im尝试在同一站点内重定向。这就是为什么重定向到登录页面的原因。如果请求只是内部的,我建议使用此处引用的基于会话的重定向:从阅读引用的页面。您可能可以删除这两行代码:
.failureHandler(customAuthenticationFailureHandler)
.successHandler(authenticationSuccessHandler)
,一切都应该正常工作。因为“对于表单登录,SavedRequestAwareAuthenticationSuccessHandler用作默认的AuthenticationSuccessHandler。”这很好。