我想配置我的 Spring 应用程序安全性,所有请求都应在提供服务之前进行身份验证。
所以我创建了一个过滤器链 bean:
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
throws Exception {
return http
.authorizeRequests().anyRequest().authenticated()
.and().formLogin()
.and().build();
}
Run Code Online (Sandbox Code Playgroud)
我还发现authorizeRequests方法有一个重载版本,它接受定制器接口参数。所以我尝试了参数化版本。
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
throws Exception {
return http
.authorizeRequests(authorizeRequests ->
authorizeRequests.anyRequest().authenticated()
)
.formLogin()
.and().build();
}
Run Code Online (Sandbox Code Playgroud)
我注意到参数化的authorizeRequests方法将返回相同的HttpSecurity对象,因此您可以继续配置而无需调用and()。
这是他们之间唯一的区别吗?如果真是这样,这个重载版本岂不是显得多余了吗?