tomcat - 根据动态字段更改 url

标签 tomcat spring-boot spring-security

有没有办法在用户登录系统后更改 url?例如,如果我的登录 url 是 demo.xxx.com/login,在用户通过身份验证后,我希望它更改为 abc.xxx.com/dashboard。 abc 是用户组,在用户通过身份验证时确定。我的应用程序基于 spring boot,并使用 spring security 进行身份验证。

最佳答案

最简单的方法是在 spring security WebSecurityConfigurerAdapter bean 中设置默认的成功 url。如果使用 XML 配置,我相信这也可以配置,尽管我不确定如何配置。

@Configuration
@EnableWebSecurity
public class AppSecurityConfiguration extends WebSecurityConfigurerAdapter {
    protected void configure( HttpSecurity http ) throws Exception {
        http.formLogin()
            .loginPage( "/login" )
            .defaultSuccessUrl( "/dashboard" );
    }
}

如果您需要更好地控制重定向,您可以实现自己的身份验证成功处理程序并提供自定义重定向策略。

http.formLogin().successHandler( new SavedRequestAwareAuthenticationSuccessHandler() {
    {
        setRedirectStrategy( new RedirectStrategy() {
            @Override
            public void sendRedirect( HttpServletRequest request, HttpServletResponse response, String url ) throws IOException {
                // Put your logic here for sending redirects
                response.sendRedirect("https://xyx.mydomain.com/dashboard");
            }
        } );
    }
} );

关于tomcat - 根据动态字段更改 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46813311/

相关文章:

java - 在 Spring Boot 中运行 hql 查询时没有得到任何结果

java - 你怎么能找出java应用程序是建立在Spring或Spring Boot或Spring MVC上的

Spring OAuth2多服务器注释配置(资源和授权)

java - 将 spring boot 与 vaadin 结合使用时,js 文件出现 404

Tomcat 8集群提高性能

java - Jenkins 在构建后操作后运行构建后步骤

java - springboot Thymeleaf 添加不同的列表字段

grails - 使用用户名作为电子邮件,Spring Security UI 插件

java - java文件运行出错

java - 如何使用基本身份验证阻止 tomcat 中的 servlet session ?