|
| 1 | += Configuration Migrations |
| 2 | + |
| 3 | +The following steps relate to changes around how to configure `HttpSecurity`, `WebSecurity` and related components. |
| 4 | + |
| 5 | +== Use the Lambda DSL |
| 6 | + |
| 7 | +The Lambda DSL is present in Spring Security since version 5.2, and it allows HTTP security to be configured using lambdas. |
| 8 | + |
| 9 | +The prior configuration style will not be valid in Spring Security 7 where the usage of the Lambda DSL will be required. |
| 10 | + |
| 11 | +You may have seen this style of configuration in the Spring Security documentation or samples. |
| 12 | +Let us take a look at how a lambda configuration of HTTP security compares to the previous configuration style. |
| 13 | + |
| 14 | +==== |
| 15 | +[source,java] |
| 16 | +.Configuration using lambdas |
| 17 | +---- |
| 18 | +@Configuration |
| 19 | +@EnableWebSecurity |
| 20 | +public class SecurityConfig { |
| 21 | +
|
| 22 | + @Bean |
| 23 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 24 | + http |
| 25 | + .authorizeHttpRequests(authorize -> authorize |
| 26 | + .requestMatchers("/blog/**").permitAll() |
| 27 | + .anyRequest().authenticated() |
| 28 | + ) |
| 29 | + .formLogin(formLogin -> formLogin |
| 30 | + .loginPage("/login") |
| 31 | + .permitAll() |
| 32 | + ) |
| 33 | + .rememberMe(Customizer.withDefaults()); |
| 34 | +
|
| 35 | + return http.build(); |
| 36 | + } |
| 37 | +} |
| 38 | +---- |
| 39 | +==== |
| 40 | + |
| 41 | +==== |
| 42 | +[source,java] |
| 43 | +.Equivalent configuration without using lambdas |
| 44 | +---- |
| 45 | +@Configuration |
| 46 | +@EnableWebSecurity |
| 47 | +public class SecurityConfig { |
| 48 | +
|
| 49 | + @Bean |
| 50 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 51 | + http |
| 52 | + .authorizeHttpRequests() |
| 53 | + .requestMatchers("/blog/**").permitAll() |
| 54 | + .anyRequest().authenticated() |
| 55 | + .and() |
| 56 | + .formLogin() |
| 57 | + .loginPage("/login") |
| 58 | + .permitAll() |
| 59 | + .and() |
| 60 | + .rememberMe(); |
| 61 | +
|
| 62 | + return http.build(); |
| 63 | + } |
| 64 | +} |
| 65 | +---- |
| 66 | +==== |
| 67 | + |
| 68 | +=== Lambda DSL Configuration Tips |
| 69 | + |
| 70 | +When comparing the two samples above, you will notice some key differences: |
| 71 | + |
| 72 | +- In the Lambda DSL there is no need to chain configuration options using the `.and()` method. |
| 73 | +The `HttpSecurity` instance is automatically returned for further configuration after the call to the lambda method. |
| 74 | + |
| 75 | +- `Customizer.withDefaults()` enables a security feature using the defaults provided by Spring Security. |
| 76 | +This is a shortcut for the lambda expression `it -> {}`. |
| 77 | + |
| 78 | +=== WebFlux Security |
| 79 | + |
| 80 | +You may also configure WebFlux security using lambdas in a similar manner. |
| 81 | +Below is an example configuration using lambdas. |
| 82 | + |
| 83 | +==== |
| 84 | +[source,java] |
| 85 | +.WebFlux configuration using lambdas |
| 86 | +---- |
| 87 | +@Configuration |
| 88 | +@EnableWebFluxSecurity |
| 89 | +public class SecurityConfig { |
| 90 | +
|
| 91 | + @Bean |
| 92 | + public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { |
| 93 | + http |
| 94 | + .authorizeExchange(exchanges -> exchanges |
| 95 | + .pathMatchers("/blog/**").permitAll() |
| 96 | + .anyExchange().authenticated() |
| 97 | + ) |
| 98 | + .httpBasic(Customizer.withDefaults()) |
| 99 | + .formLogin(formLogin -> formLogin |
| 100 | + .loginPage("/login") |
| 101 | + ); |
| 102 | +
|
| 103 | + return http.build(); |
| 104 | + } |
| 105 | +
|
| 106 | +} |
| 107 | +---- |
| 108 | +==== |
| 109 | + |
| 110 | +=== Goals of the Lambda DSL |
| 111 | + |
| 112 | +The Lambda DSL was created to accomplish to following goals: |
| 113 | + |
| 114 | +- Automatic indentation makes the configuration more readable. |
| 115 | +- The is no need to chain configuration options using `.and()` |
| 116 | +- The Spring Security DSL has a similar configuration style to other Spring DSLs such as Spring Integration and Spring Cloud Gateway. |
0 commit comments