Skip to content

Commit 4492e5b

Browse files
committed
Replace WebSecurityConfigurerAdapter with SecurityFilterChain in docs
Closes gh-10003
1 parent cbd87fa commit 4492e5b

File tree

22 files changed

+622
-443
lines changed

22 files changed

+622
-443
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[[logout-java-configuration]]
55
== Logout Java/Kotlin Configuration
66

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

1010
- Invalidating the HTTP Session
@@ -19,7 +19,7 @@ Similar to configuring login capabilities, however, you also have various option
1919
.Java
2020
[source,java,role="primary"]
2121
----
22-
protected void configure(HttpSecurity http) throws Exception {
22+
public SecurityFilterChain filterChain(HttpSecurity http) {
2323
http
2424
.logout(logout -> logout // <1>
2525
.logoutUrl("/my/logout") // <2>
@@ -36,7 +36,7 @@ protected void configure(HttpSecurity http) throws Exception {
3636
.Kotlin
3737
[source,kotlin,role="secondary"]
3838
-----
39-
override fun configure(http: HttpSecurity) {
39+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
4040
http {
4141
logout {
4242
logoutUrl = "/my/logout" // <1>
@@ -47,12 +47,12 @@ override fun configure(http: HttpSecurity) {
4747
deleteCookies(cookieNamesToClear) // <6>
4848
}
4949
}
50+
// ...
5051
}
5152
-----
5253
====
5354

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ A minimal, explicit configuration can be found below:
6262
[source,java,role="primary"]
6363
.Java
6464
----
65-
protected void configure(HttpSecurity http) {
65+
@Bean
66+
public SecurityFilterChain filterChain(HttpSecurity http) {
6667
http
6768
// ...
6869
.httpBasic(withDefaults());
70+
return http.build();
6971
}
7072
----
7173
@@ -81,11 +83,13 @@ protected void configure(HttpSecurity http) {
8183
[source,kotlin,role="secondary"]
8284
.Kotlin
8385
----
84-
fun configure(http: HttpSecurity) {
86+
@Bean
87+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
8588
http {
8689
// ...
8790
httpBasic { }
8891
}
92+
return http.build()
8993
}
9094
----
9195
====

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`.
@@ -57,11 +57,13 @@ DigestAuthenticationFilter digestAuthenticationFilter() {
5757
result.setAuthenticationEntryPoint(entryPoint());
5858
}
5959
60-
protected void configure(HttpSecurity http) throws Exception {
60+
@Bean
61+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6162
http
6263
// ...
6364
.exceptionHandling(e -> e.authenticationEntryPoint(authenticationEntryPoint()))
6465
.addFilterBefore(digestFilter());
66+
return http.build();
6567
}
6668
----
6769

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 @@ A minimal, explicit Java configuration can be found below:
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 configuration below demonstrates how to provide a custom log in 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 @@ This is achieved through 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, for e
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
@@ -105,12 +107,13 @@ Then add the following lines to your application context:
105107
.Java
106108
[source,java,role="primary"]
107109
----
108-
@Override
109-
protected void configure(HttpSecurity http) throws Exception {
110+
@Bean
111+
public SecurityFilterChain filterChain(HttpSecurity http) {
110112
http
111113
.sessionManagement(session -> session
112114
.maximumSessions(1)
113115
);
116+
return http.build();
114117
}
115118
----
116119
@@ -134,13 +137,14 @@ Often you would prefer to prevent a second login, in which case you can use
134137
.Java
135138
[source,java,role="primary"]
136139
----
137-
@Override
138-
protected void configure(HttpSecurity http) throws Exception {
140+
@Bean
141+
public SecurityFilterChain filterChain(HttpSecurity http) {
139142
http
140143
.sessionManagement(session -> session
141144
.maximumSessions(1)
142145
.maxSessionsPreventsLogin(true)
143146
);
147+
return http.build();
144148
}
145149
----
146150

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ The explicit configuration looks like:
3434
.Java
3535
[source,java,role="primary"]
3636
----
37-
protected void configure(HttpSecurity http) throws Exception {
37+
@Bean
38+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3839
http
3940
// ...
4041
.authorizeRequests(authorize -> authorize
4142
.anyRequest().authenticated()
4243
);
44+
return http.build();
4345
}
4446
----
4547
@@ -55,13 +57,15 @@ protected void configure(HttpSecurity http) throws Exception {
5557
.Kotlin
5658
[source,kotlin,role="secondary"]
5759
----
58-
fun configure(http: HttpSecurity) {
60+
@Bean
61+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
5962
http {
6063
// ...
6164
authorizeRequests {
6265
authorize(anyRequest, authenticated)
6366
}
6467
}
68+
return http.build()
6569
}
6670
----
6771
====
@@ -73,7 +77,8 @@ We can configure Spring Security to have different rules by adding more rules in
7377
.Java
7478
[source,java,role="primary"]
7579
----
76-
protected void configure(HttpSecurity http) throws Exception {
80+
@Bean
81+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
7782
http
7883
// ...
7984
.authorizeRequests(authorize -> authorize // <1>
@@ -82,6 +87,7 @@ protected void configure(HttpSecurity http) throws Exception {
8287
.mvcMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") // <4>
8388
.anyRequest().denyAll() // <5>
8489
);
90+
return http.build();
8591
}
8692
----
8793
@@ -104,7 +110,8 @@ protected void configure(HttpSecurity http) throws Exception {
104110
.Kotlin
105111
[source,kotlin,role="secondary"]
106112
----
107-
fun configure(http: HttpSecurity) {
113+
@Bean
114+
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
108115
http {
109116
authorizeRequests { // <1>
110117
authorize("/resources/**", permitAll) // <2>
@@ -116,6 +123,7 @@ fun configure(http: HttpSecurity) {
116123
authorize(anyRequest, denyAll) // <5>
117124
}
118125
}
126+
return http.build()
119127
}
120128
----
121129
====

0 commit comments

Comments
 (0)