【问题标题】:Spring Security ignoring context-pathSpring Security 忽略上下文路径
【发布时间】:2022-01-25 14:31:18
【问题描述】:

控制器

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

}

application.properties

server.servlet.context-path=/v1

春季安全

http.authorizeRequests().anyMatcher("/v1/**").authenicated()

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

对于上面的图片,我预计是 401,因为 v1/** 应该是授权的

一切正常,

http.authorizeRequests().anyMatcher("/**").authenicated()

【问题讨论】:

  • spring(web)安全性(通常)配置为“每个 servlet”,并且具有相同的“上下文根”......并且您的安全配置相对于它而言! (在浏览器中尝试:.../v1/v1 ;)
  • 或在安全配置中:.authorizeRequests().anyMatcher("/**").authenicated()
  • 所以上下文路径应该可以工作?我已经更新了我的问题
  • context-path 在 tomcat 或底层服务器上设置,然后在您的应用程序中配置所有其他。正如所指出的,您的配置现在显示“../v1/v1....”

标签: spring spring-boot spring-security


【解决方案1】:

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

@EnableWebSecurity(debug = true)

什么时候,

server.servlet.context-path=/v1

生成的请求:

Request received for GET '/api/data':

servletPath:/api/data
pathInfo:null

什么时候,

spring.mvc.servlet.path=/v1

生成的请求:

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

servletPath:/v1
pathInfo:/api/data

使用 servlet-path 来实现您要实现的功能...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2019-02-25
    • 2018-03-24
    相关资源
    最近更新 更多