gpt4 book ai didi

java - http.antMatcher ("/**") .authorizeRequests().antMatchers ("/**") 中的 antMatcher ("/") 需要什么?

转载 作者:行者123 更新时间:2023-12-01 19:31:57 25 4
gpt4 key购买 nike

我正在学习 Spring Security,我从 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()配置此 SecurityFilterChain 将处理哪个 URL 。默认匹配所有 URL。这就是为什么如果删除 http.antMatcher("/**") 效果是一样的。 .

  • http.authorizeRequests()配置 URL 的授权事项,例如是否需要进行身份验证或是否只有某些角色可以访问等。

因此,如果 URL 与 http.antMatcher() 不匹配,Spring security 不会处理它并且 http.authorizeRequests()不适用于此 URL。换句话说,为了让http.authorizeRequests()中配置URL要生效,需要由 Spring Security 处理并在 http.antMatcher() 中进行匹配也是。

关于java - http.antMatcher ("/**") .authorizeRequests().antMatchers ("/**") 中的 antMatcher ("/") 需要什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59484889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com