场景
我有一个 spring-boot 应用程序,可以使用上下文而不是域根来访问它:
- 域根:http://app.com
- 我的应用程序:http://app.com/organization_a (将我重定向到我的应用登录)
- 我的应用程序登录:http://app.com/organization_a/login (表单已完美加载)
问题
登录成功后,使用经典配置:
formLogin()
.loginPage("/login")
.defaultSuccessUrl("/my_form")
将我重定向到:http://app.com/my_form抛出错误,因为 url 必须是: http://app.com/organization_a/my_form
所以,我尝试使用相对网址(注意/my_form之前的点)./my_form而不是/my_form
formLogin()
.loginPage("/login")
.defaultSuccessUrl("./my_form")
Spring 向我抛出此错误:由于 spring-security-web jar 中的此验证,defaultTarget 必须以“/”或“http(s)”开头:
//org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler
public void setDefaultTargetUrl(String defaultTargetUrl) {
Assert.isTrue(UrlUtils.isValidRedirectUrl(defaultTargetUrl),
"defaultTarget must start with '/' or with 'http(s)'");
this.defaultTargetUrl = defaultTargetUrl;
}
So, I can say: Classic default success url method does not allow to use relative urls, because if I use root domain , works like charm as in my localhost.
相对网址与绝对网址
更多信息here
我在 java 中的 html 和资源以及 PHP 和 Nodejs 等其他语言中也遇到了类似的问题。
基本上,如果您使用上下文而不是应用程序的域根,则必须需要更改此设置:
<link href="/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
到
<link href="./vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
注意/vendor/bootstrap/... 之前的点
如果没有这个点,您的资源将不会下载,因为浏览器请求的网址如下:
什么时候应该:
当前的解决方法
- 使用域或子域代替上下文。
- 这很费力,因为我需要为每个新应用程序向我的 ISP 开具一张票证。
- 将整个 URL 放入 defaultSuccessUrl 方法中。
- 这太糟糕了,因为我的应用程序无法移植,因为我必须在构建之前设置绝对网址:
formLogin()
.loginPage("/login")
.defaultSuccessUrl("http://app.com/organization_a/my_form")
问题
Is there a way to use relative redirect in spring security?
我正在使用最新的 Spring Boot 版本之一:2.1.7.RELEASE
感谢任何帮助或评论。
谢谢
最佳答案
我设法通过以下 spring 属性启用相对重定向:
server:
tomcat:
use-relative-redirects: true
关于java - 在 Spring Boot 中使用相对重定向失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57503924/