【发布时间】:2019-02-01 09:33:44
【问题描述】:
我正在研究一些 spring-security 的代码。我想了解我在互联网上找到的这个例子 1:
http.requestMatchers()
.antMatchers("/management/**") // (1)
.and()
.authorizeRequests() // (2)
.antMatchers("/management/health")
.permitAll()
.antMatchers("/management/info")
.permitAll()
.antMatchers("/management/**")
.hasRole("ACTUATOR")
.anyRequest().permitAll()
.and()
.httpBasic(); (3)
}
看不懂这个配置,为什么是这个代码:
http.requestMatchers()
.antMatchers("/management/**")
.and()
在 .authorizeRequests() 之前? (1)
这是什么意思?
你能解释一下这个例子吗?
2:第二种情况,有什么区别?
http.requestMatchers().antMatchers("/rest2/**")
.and()
.authorizeRequests()
.antMatchers("/rest/v1/test/hello").permitAll()
.antMatchers("/rest/v1/test/**").denyAll()
.and()
.requestMatchers().antMatchers("/rest/**")
.and()
.authorizeRequests()
.antMatchers("/rest/v1/test/hello").permitAll();
使用 requestMatchers() 有什么影响?
如果我向“/rest/v1/test/hello2”发送请求,我收到 401 为什么如果拒绝请求的规则与 antMatchers("/rest2/**") 不匹配?
【问题讨论】:
-
不要在一个问题中问两个不同的问题。您应该打开两个单独的问题,因为它们不相关。
-
但是,你第一个问题的答案是你对默认值的理解是错误的。在这种情况下,默认意味着没有自定义 Spring Security 配置,但您有自定义 Sprig Security 配置。
-
您的第二个问题很可能是重复的。例如:stackoverflow.com/questions/35890540/…(大约是
antMatcher,但requestMatchers的解释是一样的。 -
@dur,我将我的问题分为两个问题,请参阅stackoverflow.com/questions/52039148/…。我仍然怀疑为什么在其他配置之前使用 requestMatchers(),我在示例中几乎只看到了 authorizeRequests()。
标签: spring-boot spring-security