从我拥有此配置的那一刻起,我一直在尝试为不同的 api 端点设置多个安全配置:
http
.antMatcher("/user/**")
.authorizeRequests()
.antMatchers("/user/document/**").permitAll()
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
此配置有效,但当我尝试删除第二个 authorizeRequests()
时,我也很困惑(见下文)。配置也有效。
http
.antMatcher("/user/**")
.authorizeRequests()
.antMatchers("/user/document/**").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
这个方法有什么作用?我每次都必须调用它吗?
最佳答案
引用文档:
Note that the matchers are considered in order.
在您的配置中,/user/**
优先于 /user/document/**
。因此,在这两种情况下,都不会应用第二个 antMatcher(...) 。
回答你的第二个问题(我需要每次都调用它吗?) - 答案是否。这是一个示例配置(同样来自文档):
http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/**").hasRole("USER").and().formLogin();
关于java - Spring Security中的authorizeRequests有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57780908/