开发者

Spring Security使用Lambda DSL配置流程详解

开发者 https://www.devze.com 2023-02-16 10:28 出处:网络 作者: 自牧君
目录1. 概述2. 新老配置风格对比Lambda风格等效的旧配置风格3. WebFlux Securit开发者_Python开发y4. Lambda DSL的目标1. 概述
目录
  • 1. 概述
  • 2. 新老配置风格对比
    • Lambda风格
    • 等效的旧配置风格
  • 3. WebFlux Securit开发者_Python开发y
    • 4. Lambda DSL的目标

      1. 概述

      在 Spring Secjavascripturity 5.2 中增强了 DSL 的功能:允许使用 Lambda 表达式来配置 HTTP security 编程客栈

      需要注意的是:先前版本的配置风格仍然是有效的且受支持的。Spring 官方额外新增 Lambda 表达式是为了提高代码的灵活性,只是一个可选的用法。

      下面让我们看一下 Lambda 表达式配置 HTTP security 和先前的配置风格的对比。

      2. 新老配置风格对比

      Lambda风格

      @EnableWebSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                  .authorizeRequests(authorizeRequests ->
                      authorizeRequests
                          .antMatchers("/blog/**"www.devze.com).permitAll()
                          .anyRequest().authenticated()
                  )
                  .formLogin(formLogin ->
                      formLogin
                          .loginPage("/login")
                          .permitAll()
                  )
                  .rememberMe(withDefaults());
          }
      }
      

      等效的旧配置风格

      @EnableWebSecurity
      publicAFxNB class SecurityConfig extends WebSecurityConfigurerAdapter {
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                  .authorizeRequests()
                      .antMatchers("/blog/**").permitAll()
                      .anyRequest().authenticated()
                      .and()
                  .formLogin()
                      .loginPage("/login")
                      .permitAll()
                      .and()
                  .rememberMe();
          }
      }
      

      对比上述两种配置风格,你会注意到一些关键的不同点:

      在 Lambda 风格中,不再需要通过 .and() 方法来串联配置项。

      在调用 Lambda 方法后,HttpSecurity 对象 http 会自动返回以继续执行进一步的配置。

      方法 withDefaults() 可以使用 Spring Security 提供的默认值启用安全功能。这是 Lambda 表达式 it -> {} 的快捷方式。

      3. WebFlux Security

      此外,你还可以使用 Lambda 表达式来配置 WebFlux security ,配置方式与上面基本相似。

      举个例子:

      @EnableWebFluxSecurity
      public class SecurityConfig {
          @Bean
          SecurityWebFilterChain编程 springSecurityFilterChain(ServerHttpSecurity http) {
              http
                  .authorizeExchange(exchanges ->
                      exchanges
                          .pathMatchers("/blog/**").permitAll()
                          .anyExchange().authenticated()
                  )
                  .httpBasic(withDefaults())
                  .formLogin(formLogin ->
                      formLogin
                          .loginPage("/login")
                  );
              return http.build();
          }
      }
      

      4. Lambda DSL的目标

      Lambda DSL 被开发出来,是为了完成以下的目的:

      • 自动缩进以提高配置的可读性。
      • 不再需要使用 .and() 方法来串联配置项。
      • Spring Security DSL 与其他 Spring DSLs (例如 Spring Integration 和 Spring Cloud Gateway ) 拥有相似的配置风格。

      到此这篇关于Spring Security使用Lambda DSL配置流程详解的文章就介绍到这了,更多相关Spring Security Lambda DSL内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

      0

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      关注公众号