Skip to content

Commit 4142f06

Browse files
committed
Replace WebSecurityConfigurerAdapter with SecurityFilterChain in docs
Closes gh-10003
1 parent 8461654 commit 4142f06

File tree

22 files changed

+622
-444
lines changed

22 files changed

+622
-444
lines changed

docs/modules/ROOT/pages/servlet/authentication/logout.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This section covers how to customize the handling of logouts.
66
[[logout-java-configuration]]
77
== Logout Java/Kotlin Configuration
88

9-
When using the `{security-api-url}org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.html[WebSecurityConfigurerAdapter]`, logout capabilities are automatically applied.
9+
When using the `{security-api-url}org/springframework/security/config/annotation/web/builders/HttpSecurity.html[HttpSecurity]` bean, logout capabilities are automatically applied.
1010
The default is that accessing the URL `/logout` logs the user out by:
1111

1212
- Invalidating the HTTP Session
@@ -21,7 +21,7 @@ Similar to configuring login capabilities, however, you also have various option
2121
.Java
2222
[source,java,role="primary"]
2323
----
24-
protected void configure(HttpSecurity http) throws Exception {
24+
public SecurityFilterChain filterChain(HttpSecurity http) {
2525
http
2626
.logout(logout -> logout // <1>
2727
.logoutUrl("/my/logout") // <2>
@@ -38,7 +38,7 @@ protected void configure(HttpSecurity http) throws Exception {
3838
.Kotlin
3939
[source,kotlin,role="secondary"]
4040
-----
41-
override fun configure(http: HttpSecurity) {
41+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
4242
http {
4343
logout {
4444
logoutUrl = "/my/logout" // <1>
@@ -49,12 +49,12 @@ override fun configure(http: HttpSecurity) {
4949
deleteCookies(cookieNamesToClear) // <6>
5050
}
5151
}
52+
// ...
5253
}
5354
-----
5455
====
5556

5657
<1> Provides logout support.
57-
This is automatically applied when using `WebSecurityConfigurerAdapter`.
5858
<2> The URL that triggers log out to occur (the default is `/logout`).
5959
If CSRF protection is enabled (the default), the request must also be a POST.
6060
For more information, see {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#logoutUrl-java.lang.String-[`logoutUrl(java.lang.String logoutUrl)`].

docs/modules/ROOT/pages/servlet/authentication/passwords/basic.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ The following example shows a minimal, explicit configuration:
6464
[source,java,role="primary"]
6565
.Java
6666
----
67-
protected void configure(HttpSecurity http) {
67+
@Bean
68+
public SecurityFilterChain filterChain(HttpSecurity http) {
6869
http
6970
// ...
7071
.httpBasic(withDefaults());
72+
return http.build();
7173
}
7274
----
7375
@@ -83,11 +85,13 @@ protected void configure(HttpSecurity http) {
8385
[source,kotlin,role="secondary"]
8486
.Kotlin
8587
----
86-
fun configure(http: HttpSecurity) {
88+
@Bean
89+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
8790
http {
8891
// ...
8992
httpBasic { }
9093
}
94+
return http.build()
9195
}
9296
----
9397
====

docs/modules/ROOT/pages/servlet/authentication/passwords/digest.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[[servlet-authentication-digest]]
1+
**[[**servlet-authentication-digest]]
22
= Digest Authentication
33

44
This section provides details on how Spring Security provides support for https://tools.ietf.org/html/rfc2617[Digest Authentication], which is provided `DigestAuthenticationFilter`.
@@ -58,11 +58,13 @@ DigestAuthenticationFilter digestAuthenticationFilter() {
5858
result.setAuthenticationEntryPoint(entryPoint());
5959
}
6060
61-
protected void configure(HttpSecurity http) throws Exception {
61+
@Bean
62+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6263
http
6364
// ...
6465
.exceptionHandling(e -> e.authenticationEntryPoint(authenticationEntryPoint()))
6566
.addFilterBefore(digestFilter());
67+
return http.build();
6668
}
6769
----
6870

docs/modules/ROOT/pages/servlet/authentication/passwords/form.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ The following example shows a minimal, explicit Java configuration:
7171
.Java
7272
[source,java,role="primary"]
7373
----
74-
protected void configure(HttpSecurity http) {
74+
public SecurityFilterChain filterChain(HttpSecurity http) {
7575
http
76-
// ...
7776
.formLogin(withDefaults());
77+
// ...
7878
}
7979
----
8080
@@ -90,11 +90,11 @@ protected void configure(HttpSecurity http) {
9090
.Kotlin
9191
[source,kotlin,role="secondary"]
9292
----
93-
fun configure(http: HttpSecurity) {
93+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
9494
http {
95-
// ...
9695
formLogin { }
9796
}
97+
// ...
9898
}
9999
----
100100
====
@@ -110,13 +110,13 @@ The following configuration demonstrates how to provide a custom login form.
110110
.Java
111111
[source,java,role="primary"]
112112
----
113-
protected void configure(HttpSecurity http) throws Exception {
113+
public SecurityFilterChain filterChain(HttpSecurity http) {
114114
http
115-
// ...
116115
.formLogin(form -> form
117116
.loginPage("/login")
118117
.permitAll()
119118
);
119+
// ...
120120
}
121121
----
122122
@@ -133,14 +133,14 @@ protected void configure(HttpSecurity http) throws Exception {
133133
.Kotlin
134134
[source,kotlin,role="secondary"]
135135
----
136-
fun configure(http: HttpSecurity) {
136+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
137137
http {
138-
// ...
139138
formLogin {
140139
loginPage = "/login"
141140
permitAll()
142141
}
143142
}
143+
// ...
144144
}
145145
----
146146
====

docs/modules/ROOT/pages/servlet/authentication/session-management.adoc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ To do so, configure the `session-management` element:
1111
.Java
1212
[source,java,role="primary"]
1313
----
14-
@Override
15-
protected void configure(HttpSecurity http) throws Exception{
14+
@Bean
15+
public SecurityFilterChain filterChain(HttpSecurity http) {
1616
http
1717
.sessionManagement(session -> session
1818
.invalidSessionUrl("/invalidSession.htm")
1919
);
20+
return http.build();
2021
}
2122
----
2223
@@ -38,12 +39,13 @@ You may be able to explicitly delete the `JSESSIONID` cookie on logging out -- f
3839
.Java
3940
[source,java,role="primary"]
4041
----
41-
@Override
42-
protected void configure(HttpSecurity http) throws Exception{
42+
@Bean
43+
public SecurityFilterChain filterChain(HttpSecurity http) {
4344
http
4445
.logout(logout -> logout
4546
.deleteCookies("JSESSIONID")
4647
);
48+
return http.build();
4749
}
4850
----
4951
@@ -107,12 +109,13 @@ Then add the following lines to your application context:
107109
.Java
108110
[source,java,role="primary"]
109111
----
110-
@Override
111-
protected void configure(HttpSecurity http) throws Exception {
112+
@Bean
113+
public SecurityFilterChain filterChain(HttpSecurity http) {
112114
http
113115
.sessionManagement(session -> session
114116
.maximumSessions(1)
115117
);
118+
return http.build();
116119
}
117120
----
118121
@@ -135,13 +138,14 @@ Often, you would prefer to prevent a second login. In that case, you can use:
135138
.Java
136139
[source,java,role="primary"]
137140
----
138-
@Override
139-
protected void configure(HttpSecurity http) throws Exception {
141+
@Bean
142+
public SecurityFilterChain filterChain(HttpSecurity http) {
140143
http
141144
.sessionManagement(session -> session
142145
.maximumSessions(1)
143146
.maxSessionsPreventsLogin(true)
144147
);
148+
return http.build();
145149
}
146150
----
147151

docs/modules/ROOT/pages/servlet/authorization/authorize-requests.adoc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ The following listing shows the explicit configuration:
3737
.Java
3838
[source,java,role="primary"]
3939
----
40-
protected void configure(HttpSecurity http) throws Exception {
40+
@Bean
41+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4142
http
4243
// ...
4344
.authorizeRequests(authorize -> authorize
4445
.anyRequest().authenticated()
4546
);
47+
return http.build();
4648
}
4749
----
4850
@@ -58,13 +60,15 @@ protected void configure(HttpSecurity http) throws Exception {
5860
.Kotlin
5961
[source,kotlin,role="secondary"]
6062
----
61-
fun configure(http: HttpSecurity) {
63+
@Bean
64+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
6265
http {
6366
// ...
6467
authorizeRequests {
6568
authorize(anyRequest, authenticated)
6669
}
6770
}
71+
return http.build()
6872
}
6973
----
7074
====
@@ -76,7 +80,8 @@ We can configure Spring Security to have different rules by adding more rules in
7680
.Java
7781
[source,java,role="primary"]
7882
----
79-
protected void configure(HttpSecurity http) throws Exception {
83+
@Bean
84+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
8085
http
8186
// ...
8287
.authorizeRequests(authorize -> authorize // <1>
@@ -85,6 +90,7 @@ protected void configure(HttpSecurity http) throws Exception {
8590
.mvcMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") // <4>
8691
.anyRequest().denyAll() // <5>
8792
);
93+
return http.build();
8894
}
8995
----
9096
@@ -107,7 +113,8 @@ protected void configure(HttpSecurity http) throws Exception {
107113
.Kotlin
108114
[source,kotlin,role="secondary"]
109115
----
110-
fun configure(http: HttpSecurity) {
116+
@Bean
117+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
111118
http {
112119
authorizeRequests { // <1>
113120
authorize("/resources/**", permitAll) // <2>
@@ -119,6 +126,7 @@ fun configure(http: HttpSecurity) {
119126
authorize(anyRequest, denyAll) // <5>
120127
}
121128
}
129+
return http.build()
122130
}
123131
----
124132
<1> There are multiple authorization rules specified.

0 commit comments

Comments
 (0)