原文链接:Spring Security without the WebSecurityConfigurerAdapter

作者:ELEFTHERIA STEIN-KOUSATHANA

发表日期:2022年2月21日

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

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

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

配置HttpSecurity

在Spring Security 5.4,我们引入了创建一个SecurityFilterChian bean来配置HttpSecurity的功能。

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

 1 @Configuration
2 public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
3
4 @Override
5 protected void configure(HttpSecurity http) throws Exception {
6 http
7 .authorizeHttpRequests((authz) -> authz
8 .anyRequest().authenticated()
9 )
10 .httpBasic(withDefaults());
11 }
12
13 }

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

 1 @Configuration
2 public class SecurityConfiguration {
3
4 @Bean
5 public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6 http
7 .authorizeHttpRequests((authz) -> authz
8 .anyRequest().authenticated()
9 )
10 .httpBasic(withDefaults());
11 return http.build();
12 }
13
14 }

配置WebSecurity

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

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

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

1 @Configuration
2 public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
3
4 @Override
5 public void configure(WebSecurity web) {
6 web.ignoring().antMatchers("/ignore1", "/ignore2");
7 }
8
9 }

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

1 @Configuration
2 public class SecurityConfiguration {
3
4 @Bean
5 public WebSecurityCustomizer webSecurityCustomizer() {
6 return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
7 }
8
9 }

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

LDAP认证

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

下面是一个使用是一个使用绑定验证的示例配置,它使用了WebSecurityConfugurerAdapter创建嵌入式LDAP服务器并且使用AuthenticationManager执行LDAP认证(Below is an example configuration using WebSecurityConfigurerAdappter the that creates an embedded LDAP server and an AuthenticationManager that performs LDAP authentication using bingd authentication):

 1 @Configuration
2 public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
3
4 @Override
5 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
6 auth
7 .ldapAuthentication()
8 .userDetailsContextMapper(new PersonContextMapper())
9 .userDnPatterns("uid={0},ou=people")
10 .contextSource()
11 .port(0);
12 }
13
14 }

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

 1 @Configuration
2 public class SecurityConfiguration {
3 @Bean
4 public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
5 EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
6 EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
7 contextSourceFactoryBean.setPort(0);
8 return contextSourceFactoryBean;
9 }
10
11 @Bean
12 AuthenticationManager ldapAuthenticationManager(
13 BaseLdapPathContextSource contextSource) {
14 LdapBindAuthenticationManagerFactory factory =
15 new LdapBindAuthenticationManagerFactory(contextSource);
16 factory.setUserDnPatterns("uid={0},ou=people");
17 factory.setUserDetailsContextMapper(new PersonContextMapper());
18 return factory.createAuthenticationManager();
19 }
20 }

JDBC认证

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

 1 @Configuration
2 public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
3 @Bean
4 public DataSource dataSource() {
5 return new EmbeddedDatabaseBuilder()
6 .setType(EmbeddedDatabaseType.H2)
7 .build();
8 }
9
10 @Override
11 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
12 UserDetails user = User.withDefaultPasswordEncoder()
13 .username("user")
14 .password("password")
15 .roles("USER")
16 .build();
17 auth.jdbcAuthentication()
18 .withDefaultSchema()
19 .dataSource(dataSource())
20 .withUser(user);
21 }
22 }

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

 1 @Configuration
2 public class SecurityConfiguration {
3 @Bean
4 public DataSource dataSource() {
5 return new EmbeddedDatabaseBuilder()
6 .setType(EmbeddedDatabaseType.H2)
7 .addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
8 .build();
9 }
10
11 @Bean
12 public UserDetailsManager users(DataSource dataSource) {
13 UserDetails user = User.withDefaultPasswordEncoder()
14 .username("user")
15 .password("password")
16 .roles("USER")
17 .build();
18 JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);
19 users.createUser(user);
20 return users;
21 }
22 }

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

内存内认证

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

 1 @Configuration
2 public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
3 @Override
4 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
5 UserDetails user = User.withDefaultPasswordEncoder()
6 .username("user")
7 .password("password")
8 .roles("USER")
9 .build();
10 auth.inMemoryAuthentication()
11 .withUser(user);
12 }
13 }

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

 1 @Configuration
2 public class SecurityConfiguration {
3 @Bean
4 public InMemoryUserDetailsManager userDetailsService() {
5 UserDetails user = User.withDefaultPasswordEncoder()
6 .username("user")
7 .password("password")
8 .roles("USER")
9 .build();
10 return new InMemoryUserDetailsManager(user);
11 }
12 }

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

全局AuthenticationManager

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

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

局部AuthenticationManager

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

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

 1 @Configuration
2 public class SecurityConfiguration {
3
4 @Bean
5 public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6 http
7 .authorizeHttpRequests((authz) -> authz
8 .anyRequest().authenticated()
9 )
10 .httpBasic(withDefaults())
11 .authenticationManager(new CustomAuthenticationManager());
12 return http.build();
13 }
14
15 }

访问局部AuthenticationManager

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

 1 public class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {
2 @Override
3 public void configure(HttpSecurity http) throws Exception {
4 AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
5 http.addFilter(new CustomFilter(authenticationManager));
6 }
7
8 public static MyCustomDsl customDsl() {
9 return new MyCustomDsl();
10 }
11 }

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

1 @Bean
2 public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3 // ...
4 http.apply(customDsl());
5 return http.build();
6 }

欢迎加入

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

【翻译】Spring Security抛弃了WebSecurityConfigurerAdapter的更多相关文章

  1. 【翻译】Spring Security - 如何解决WebSecurityConfigurerAdapter类已被弃用的问题?

    原文链接:Spring Security - How to Fix WebSecurityConfigurerAdapter Deprecated 原文作者:Nam Ha Minh 原文发表日期:20 ...

  2. SpringBoot第二十三篇:安全性之Spring Security

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11350255.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   系统的安全 ...

  3. Spring Security 自定义 登陆 权限验证

    转载于:https://www.jianshu.com/p/6b8fb59b614b 项目简介 基于Spring Cloud 的项目,Spring Cloud是在Spring Boot上搭建的所以按照 ...

  4. 【微服务】 数据库案例理解Spring Security OAuth

    突然被问,你是做技术的怎么不走技术路线呢?是啊~仔细想想至今做了这么多年的技术,研发过的系统&产品五花八门,涉及到的领域各行各业:政府.军队.公安.国安.石油&石化.金融.教育.华为等 ...

  5. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  6. Spring Security(一):官网向导翻译

    原文出自  https://spring.io/guides/topicals/spring-security-architecture Spring Security Architecture   ...

  7. spring security 继承 WebSecurityConfigurerAdapter 的重写方法configure() 参数 HttpSecurity 常用方法及说明

    HttpSecurity 常用方法及说明 方法 说明 openidLogin() 用于基于 OpenId 的验证 headers() 将安全标头添加到响应 cors() 配置跨域资源共享( CORS ...

  8. Spring Security即将弃用WebSecurityConfigurerAdapter配置类

    用过WebSecurityConfigurerAdapter的都知道对Spring Security十分重要,总管Spring Security的配置体系.但是马上这个类要废了,你没有看错,这个类将在 ...

  9. 【翻译】Thymeleaf – Spring Security集成模块

    原文链接:Thymeleaf - Spring Security integration modules 来源:thymeleaf/thymeleaf-extras-springsecurity自述文 ...

随机推荐

  1. 使用Python批量爬取美女图片

    运行截图 实列代码: from bs4 import BeautifulSoup import requests,re,os headers = { 'User-Agent': 'Mozilla/5. ...

  2. 【Java】学习路径58-TCP聊天-双向发送实现

    这一章内容比较复杂(乱) 重点在于解决利用TCP协议实现双向传输. 其余的细节(比如end)等,不需要太在意. 但是我也把折腾经历写出来了,如果大家和我遇到了类似的问题,下文可以提供一个参考. 目标: ...

  3. Codeforces Round #604 (Div. 2) -D

    Problem - D - Codeforces 题意 : 有 a 个0,b个1,c个2,d个3,构成一个序列,使得每两个数字之间的差值为1 题解: 就是以四种数字分别为起点,暴力模拟 #includ ...

  4. 搭建docker镜像仓库(二):使用harbor搭建本地镜像仓库

    目录 一.系统环境 二.前言 三.Harbor 四.使用harbor搭建私有镜像仓库 4.1 环境介绍 4.2 k8smaster节点安装配置harbor 4.2.1 安装harbor离线包 4.2. ...

  5. 使用plsql_plprofiler 分析过程块的执行

    前言:存储过程可能涉及很多的SQL及控制块,我们看到的执行时间是整个过程块的执行时间,如果我们认为性能有问题,我们只能逐条SQL的分析,查找问题SQL,效率非常低下.KingbaseES 提供了 pl ...

  6. 《现代操作系统(中文第四版)》实验一 bash脚本实现cal

    题目大意:让你写一个bash脚本,实现与linux下cal相同的效果 学习bash语言题,除了区别和c++有亿点区别外其他都还好 1 monName=("invalid" &quo ...

  7. [Python]-sklearn.model_selection模块-处理数据集

    拆分数据集train&test from sklearn.model_selection import train_test_split 可以按比例拆分数据集,分为train和test x_t ...

  8. Elasticsearch不支持事务有什么好的弥补方案

    1.问题 源自星球同学的提问:es如何与hive或mysql结合使用?es不支持事务有什么好的弥补方案吗? 2.事务的核心概念 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下ACID四个 ...

  9. 复现禅道V16.5的SQL注入(CNVD-2022-42853)

    漏洞详情 禅道V16.5未对输入的account参数内容作过滤校验,导致攻击者拼接恶意SQL语句执行. 环境搭建 环境下载:禅道V16.5 下载后双击运行,进入目录运行start.exe 直接访问即可 ...

  10. 2_JDBC

    一. 引言 1.1 如何操作数据库 使用客户端工具访问数据库, 需要手工建立连接, 输入用户名和密码登陆, 编写SQL语句, 点击执行, 查看操作结果(结果集或受行数影响) 1.2 实际开发中, 会采 ...