首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >authorizeHttpRequests​(Customizer<AuthorizeHttpRequestsConfigurer.AuthorizationManagerRequestMatcherRegistry>与authorizeRequests()之争

authorizeHttpRequests​(Customizer<AuthorizeHttpRequestsConfigurer.AuthorizationManagerRequestMatcherRegistry>与authorizeRequests()之争
EN

Stack Overflow用户
提问于 2022-07-23 09:55:17
回答 1查看 1.3K关注 0票数 3

我想配置我的spring应用程序安全性,所有请求都应该在被处理之前进行身份验证。

所以我创建了一个过滤器链bean:

代码语言:javascript
复制
    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
            throws Exception {
        return http
            .authorizeRequests().anyRequest().authenticated()
            .and().formLogin()
            .and().build();
    }

我还发现authorizeRequests方法有一个过载版本,它接受一个Customizer接口参数。所以我尝试了参数化版本。

代码语言:javascript
复制
    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
            throws Exception {
        return http
            .authorizeRequests(authorizeRequests ->
                authorizeRequests.anyRequest().authenticated()
            )

            .formLogin()
            .and().build();
    }

我注意到参数化的authorizeRequests方法将返回相同的HttpSecurity对象,因此您可以在不调用和()的情况下继续配置。

这是他们之间唯一的区别吗?如果是这样的话,这个超载的版本不是多余的吗?

EN

回答 1

Stack Overflow用户

发布于 2022-07-23 12:08:53

声明authorizeRequests的两种方法都是有效的。接受自定义程序的方法可以使您的代码更容易阅读,因为它避免了多个缩进级别。建议使用lambda自定义程序。

authorizeRequestsauthorizeHttpRequests的区别在于这里authorizeHttpRequests使用新的简化AuthorizationManager API和AuthorizationFilter,而authorizeRequests使用AccessDecisionManagerFilterSecurityInterceptor。后者将在Security的未来版本中被废弃。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73089730

复制
相关文章

相似问题

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