You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Security首次登录成功后无法正确跳转地址问题求助

问题根因
  • 你使用的defaultSuccessUrl(String defaultSuccessUrl)方法默认第二个alwaysUse参数为false,该模式下Spring Security登录成功后优先跳转用户登录前被拦截的请求路径,只有无历史拦截路径记录时才会跳转你指定的默认地址。
  • 服务器首次启动后,用户首次访问服务通常会触发根路径/的请求(比如浏览器默认访问域名根路径、自动请求/favicon.ico资源等),该请求被权限拦截后跳转到登录页,Spring Security会将/记录为登录后目标跳转地址,因此首次登录后直接跳转/。
  • 第二次登录时session中已无历史拦截路径记录,因此正常跳转到你配置的默认地址。
解决方案

修改Spring Security的登录成功配置,给defaultSuccessUrl方法添加第二个参数true,强制登录成功后固定跳转到指定地址,忽略之前的拦截路径记录:

.loginPage("/admin/to/login/page.html") 
.loginProcessingUrl("/security/do/login.html") 
// 第二个参数设为true,强制使用指定的成功跳转地址
.defaultSuccessUrl("/admin/to/main/page.html", true)  
.usernameParameter("loginAcct")   
.passwordParameter("userPswd")      
.and()
.logout()                    
.logoutUrl("/security/do/logout.html") 
.logoutSuccessUrl("/admin/to/login/page.html")

如果不想强制所有场景都固定跳转,也可以单独配置根路径/的访问权限,避免无权限的根路径请求被拦截记录到跳转缓存中。

内容的提问来源于stack exchange,提问作者shineron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 09:09:04