首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >withDefaults()的目的是什么?

withDefaults()的目的是什么?
EN

Stack Overflow用户
提问于 2022-08-29 13:47:35
回答 2查看 270关注 0票数 1

根据春季医生,它说:

返回不更改输入参数的Customizer。

但这究竟意味着什么呢?

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

代码语言:javascript
运行
复制
@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();
  }
}
EN

回答 2

Stack Overflow用户

发布于 2022-08-30 15:42:49

根据爪哇文档,我们有

代码语言:javascript
运行
复制
 public HttpSecurity httpBasic(Customizer<HttpBasicConfigurer<HttpSecurity>> httpBasicCustomizer) throws Exception {
            httpBasicCustomizer.customize((HttpBasicConfigurer)this.getOrApply(new HttpBasicConfigurer()));
            return this;
        }

该参数的类型为Customizer<HttpBasicConfigurer<HttpSecurity>>,可用作lambda函数,以传递您希望应用于提供给httpBasic方法的配置程序中的更改。此方法还返回生成的HttpSecurity,因此在httpBasic(....)方法结束时已经应用了配置程序。

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

代码语言:javascript
运行
复制
httpSecurity.httpBasic(httpSecurityHttpBasicConfigurer -> {
                          httpSecurityHttpBasicConfigurer.realmName("My Realm");
                          httpSecurityHttpBasicConfigurer.authenticationEntryPoint(new YourAuthEntryClass());
                          })
    .authorizeRequests().and().csrf().disable().authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());

因此,配置程序将通过您提供的lambda函数将realmNameauthenticationEntryPoint应用于httpSecurity

如果您不想在httpSecurity方法中对httpBasic进行任何修改,那么您也可以在

代码语言:javascript
运行
复制
httpSecurity.httpBasic(httpSecurityHttpBasicConfigurer -> {} )
       .authorizeRequests().and().csrf().disable().authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());

为了避免将这个非httpSecurityHttpBasicConfigurer -> {}作为参数写入,Spring还提供了函数接口Customizer中的静态withDefaults方法。请记住,这个Customizer只是一个通用接口,也将在其他地方使用,而不仅仅是在这里。

@FunctionalInterface公共接口定制器{ void ( t);静态定制器withDefaults() {返回(T) -> { };}

因此,为了避免

代码语言:javascript
运行
复制
  httpSecurity.httpBasic(httpSecurityHttpBasicConfigurer -> {} )....

你也可以写

代码语言:javascript
运行
复制
 httpSecurity.httpBasic(Customizer.withDefaults())....

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

请记住,但是,您也可以从Java文档获得另一个方法

public HttpBasicConfigurer<HttpSecurity> httpBasic() throws Exception { return (HttpBasicConfigurer)this.getOrApply(new HttpBasicConfigurer()); }

它也可以使用,这并不返回修改过的httpSecurity对象,而是一个HttpBasicConfigurer,它可以编写成使用构建器模式修改httpSecurity

因此,示例1现在可以编写为

代码语言:javascript
运行
复制
httpSecurity.httpBasic()
            .realmName("My Realm")
            .authenticationEntryPoint(new YourAuthEntryClass())
            .and().authorizeRequests().and().csrf().disable()
            .authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());

如果您不想向httpSecurity应用任何基本的http配置更改,您只需跳过构建器模式中的realmNameauthenticationEntryPoint方法,它将再次为您提供httpSecurity的默认基本配置

代码语言:javascript
运行
复制
httpSecurity.httpBasic()
                .and()
                .authorizeRequests().and().csrf().disable()
                .authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());

的版本完全相同。

代码语言:javascript
运行
复制
httpSecurity.httpBasic(Customizer.withDefaults())
                .authorizeRequests().and().csrf().disable()
                .authorizeHttpRequests((authz) -> authz.anyRequest().authenticated());
票数 1
EN

Stack Overflow用户

发布于 2022-08-29 13:52:40

使用默认设置。

为了前夫,

  1. 授权谁
  2. 如何授权
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73529846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档