Spring Security 忽略上下文路径

use*_*167 1 spring spring-security spring-boot

控制器

@GetMapping("/api/data")
String response(){

}
Run Code Online (Sandbox Code Playgroud)

应用程序属性

server.servlet.context-path=/v1
Run Code Online (Sandbox Code Playgroud)

弹簧安全

http.authorizeRequests().anyMatcher("/v1/**").authenicated()
Run Code Online (Sandbox Code Playgroud)

这里没有发生身份验证。我相信, spring-security 忽略了application.properties 中配置的上下文路径。为什么 spring-security 忽略上下文路径。如何解决这个问题?

对于上图,我预计会出现 401,因为 v1/** 应该已获得授权

这工作正常,

http.authorizeRequests().anyMatcher("/**").authenicated()
Run Code Online (Sandbox Code Playgroud)

ima*_*yle 5

打开 Spring Security 的调试,然后你就会明白发生了什么。

@EnableWebSecurity(debug = true)
Run Code Online (Sandbox Code Playgroud)

什么时候,

server.servlet.context-path=/v1
Run Code Online (Sandbox Code Playgroud)

生成的请求:

Request received for GET '/api/data':

servletPath:/api/data
pathInfo:null
Run Code Online (Sandbox Code Playgroud)

什么时候,

spring.mvc.servlet.path=/v1
Run Code Online (Sandbox Code Playgroud)

生成的请求:

Request received for GET '/v1/api/data':

servletPath:/v1
pathInfo:/api/data
Run Code Online (Sandbox Code Playgroud)

使用 servlet-path 来实现您想要实现的内容...