Spring Security withDefaults() 的目的是什么

rah*_*l s 7 spring spring-security spring-boot

根据Spring Security定制器的 withDefaults()指示:

返回一个Customizer不改变输入参数的值。

但这究竟意味着什么呢?

例如,如果我像这样使用它,结果是什么:

@EnableWebSecurity
@Configuration
public class SecurityConfiguration {

   @Bean
   public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
     http.csrf().disable()
         .authorizeHttpRequests((authz) -> authz.anyRequest().authenticated())
         .httpBasic(withDefaults());
     return http.build();
   }

}
Run Code Online (Sandbox Code Playgroud)

Pan*_*kos 10

根据java文档,我们有

 public HttpSecurity httpBasic(Customizer<HttpBasicConfigurer<HttpSecurity>> httpBasicCustomizer) throws Exception {
            httpBasicCustomizer.customize((HttpBasicConfigurer)this.getOrApply(new HttpBasicConfigurer()));
            return this;
        }
Run Code Online (Sandbox Code Playgroud)

该参数的类型Customizer<HttpBasicConfigurer<HttpSecurity>>可用作 lambda 函数,以传递您想要在提供给该httpBasic方法的配置器中应用的更改。此方法还返回构建的内容HttpSecurity,因此当该方法结束时配置器已经应用httpBasic(....)

相对于您的示例,我们将其命名为示例 ​​1

httpSecurity.httpBasic(httpSecurityHttpBasicConfigurer -> {
                          httpSecurityHttpBasicConfigurer.realmName("My Realm");
                          httpSecurityHttpBasicConfigurer.authenticationEntryPoint(new YourAuthEntryClass());
                          })
    .authorizeRequests().and().csrf().disable().authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());
Run Code Online (Sandbox Code Playgroud)

因此配置器将通过您提供的 lambda 函数将realmNameauthenticationEntryPoint应用于。httpSecurity

如果您不想对方法httpSecurity内部进行任何修改httpBasic,您也可以这样做

httpSecurity.httpBasic(httpSecurityHttpBasicConfigurer -> {} )
       .authorizeRequests().and().csrf().disable().authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());
Run Code Online (Sandbox Code Playgroud)

Spring 只是为了避免将这些废话写httpSecurityHttpBasicConfigurer -> {}为参数,还在withDefaults功能接口中为您提供了静态方法Customizer。请记住,这Customizer只是一个通用接口,不仅在这里,还会在其他地方使用。

@FunctionalInterface
public interface Customizer<T> {
    void customize(T t);

    static <T> Customizer<T> withDefaults() {
        return (t) -> {
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

所以为了避免

  httpSecurity.httpBasic(httpSecurityHttpBasicConfigurer -> {} )....
Run Code Online (Sandbox Code Playgroud)

你也可以写

 httpSecurity.httpBasic(Customizer.withDefaults())....
Run Code Online (Sandbox Code Playgroud)

httpBasic意味着不会在对象的方法内部应用任何配置httpSecurity

但请记住,您还可以使用Java 文档中的另一种方法。

   public HttpBasicConfigurer<HttpSecurity> httpBasic() throws Exception {
                return (HttpBasicConfigurer)this.getOrApply(new HttpBasicConfigurer());
            }
Run Code Online (Sandbox Code Playgroud)

也可以使用它,并且它不会返回修改后的httpSecurity对象,而是返回一个HttpBasicConfigurer可以编写为httpSecurity使用构建器模式修改的对象。

所以示例 1现在可以写成

httpSecurity.httpBasic()
            .realmName("My Realm")
            .authenticationEntryPoint(new YourAuthEntryClass())
            .and().authorizeRequests().and().csrf().disable()
            .authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());
Run Code Online (Sandbox Code Playgroud)

如果您不想应用任何基本的 http 配置更改,httpSecurity您可以跳过构建器模式中的realmName方法,它会再次为您提供默认的基本配置authenticationEntryPointhttpSecurity

httpSecurity.httpBasic()
                .and()
                .authorizeRequests().and().csrf().disable()
                .authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());
Run Code Online (Sandbox Code Playgroud)

这将与版本完全相同

httpSecurity.httpBasic(Customizer.withDefaults())
                .authorizeRequests().and().csrf().disable()
                .authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());
Run Code Online (Sandbox Code Playgroud)


Nav*_*ngh 0

使用默认设置。

对于前,

  1. 授权给谁
  2. 如何授权