有没有办法在用户登录系统后更改 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/