登录成功后,Spring Boot 重定向到当前页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26833452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot redirect to current page after successful login
提问by set4812
I have login forms in modal windows. After successful login user is redirected to /page. I am trying to find a method to stay on contact page or another page after login. How to do this? My code is:
我在模态窗口中有登录表单。成功登录后用户被重定向到/页面。我正在尝试找到一种方法在登录后留在联系页面或其他页面上。这该怎么做?我的代码是:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**","/js/**","/fonts/**","/images/**","/home","/","/kontakt").permitAll()
.antMatchers("/userlist").hasRole("ADMIN")
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/");
}
回答by Bohuslav Burghardt
You could use custom AuthenticationSuccessHandlerand set useRefererto true.
您可以使用 customAuthenticationSuccessHandler并设置useReferer为true.
@Bean
public AuthenticationSuccessHandler successHandler() {
SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
handler.setUseReferer(true);
return handler;
}
And in your configuremethod:
在你的configure方法中:
http
.formLogin()
.loginPage("/login")
.successHandler(successHandler())
.permitAll()
.and()
回答by Jonas
Just to provide an alternative solution:
只是为了提供替代解决方案:
formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
defaultSuccessUrlis a shortcut to adding the custom SuccessHandler.
defaultSuccessUrl是添加自定义SuccessHandler.
回答by benscabbia
I had a weird issue that would cause on login to redirect the user to localhost:8080/js/bootstrap.min.js
我有一个奇怪的问题会导致登录时将用户重定向到 localhost:8080/js/bootstrap.min.js
If anyone else is experiencing an odd redirection on login, which seems to override the .defaultSuccessUrl(), then try adding this code below in SecurityConfig:
如果其他人在登录时遇到奇怪的重定向,这似乎覆盖了.defaultSuccessUrl(),然后尝试在下面添加此代码SecurityConfig:
@Override
public void configure(WebSecurity security){
security.ignoring().antMatchers("/css/**","/images/**","/js/**");
}
Add all your Resources/staticfolders to the antMatchers()
将所有Resources/static文件夹添加到antMatchers()
回答by Augustin Ghauratto
You can as well do it in your AuthenticationSuccessHandlerimplementation:
你也可以在你的AuthenticationSuccessHandler实现中做到这一点:
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws
IOException, ServletException
{
//Do your logic;
response.sendRedirect(request.getHeader("referer");
}
回答by Isaac Riley
The Spring route, ala extending SavedRequestAwareAuthenticationSuccessHandleror SimpleUrlAuthenticationSuccessHandlercan be a bit clunky to implement. In the controller (ex. one that processes logins), you can do the header request yourself; ex:
Spring 路线,ala 扩展SavedRequestAwareAuthenticationSuccessHandler或实施起来SimpleUrlAuthenticationSuccessHandler可能有点笨拙。在控制器(例如处理登录的控制器)中,您可以自己执行标头请求;前任:
HttpServletRequest request =null;
HttpServletRequest request =null;
String priorUrl = request.getHeader("Referer");
String priorUrl = request.getHeader("Referer");
You will notice that you will have the URL prior to either a manual (initiated by user) logout or a session timeout (as handled by Spring session): you'll get an https://iAmPriorUrl.com/.... Then you can do whatever you want with it.
您会注意到,在手动(由用户启动)注销或会话超时(由 Spring 会话处理)之前,您将拥有 URL:您将获得 https://iAmPriorUrl.com/.... 然后你可以用它做任何你想做的事。
回答by Alexander
I had absolutely the same issue with inadequate redirect after adding bootstrap to my project tree.
在将引导程序添加到我的项目树后,我遇到了完全相同的重定向不足问题。
Method .defaultSuccessUrlwith flag = truesaved me time and lines of code.
方法 。defaultSuccessUrlwith flag = true 为我节省了时间和代码行。
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/", true)
.permitAll()
回答by Sida Zhou
Config is same as the accepted answer,
only luck I had was with extending SavedRequestAwareAuthenticationSuccessHandler.
Config 与接受的答案相同,唯一的运气是扩展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);
}
}
回答by raviteja kondrakunta
@Jonas All you need to do is add .requestCache() at the end
@Jonas 你需要做的就是在最后添加 .requestCache()
you config will look like this
你的配置看起来像这样
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.requestCache()

