草庐IT

【Spring Security】WebSecurityConfigurerAdapter被deprecated怎么办?官方推荐新的Security配置风格总结

自牧君 2023-07-17 原文

h


本期目录



背景

笔者使用 Spring Security 5.8 时,发现网上很多教程所教的 Spring Security 配置类 SecurityConfig.java 的配置风格还是停留在继承 WebSecurityConfigurerAdapter 的风格。然而, WebSecurityConfigurerAdapter 在 Spring Security 5.7.0-M2 版本中已经被 deprecated 了。因此在本文中分享 Spring 官方最新推荐的 Spring Security 配置风格。


一、 前言

在 Spring Security 5.7.0 (2022 年 2 月 21 日更新) 中,官方弃用了 WebSecurityConfigurerAdapter 。因为Spring 官方鼓励开发者朝着组件化安全配置迁移。为了帮助开发者顺利过渡到这种配置风格,Spring 官方准备了一系列常见的使用案例和建议的替代方案。

在下面的例子中,我们将遵循最佳实践,使用 Spring Security lambda DSL 和 HttpSecurity.java 中的 authorizeHttpRequests() 方法来定义授权规则。如果您对 lambda DSL 感到陌生,可以参考我的这篇文章进行学习:《如何使用Lambda DSL配置Spring Security》。


二、 配置HttpSecurity

在 Spring Security 5.4 中,新增了通过创建一个 SecurityFilterChain 的 Bean 来配置 HttpSecurity 的功能。

首先来看看没有弃用 WebSecurityConfigurerAdapter 的示例,下面是使用 WebSecurityConfigurerAdapter 的配置示例,该配置使用 httpBasic() 方法来保护所有的接口。

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

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

}

但今后, WebSecurityConfigurerAdapter 就被 Spring 官方弃用了,取而代之的是通过注册一个 SecurityFilterChain 的 Bean 来配置 HttpSecurity

@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 中,Spring 官方新增了 WebSecurityCustomizer

WebSecurityCustomizer 是一个回调接口,可以用来配置 WebSecurity

首先来看看先前的旧配置风格的代码示例:使用 WebSecurityConfigurerAdapter 来忽略与 /ignore1/ignore2 匹配的请求 (即不拦截这两个请求进行认证授权) 。

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

但今后,Spring 官方不再推荐上面的配置风格,取而代之的是向 Spring 容器中注入一个 WebSecurityCustomizer 的 Bean 。

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

注意:如果你想通过配置 WebSecurity 来忽略请求,建议优先考虑通过配置 HttpSecurityauthorizeHttpRequests() 方法,使用 .permitAll() 来实现。


四、 配置LDAP认证

LDAP (Light Directory Access Protocol) ,是基于 X.500 标准的轻量级目录访问协议。有兴趣的同学可以自行谷歌搜索学习,LDAP 这种协议常用于授权认证的情景。

在 Spring Security 5.7 中,Spring 官方新增了 EmbeddedLdapServerContextSourceFactoryBeanLdapBindAuthenticationManagerFactoryLdapPasswordComparisonAuthenticationManagerFactory ,用来创建一个嵌入式 LDAP 服务器和一个 AuthenticationManager 对象,来执行 LDAP 认证。

同样的,先来看先前旧的配置风格的示例,继承 WebSecurityConfigurerAdapter ,创建一个嵌入式 LDAP 服务器,创建一个 AuthenticationManager ,使用绑定认证 (Bind Authentication) 来执行 LDAP 认证。

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ladpAuthentication()
            .userDetailsContextMapper(new PersonContextMapper())
            .userDnPatterns("uid={0}, ou=people")
            .contextSource()
            .port(0);
    }
    
}

但今后,Spring 官方不再推荐上面的配置风格,取而代之的是使用新的 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);
    }
    
}

但今后,Spring 官方不再推荐上面的配置风格,取而代之的是向 Spring 容器注入一个 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;
    }
    
}

注意:上面的代码示例第 14 行中,为了密码的可读性才使用方法 User.withDefaultPasswordEncoder() 。在实际的生产环境中并不会使用默认的密码编码器,因为这样存储的密码是明文,十分不安全。这里推荐使用 BCryptPasswordEncoder 作为密码编码器来对密码进行加密成暗文存储。


六、 In-Memory Authentication

与上面存储在数据库的不同,In-Memory 是把用户信息存储在内存中。

同样的,先来看先前旧的配置风格的示例,继承 WebSecurityConfigurerAdapter 来配置存储在内存中的一个用户信息。

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

但今后,Spring 官方不再推荐上面的配置风格,取而代之的是向 Spring 容器注入一个 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);
    }
    
}

注意:上面的代码示例第 6 行中,为了密码的可读性才使用方法 User.withDefaultPasswordEncoder() 。在实际的生产环境中并不会使用默认的密码编码器,因为这样存储的密码是明文,十分不安全。这里推荐使用 BCryptPasswordEncoder 作为密码编码器来对密码进行加密成暗文存储。


七、 配置全局AuthenticationManager

如果你想要创建整个应用都可以调用的 AuthenticationManager 对象,只需要简单地使用注解 @Bean 来注入 Spring 容器。

配置风格示例与 LDAP 的配置示例相同。


八、 配置局部AuthenticationManager

在 Spring Security 5.6 中,Spring 官方在 HttpSecurity 中新增了方法 authenticationManager() ,该方法重写具体的 SecurityFilterChain 的默认 AuthenticationManager

下面是配置自定义局部 AuthenticationManager 的代码示例。

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

九、 调用局部AuthenticationManager

可以使用 custom 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();
    }
}

当Spring Security开始构建 SecurityFilterChain 时,custom DSL 就会被自动调用。

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

以上就是最新的 Spring Security 官方推荐的配置风格总结,如果对你有帮助,希望可以给博主点一个免费的赞。

有关【Spring Security】WebSecurityConfigurerAdapter被deprecated怎么办?官方推荐新的Security配置风格总结的更多相关文章

  1. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  2. ruby - 怎么来的(a_method || :other) returns :other only when assigning to a var called a_method? - 2

    给定以下方法:defsome_method:valueend以下语句按我的预期工作:some_method||:other#=>:valuex=some_method||:other#=>:value但是下面语句的行为让我感到困惑:some_method=some_method||:other#=>:other它按预期创建了一个名为some_method的局部变量,随后对some_method的调用返回该局部变量的值。但为什么它分配:other而不是:value呢?我知道这可能不是一件明智的事情,并且可以看出它可能有多么模棱两可,但我认为应该在考虑作业之前评估作业的右侧...我已经在R

  3. ruby-on-rails - 我该怎么办 :remote location validation with CarrierWave? - 2

    我在我的Rails3示例应用程序上使用CarrierWave。我想验证远程位置上传,因此当用户提交无效URL(空白或非图像)时,我不会收到标准错误异常:CarrierWave::DownloadErrorinImageController#createtryingtodownloadafilewhichisnotservedoverHTTP这是我的模型:classPaintingtrue,:length=>{:minimum=>5,:maximum=>100}validates:image,:presence=>trueend这是我的Controller:classPaintingsC

  4. ruby-on-rails - Rails 中的推荐引擎 - 2

    我想为我的Rails网络应用程序提供推荐功能。特别是,我想向新注册的用户推荐他可能想要关注的其他用户。Rails中是否有用于此目的的引擎/gem?如果没有,我应该从哪里开始构建它?谢谢。 最佳答案 有Coletivogemhttps://github.com/diogenes/coletivo我试了一下。在MySQL上运行。Neo4jhttp://neo4j.org真的很容易实现一个“跟随谁”。事实上,大多数展示其能力的样本都涉及“跟随谁”。快速提示-只有在JRuby上运行时,Neo4j.rb才会很酷。如果不是-使用Neograph

  5. 电脑0x0000001A蓝屏错误怎么U盘重装系统教学 - 2

      电脑0x0000001A蓝屏错误怎么U盘重装系统教学分享。有用户电脑开机之后遇到了系统蓝屏的情况。系统蓝屏问题很多时候都是系统bug,只有通过重装系统来进行解决。那么蓝屏问题如何通过U盘重装新系统来解决呢?来看看以下的详细操作方法教学吧。  准备工作:  1、U盘一个(尽量使用8G以上的U盘)。  2、一台正常联网可使用的电脑。  3、ghost或ISO系统镜像文件(Win10系统下载_Win10专业版_windows10正式版下载-系统之家)。  4、在本页面下载U盘启动盘制作工具:系统之家U盘启动工具。  U盘启动盘制作步骤:  注意:制作期间,U盘会被格式化,因此U盘中的重要文件请注

  6. SPI接收数据异常问题总结 - 2

    SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手

  7. ruby-on-rails - 一般建议和推荐的文件夹结构 - Sinatra - 2

    您将如何构建一个简单的Sinatra应用程序?我正在制作,我希望该应用具有以下功能:“应用程序”更像是一个包含所有信息的管理仪表板。然后另一个应用程序将通过REST访问信息。我还没有创建仪表板,只是从数据库中获取东西session和身份验证(尚未实现)您可以上传图片,其他应用可以显示这些图片我已经使用RSpec创建了一个测试文件通过Prawn生成报告目前的设置是这样的:app.rbtest_app.rb因为我实际上只有应用程序和测试文件。到目前为止,我已经将Datamapper用于ORM,将SQLite用于数据库。这是我的第一个Ruby/Sinatra项目,所以欢迎任何和所有建议-我应

  8. ruby - EventMachine - 你怎么知道你是否落后了? - 2

    我正在研究使用EventMachine支持的twitter-streamruby​​gem来跟踪和捕获推文。我对整个事件编程有点陌生。我如何判断我在事件循环中所做的任何处理是否导致我落后?有没有简单的检查方法? 最佳答案 您可以通过使用周期性计时器并打印出耗时来确定延迟。如果您使用的是1秒的计时器,您应该已经过了大约1秒,如果它更长,您就知道您正在减慢react器的速度。@last=Time.now.to_fEM.add_periodic_timer(1)doputs"LATENCY:#{Time.now.to_f-@last}"@

  9. ruby - 如果它是标点符号,我怎么能从字符串中删除最后一个字符,在 ruby​​ 中? - 2

    啊,正则表达式有点困惑。我正在尝试删除字符串末尾所有可能的标点符号:ifstr[str.length-1]=='?'||str[str.length-1]=='.'||str[str.length-1]=='!'orstr[str.length-1]==','||str[str.length-1]==';'str.chomp!end我相信有更好的方法来做到这一点。有什么指点吗? 最佳答案 str.sub!(/[?.!,;]?$/,'')[?.!,;]-字符类。匹配这5个字符中的任何一个(注意,。在字符类中并不特殊)?-前一个字符或组

  10. ruby - Ruby 中 <=> 运算符的名称是什么?他们怎么调用它? - 2

    在Ruby中有运算符(operator)。在API中,他们没有命名它的名字,只是:Theclassmustdefinetheoperator...Comparableusestoimplementtheconventionalcomparison......theobjectsinthecollectionmustalsoimplementameaningfuloperator...它叫什么名字? 最佳答案 参见上面的@Tony。然而,它也被称为(俚语)“宇宙飞船运算符(operator)”。

随机推荐