成功登录后,Spring引导重定向到当前页面
我在模态窗口中有登录表单。成功登录后,用户被重定向到成功登录后,Spring引导重定向到当前页面,spring,spring-security,spring-boot,Spring,Spring Security,Spring Boot,我在模态窗口中有登录表单。成功登录后,用户被重定向到/页面。我试图找到一种方法,以保持在联系页面或其他页面后登录。如何做到这一点?我的代码是: @覆盖 受保护的无效配置(HttpSecurity http)引发异常{ http .授权请求() .antMatchers(“/css/**”,“/js/**”,“/font/**”,“/images/**”,“/home”,““/”,“/kontakt”).permitAll() .antMatchers(“/userlist”).hasRole(“
/
页面。我试图找到一种方法,以保持在联系页面或其他页面后登录。如何做到这一点?我的代码是:
@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http
.授权请求()
.antMatchers(“/css/**”,“/js/**”,“/font/**”,“/images/**”,“/home”,““/”,“/kontakt”).permitAll()
.antMatchers(“/userlist”).hasRole(“管理员”)
.anyRequest().authenticated();
http
.formLogin()
.login页面(“/login”)
.permitAll()
.及()
.logout()
.logoutRequestMatcher(新的AntPathRequestMatcher(“/logout”))
.logoutSuccessUrl(“/”);
}
您可以使用自定义的AuthenticationSuccessHandler
并将useReferer
设置为true
@Bean
public AuthenticationSuccessHandler successHandler() {
SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
handler.setUseReferer(true);
return handler;
}
在配置方法中:
http
.formLogin()
.loginPage("/login")
.successHandler(successHandler())
.permitAll()
.and()
您也可以在AuthenticationSuccessHandler
实现中执行此操作:
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws
IOException, ServletException
{
//Do your logic;
response.sendRedirect(request.getHeader("referer");
}
只是为了提供另一种解决方案:
formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
defaultSuccessUrl
是添加自定义SuccessHandler
的快捷方式,我遇到了一个奇怪的问题,在登录时会导致用户重定向到localhost:8080/js/bootstrap.min.js
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
System.out.println("HEP HEP");
super.onAuthenticationSuccess(request, response, authentication);
}
}
如果其他任何人在登录时遇到异常重定向,这似乎覆盖了.defaultSuccessUrl()
,请尝试在下面的SecurityConfig
中添加此代码:
@Override
public void configure(WebSecurity security){
security.ignoring().antMatchers("/css/**","/images/**","/js/**");
}
将所有Resources/static
文件夹添加到antMatchers()
@Jonas只需在末尾添加.requestCache()
你会像这样
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.requestCache()
Spring路由扩展SavedRequestAwareAuthenticationSuccessHandler
或SimpleRuuthenticationSuccessHandler
的实现可能有点笨拙。在控制器(例如处理登录的控制器)中,您可以自己执行头请求;例:
HttpServletRequest请求=null代码>
String priorUrl=request.getHeader(“Referer”)代码>
您将注意到,在手动(由用户启动)注销或会话超时(由Spring会话处理)之前,您将拥有URL:您将获得一个https://iAmPriorUrl.com/...
。然后你可以用它做任何你想做的事情。我在将引导添加到我的项目树后遇到了完全相同的重定向不足问题
flag=true的defaultSuccessUrl方法节省了我的时间和代码行
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/", true)
.permitAll()
配置与,
我唯一的幸运是扩展了SavedRequestAwareAuthenticationSuccessHandler
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
System.out.println("HEP HEP");
super.onAuthenticationSuccess(request, response, authentication);
}
}
您的答案与问题的当前版本不匹配。你的答案是有缺陷的,defaultSuccessUrl
是SavedRequestAwareAuthenticationSuccessHandler
的快捷方式,而不是自定义SuccessHandler
。我不知道你为什么会被低估。你的回答帮助我解决了完全相同的问题。对我来说也是有效的(我有一个奇怪的重定向到logo.png)这是我唯一有效的解决方案,谢谢,真的。关键是要使用SavedRequestAwareAuthenticationSuccessHandler
。很难相信这么简单!谢谢你这样使用它。defaultSuccessUrl(“/goToThisUrlAfterLogin”,true)让我高兴极了!