java - http.antMatcher("/**") .authorizeRequests().antMatchers("/") 中的antMatcher("/**") 需要什么?
问题描述
我正在学习 spring 安全性,我从https://spring.io/guides/tutorials/spring-boot-oauth2/发现了这段代码
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated();
}
我删除.antMatcher("/**")
了,代码仍然有效。我了解**
匹配路径中的零个或多个目录,所以我认为antMatcher("/**").authorizeRequestes().antMatcher("/login")
会"/login"
直接或间接匹配根路径下的路径,即我希望它匹配路径/login
,/demo/login
但事实并非如此,它只匹配/login
根路径下的路径。那么究竟需要.antMatcher("/**") here
什么呢?
解决方案
它们是不同的东西。
http.antMatcher()
配置将由 this 处理的 URLSecurityFilterChain
。默认是匹配所有 URL。这就是为什么如果你删除它是一样的http.antMatcher("/**")
。http.authorizeRequests()
为 URL 配置授权事项,例如是否需要对其进行身份验证或是否只有某些角色可以访问它等。
所以如果一个 URL 与 不匹配http.antMatcher()
,Spring security 将不会处理它并且http.authorizeRequests()
不会应用到这个 URL。也就是说,要让in中配置的URLhttp.authorizeRequests()
生效,也要由Spring Security处理并匹配in http.antMatcher()
。
推荐阅读
- objective-c - NSPredicate - predicateWithFormat 不安全
- java - [Java]:当请求的数字不在列表中时,如何从排序列表中获取最近的较小数字
- javascript - 如何在 Angular 项目中使用 gh3
- javascript - Electron.js 防止窗口有条件地关闭
- codenameone - 代号一个地图组件不允许我输入经度和纬度(+权限)
- flutter - Flutter中的Material和MaterialApp有什么区别?
- c++ - C 与 C++ 编译不兼容 - 未命名类型
- cplex - 如何在没有 IDE 的情况下执行 Cplex 运行配置
- html - Twig 模板:循环二维数组
- automation - 如何只运行失败的测试(量角器+茉莉花)