Skip to content

Commit 2b05d5d

Browse files
Document in the reference how to migrate to lambda
Closes gh-12628
1 parent 9f6a879 commit 2b05d5d

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

docs/modules/ROOT/nav.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* xref:prerequisites.adoc[Prerequisites]
33
* xref:community.adoc[Community]
44
* xref:whats-new.adoc[What's New]
5+
* xref:migration-7/index.adoc[Preparing for 7.0]
6+
** xref:migration-7/configuration.adoc[Configuration]
57
* xref:migration/index.adoc[Migrating to 6.0]
68
** xref:migration/servlet/index.adoc[Servlet Migrations]
79
*** xref:migration/servlet/session-management.adoc[Session Management]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[[preparing]]
2+
= Preparing for 7.0
3+
4+
While Spring Security 7.0 does not have a release date yet, it is important to start preparing for it now.
5+
6+
This preparation guide is designed to summarize the biggest changes in Spring Security 7.0 and provide steps to prepare for them.
7+
8+
It is important to keep your application up to date with the latest Spring Security 6 and Spring Boot 3 releases.

0 commit comments

Comments
 (0)