Spring Security 5.7.0弃用 WebSecurityConfigurerAdapter

WebSecurityConfigurerAdapter详解 本文来详细说下security中的WebSecurityConfigurerAdapter 文章目录概述 概述 阅读详情
AI摘要

SpringSecurity5.7.0-M2开始弃用WebSecurityConfigurerAdapter,推荐使用基于组件的安全配置。本文提供了从WebSecurityConfigurerAdapter迁移到新配置风格的示例,包括HttpSecurity、WebSecurity的配置,以及LDAP、JDBC和内存内认证的实现。建议使用SecurityFilterChain、WebSecurityCustomizer、EmbeddedLdapServerContextSourceFactoryBean等新类进行配置,并注意安全最佳实践。

Spring Security 弃用 WebSecurityConfigurerAdapter

官网博客说明

官网博客链接地址:https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter

在Spring Security 5.7.0-M2,我们弃用了 WebSecurityConfigurerAdapter ,因为我们鼓励用户转向使用基于组件的安全配置。

为了帮助大家熟悉这种新的配置风格,我们编制了一份常见用例表和推荐的新写法。

在下面的例子中,我们遵循最佳实践——使用 Spring Security lambda 领域专用语言(DSL)和 HttpSecurity#authorizeHttpRequests 方法来定义我们的授权规则。如果你对lambda领域专用语言(DSL)不熟悉,你可以在这篇博客了解它。如果你想知道为什么我们选择选择使用 HttpSecurity#authorizeHttpRequests ,你可以看这篇 参考文档。

配置HttpSecurity

在Spring Security 5.4,我们介绍了创建一个SecurityFilterChain bean来配置HttpSecurity的功能。

下面是一个使用WebSecurityConfigurerAdapter和HTTP Basic保护所有端点的示例配置:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
    }
}

往后,我们建议注册一个SecurityFilterChain bean来做这件事:

@Configuration
public class SecurityConfiguration {

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

配置WebSecurity

在Spring Security 5.4中,我们还引入了WebSecurityCustomizer。

WebSecurityCustomizer是一个回调接口,可以用来定制WebSecurity。

下面是一个使用WebSecurityConfigurerAdapter忽略匹配/ignore1或/ignore2的请求的示例配置:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/ignore1", "/ignore2");
    }
}

往后,我们建议注册一个WebSecurityCustomizer bean来做这件事:

@Configuration
public class SecurityConfiguration {
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
    }
}

警告:如果你正在配置WebSecurity来忽略请求,建议你改为在HttpSecurity#authorizeHttpRequests内使用permitAll。想了解更多请参考configure Javadoc。

LDAP认证

在Spring Security 5.7,我们引入了EmbeddedLdapServerContextSourceFactoryBean、LdapBindAuthenticationManagerFactory和LdapPasswordComparisonAuthenticationManagerFactory,这些类都可以用来创建一个嵌入式的LDAP服务器;并且我们还引入一个AuthenticationManager类,它可以用来执行LDAP认证。

下面是一个使用是一个使用绑定验证的示例配置,它使用了WebSecurityConfigurerAdapter创建嵌入式LDAP服务器并且使用AuthenticationManager执行LDAP认证:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
            .userDetailsContextMapper(new PersonContextMapper())
            .userDnPatterns("uid={0},ou=people")
            .contextSource()
            .port(0);
    }
}

往后,我们建议使用新的LDAP类来做这件事:

@Configuration
public class SecurityConfiguration {
    @Bean
    public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
        EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
            EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
        contextSourceFactoryBean.setPort(0);
        return contextSourceFactoryBean;
    }

    @Bean
    AuthenticationManager ldapAuthenticationManager(
            BaseLdapPathContextSource contextSource) {
        LdapBindAuthenticationManagerFactory factory = 
            new LdapBindAuthenticationManagerFactory(contextSource);
        factory.setUserDnPatterns("uid={0},ou=people");
        factory.setUserDetailsContextMapper(new PersonContextMapper());
        return factory.createAuthenticationManager();
    }
}

JDBC认证

下面是一个示例配置,它在WebSecurityConfigurerAdapter内创建了一个使用默认模式初始化并且只有一个用户的内嵌DataSource:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .build();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        UserDetails user = User.withDefaultPasswordEncoder()
            .username("user")
            .password("password")
            .roles("USER")
            .build();
        auth.jdbcAuthentication()
            .withDefaultSchema()
            .dataSource(dataSource())
            .withUser(user);
    }
}

推荐的做法是创建一个JdbcUserDetailsManager bean来做这件事:

@Configuration
public class SecurityConfiguration {
    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
            .build();
    }

    @Bean
    public UserDetailsManager users(DataSource dataSource) {
        UserDetails user = User.withDefaultPasswordEncoder()
            .username("user")
            .password("password")
            .roles("USER")
            .build();
        JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);
        users.createUser(user);
        return users;
    }
}

注意:在这些例子中,我们为了可读性使用了User.withDefaultPasswordEncoder()。这不适合生产项目,我们建议在生产项目中使用散列密码。请按照参考文档所说的用Spring Boot命令行工具来做。

内存内认证

下面是一个示例配置,它在WebSecurityConfigurerAdapter配置了一个只存有一个用户的内存内用户:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        UserDetails user = User.withDefaultPasswordEncoder()
            .username("user")
            .password("password")
            .roles("USER")
            .build();
        auth.inMemoryAuthentication()
            .withUser(user);
    }
}

我们建议注册一个InMemoryUserDetailsManager bean来做这件事:

@Configuration
public class SecurityConfiguration {
    @Bean
    public InMemoryUserDetailsManager userDetailsService() {
        UserDetails user = User.withDefaultPasswordEncoder()
            .username("user")
            .password("password")
            .roles("USER")
            .build();
        return new InMemoryUserDetailsManager(user);
    }
}

注意:在这些例子中,我们为了可读性使用了User.withDefaultPasswordEncoder()。这不适合生产项目,我们建议在生产项目中使用散列密码。请按照参考文档所说的用Spring Boot命令行工具来做。

全局认证管理器

要创建一个整个应用都可以使用的AuthenticationManager,只需要使用@Bean将AuthenticationManager注册为bean就可以了。

这种配置已经在上面的LDAP认证示例展示过了。

局部认证管理器

在Spring Security 5.6中,我们引入了HttpSecurity#authenticationManager方法,这个方法可以为特定的SecurityFilterChain覆盖默认的AuthenticationManager。

下面是一个示例配置,它设置了一个自定义的AuthenticationManager:

@Configuration
public class SecurityConfiguration {

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

访问局部认证管理器

可以使用自定义领域专用语言(DSL)访问局部AuthenticationManager。这实际上是Spring Security内部实现HttpSecurity.authorizeRequests()等方法的方式。

public class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
        http.addFilter(new CustomFilter(authenticationManager));
    }

    public static MyCustomDsl customDsl() {
        return new MyCustomDsl();
    }
}

然后,在构建SecurityFilterChain时可以应用自定义领域专用语言(DSL):

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    // ...
    http.apply(customDsl());
    return http.build();
}

欢迎加入

我们很高兴与您分享这些更新,我们期待通过您的反馈进一步增强 Spring 安全性!如果你有兴趣贡献,你可以在GitHub上找到我们。

引用

https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter
https://www.cnblogs.com/cnblog-user/p/16386942.html

【Spring Security】WebSecurityConfigurerAdapter被deprecated怎么办?官方推荐新的Security配置风格总结 WebSecurityConfigurerAdapter被deprecated怎么办?这篇博文总结了官方推荐新的Security配置风格。 阅读详情

相关推荐

2024年Web前端最新深入理解 WebSecurityConfigurerAdapter【源码篇】,BAT面试&高级进阶

一个好的心态和一个坚持的心很重要,很多冲着高薪的人想学习前端,但是能学到最后的没有几个,遇到困难就放弃了,这种人到处都是,就是因为有的东西难,所以他的回报才很大,我们评判一个前端开发者是什么水平,就是他解决问题的能力有多强。开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】分享一些前端面试题以及学习路线给大家所以关于这两个类的介绍以及作用,松哥这里就不赘述了。咱们直接从 WebSecurityConfigurer 开始看起。

2301_82243626的博客 1427

Spring Security 六 WebSecurityConfigurerAdapter

文章目录一、WebSecurityConfigurerAdapter是什么?1.WebSecurityConfigurer是什么?2.WebSecurity2.1 WebSecurity定义2.2 AbstractConfiguredSecurityBuilder2.3二、使用步骤1.引入库2.读入数据总结 一、WebSecurityConfigurerAdapter是什么? WebSecurityConfigurerAdapter是为创建WebSecurityConfigurer实例提供方便的基类,该类允

weixin_43172297的博客 8580

Spring Security即将弃用配置类WebSecurityConfigurerAdapter

很多技术方案都不是直接更改的,是会有一个变化的过程,只要你紧追变化,其实也就没有变化。这一篇是不是学会了不少呢?欢迎留言发表看法,当然、再看也不能少哦。SpringSecurity的内置过滤器是如何维护的2022-02-21[这里是图片005]附DEMO|绝活!SpringSecurity过滤器就该这么配置2022-02-16[这里是图片006]OAuth2授权服务器Keycloak宣布不再适配SpringBoot和SpringSecurity2022-02-15https。...

geejkse_seff的博客 976

【Spring Security】—— WebSecurityConfigurerAdapter

官网地址 Spring Security Reference 版本:Version 5.5.0 WebSecurityConfigurerAdapter 继承关系图 Adapter 谷歌翻译:n. 【机】转接器 【网络】 适配器;适配器模式;接头。通过类名了解功能:我的理解,这个类是一个Web应用安全配置“接头”,及用户可通过这个“接头”接到自己的配置,也就是用户可以利用这个类来定制化安全配置。 SecurityBuilder 接口 对这个接口暂时没有细看,先根据官网了解了一下这个接口想要实

qq_33471737的博客 4433

Spring Security 5.7+ 升级指南:告别WebSecurityConfigurerAdapter,拥抱SecurityFilterChain

本文详细介绍了Spring Security 5.7+版本中WebSecurityConfigurerAdapter被弃用的原因及迁移到SecurityFilterChain的完整指南。通过对比新旧配置方式,提供从基础HttpSecurity配置到高级多SecurityFilterChain配置的迁移策略,帮助开发者平滑升级并解决常见问题。

weixin_28314575的博客 233

Audio2Face进阶玩法:结合LiveLink在UE5中实现MetaHuman全身动画+语音驱动(保姆级配置指南)

本文提供了一份详尽的保姆级教程,指导如何将NVIDIA Audio2Face与Unreal Engine 5的LiveLink功能结合,实现驱动MetaHuman数字人面部表情的同时,无缝融合身体动画。文章详细讲解了从环境准备、插件部署、基础面部驱动测试,到动画蓝图高级配置与多数据流融合的全流程,并分享了性能优化与问题排查的实战经验,旨在帮助创作者打造表情与肢体动作协调统一的生动数字人。

q9w8e7r6t5的博客 203

没有WebSecurityConfigurerAdapter的Spring Security

对于spring security来说,是该和WebSecurityConfigurerAdapter说再见了,因为在Spring Security 5.7.0-M2中WebSecurityConfigurerAdapter已经被弃用找不到了。在Spring Security 5.6中,我们引入了httpsecurity# authenticationManager方法,它覆盖了特定SecurityFilterChain的默认authenticationManager。

m0_74823947的博客 1469

RS485信号测量避坑指南:示波器抓包常见问题与波形分析技巧

本文详细解析了RS485信号测量中的常见问题与解决方案,包括差分信号测量基础、终端电阻配置、共模干扰处理等关键技巧。通过实测波形案例和优化建议,帮助工程师有效提升RS485通信的可靠性和抗干扰能力,适用于工业自动化、远程监测等场景。

kappa的博客 621

Spring Security 03 认证处理器

spring security 自定义任务处理器

weixin_46058921的博客 425

RM机器人麦轮底盘实战:从零搭建到PID调参全记录(附避坑指南)

本文详细记录了RM机器人麦轮底盘从零搭建到PID调参的全过程,涵盖硬件组装、开发环境搭建、底盘解算和PID调参等关键步骤,并附有实用的避坑指南。特别针对麦轮控制、底盘解算和电机调试等核心问题提供了专业解决方案,帮助开发者快速提升机器人性能。

bush的博客 389

ESP32-S3模组上兼容SCCB总线与I2C总线的解决方案(1)

ESP32-S3模组上兼容SCCB总线与I2C总线的解决方案(1)

phmatthaus的专栏 784

WebSecurityConfigurerAdapter已弃用

在 Spring Security 5.7.0-M2 中,弃用了 `WebSecurityConfigurerAdapter`,Spring 鼓励用户转向基于组件的安全配置。

lazy_LYF的博客 3万+

极智AI | 谈谈为什么卷积加速更喜欢 NHWC Layout

本文主要讨论一下为什么卷积加速更加喜欢 NHWC 的数据排布。

极智视界 2980

Spring Security - 如何修复 WebSecurityConfigurerAdapter 已弃用

抽象类,如下所示:这对于Spring Security版本5.6.5或更早版本,或者Spring Boot版本2.6.8或更早版本很好。的 bean,如下所示:供您参考,下面是将安全配置迁移到基于组件的方法的代码示例。的代码中为 HttpSecurity 指定此身份验证提供程序,如下所示:这就是在具有 Spring 安全性的基于 Spring 的应用程序中删除警告“在这篇简短的文章中,我想分享如何摆脱在带有Spring Security的基于Spring的应用程序中的“,请使用以下代码:并在。

allway2的博客 8299

第 1 章 Prometheus 监控 | Prometheus 入门

了解 Prometheus 监控系统,学会安装及了解页面功能。

甘蓝的专栏 493

Puchem化合物数据批量抓取采集_宝典_教程_Python爬虫

本文共计6500字,导出到PDF达42页(含代码)。从0教你如何抓取Pubchem化合物数据。 1、 Pubchem介绍 ​ Pubchem是美国国立卫生研究院(NIH)的开放式化学数据库,自2004年推出以来,pubchem已成为科学家、学生和公众的重要化学信息资源。其专门针对化学结构搜索,是网络药理学研究经常用到的数据库之一,不仅使用方便而且免费。 ​ PubChem主要包含小分子,但也包含较大的分子,例如核苷酸,碳水化合物,脂质,肽和化学修饰的大分子。收集有关化学结构,标识符,化学和物理性..

qq_34843422的博客 6079

Spring Security 替换WebSecurityConfigurerAdapter (Deprecated)的方法

在本文中,我将提供一个解决方案来配置Spring 安全性,而无需 WebSecurityConfigurerAdapter 类。在上面的例子中,我们遵循最佳实践,使用 Spring Security lambda DSL 和方法。我们创建了一个Spring Java配置类,它扩展了。类进行 Spring 安全配置,稍后我们将看到如何将此安全配置迁移到基于组件的方法。类的 JWT 配置,稍后我们将看到如何将此安全配置迁移到基于组件的方法。类已被弃用,Spring 团队鼓励用户转向基于组件的安全配置。

allway2的博客 2万+

Autosar MCAL-S32K324 Fee配置-基于EB

Autosar MCAL S32K324 Fee配置详解

赞的博客 1549

Spring Security 6 配置方法,废弃 WebSecurityConfigurerAdapter

在Spring Security 5.7.0-M2中,Spring就废弃了WebSecurityConfigurerAdapter,因为Spring官方鼓励用户转向基于组件的安全配置。本文整理了一下新的配置方法。在下面的例子中,我们使用Spring Security lambda DSL和HttpSecurity#authorizeHttpRequests方法来定义我们的授权规则,从而遵循最佳实践。

markvivv随笔 2万+

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Spring Security即将弃用WebSecurityConfigurerAdapter配置类

用过WebSecurityConfigurerAdapter的都知道对Spring Security十分重要,总管Spring Security的配置体系。但是马上这个类要废了,你没有看错,这个类将在5.7版本被@Deprecated所标记了,未来这个类将被移除 对此对此网友大呼“学着学着就被弃用了”。既然马上要弃用了,总要有个过渡方案或者新玩法吧。 早在2021年3月份胖哥就写了一篇文章,把新玩法给明明白白说清楚了,如果你看了的话,肯定不会学废弃技术。这里把整套的替代方案再搞一遍,可别再学过时技.

uuqaz的博客 1060

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Spring Security API参考:WebSecurityConfigurerAdapter使用

WebSecurityConfigurerAdapter是Spring Security框架中用于配置Web安全的适配器类(Adapter Class),它实现了WebSecurityConfigurer接口,为开发者提供了一种便捷的方式来定制HTTP请求的安全规则、用户认证(Authentication)和授权(Authorization)机制。在Spring Security 5.7.0之前的...

gitblog_00517的博客 1014

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

新版Spring Security弃用了WebSecurityConfigurerAdapter

在Spring Security 5.7.0-M2中,我们弃用了WebSecurityConfigurerAdapter,因为我们鼓励用户转向基于组件的安全配置。

weixin_43229159的博客 944

WebSecurityConfigurerAdapter被弃用Spring Security基于组件化的配置和使用

spring security过滤器链的组件化配置

个人学习笔记库,一篇一篇记录成长的足迹 5049
上一篇: SpringSecurity保姆级教程
下一篇: SpringBoot美化日志输出——自定义Banner艺术字体
字节微雕
博客等级 码龄7年 1粉丝 6原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值