成功登录后,Spring Boot重定向到当前页面 [英] Spring Boot redirect to current page after successful login

查看:265
本文介绍了成功登录后,Spring Boot重定向到当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模式窗口中有登录表单.成功登录后,用户将被重定向到/页面.我试图找到一种登录后留在联系页面或其他页面上的方法.这该怎么做?我的代码是:

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("/");
}

推荐答案

您可以使用自定义AuthenticationSuccessHandler并将useReferer设置为true.

You could use custom AuthenticationSuccessHandler and set useReferer to true.

@Bean
public AuthenticationSuccessHandler successHandler() {
    SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
    handler.setUseReferer(true);
    return handler;
}

并在您的configure方法中:

http
    .formLogin()
        .loginPage("/login")
        .successHandler(successHandler())
        .permitAll()
        .and()

这篇关于成功登录后,Spring Boot重定向到当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆