Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
成功登录后,Spring引导重定向到当前页面_Spring_Spring Security_Spring Boot - Fatal编程技术网

成功登录后,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)让我高兴极了!