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

作者:自牧君 时间:2021-12-23 19:20:32 

1. 概述

在 Spring Security 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/**").permitAll()
                   .anyRequest().authenticated()
           )
           .formLogin(formLogin ->
               formLogin
                   .loginPage("/login")
                   .permitAll()
           )
           .rememberMe(withDefaults());
   }
}

等效的旧配置风格

@EnableWebSecurity
public 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 ) 拥有相似的配置风格。

来源:https://blog.csdn.net/Sihang_Xie/article/details/128748845

标签:Spring,Security,Lambda,配置
0
投稿

猜你喜欢

  • Flutter开发Widgets 之 PageView使用示例

    2023-06-24 13:23:34
  • IntelliJ IDEA 如何配置git的操作方法

    2021-12-28 11:24:44
  • java selenium教程环境搭建基于Maven

    2023-11-27 01:35:38
  • Android巧用XListView实现万能下拉刷新控件

    2023-07-25 00:33:03
  • java连接sql server 2008数据库代码

    2023-05-27 10:40:01
  • Spring Boot配置AOP打印日志的全过程

    2023-08-07 12:56:38
  • springboot使用Mybatis-plus分页插件的案例详解

    2023-10-27 13:47:43
  • JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解

    2023-11-14 00:27:55
  • Java新手环境搭建 JDK8安装配置教程

    2023-11-25 17:23:10
  • Spring JPA联表查询之注解属性详解

    2021-11-04 14:19:04
  • 详解c#与python的交互方式

    2023-12-13 09:16:11
  • springboot结合vue实现增删改查及分页查询

    2023-11-24 15:53:44
  • 如何用注解的方式实现Mybatis插入数据时返回自增的主键Id

    2022-02-05 09:29:55
  • C#控制图像旋转和翻转的方法

    2023-11-26 08:25:08
  • 源码解析JDK 1.8 中的 Map.merge()

    2023-11-16 23:49:25
  • 谈谈Java中整数类型(short int long)的存储方式

    2023-01-01 08:24:33
  • 详解java中BigDecimal精度问题

    2021-08-17 10:24:59
  • Java的Spring框架中AOP项目的一般配置和部署教程

    2021-07-26 16:49:25
  • 详解基于java的Socket聊天程序——初始设计(附demo)

    2023-02-01 06:46:21
  • 详解Spring框架入门

    2023-08-14 12:56:14
  • asp之家 软件编程 m.aspxhome.com