Skip to content

Commit 4aa19a7

Browse files
avenaven
aven
authored and
aven
committed
WebSecurityConfigurerAdapter is deprecated
registered filterChain bean and added logout/csrf logic
1 parent ad05eba commit 4aa19a7

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

Diff for: logout/README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ Now we can switch over to the server side to implement that endpoint.
4040
== Adding a Logout Endpoint
4141

4242
Spring Security has built in support for a `/logout` endpoint which will do the right thing for us (clear the session and invalidate the cookie).
43-
To configure the endpoint we simply extend the existing `configure()` method in our `WebSecurityConfigurerAdapter`:
43+
To configure the endpoint we simply extend the existing `filterChain()` method of our bean:
4444

4545
.SocialApplication.java
4646
[source,java]
4747
----
48-
@Override
49-
protected void configure(HttpSecurity http) throws Exception {
48+
@Bean
49+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
5050
// @formatter:off
5151
http
5252
// ... existing code here
@@ -66,13 +66,13 @@ For instance, in Angular, the front end would like the server to send it a cooki
6666
We can implement the same behaviour with our simple jQuery client, and then the server-side changes will work with other front end implementations with no or very few changes.
6767
To teach Spring Security about this we need to add a filter that creates the cookie.
6868

69-
In the `WebSecurityConfigurerAdapter` we do the following:
69+
In the `filterChain` bean we do the following:
7070

7171
.SocialApplication.java
7272
[source,java]
7373
----
74-
@Override
75-
protected void configure(HttpSecurity http) throws Exception {
74+
@Bean
75+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
7676
// @formatter:off
7777
http
7878
// ... existing code here

Diff for: logout/src/main/java/com/example/SocialApplication.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@
2222
import org.springframework.boot.autoconfigure.SpringBootApplication;
2323
import org.springframework.http.HttpStatus;
2424
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
25-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
25+
import org.springframework.context.annotation.Bean;
2626
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2727
import org.springframework.security.oauth2.core.user.OAuth2User;
28+
import org.springframework.security.web.SecurityFilterChain;
2829
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
2930
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
3031
import org.springframework.web.bind.annotation.RequestMapping;
3132
import org.springframework.web.bind.annotation.RestController;
3233

3334
@SpringBootApplication
3435
@RestController
35-
public class SocialApplication extends WebSecurityConfigurerAdapter {
36+
public class SocialApplication {
3637

3738
@RequestMapping("/user")
3839
public Map<String, Object> user(@AuthenticationPrincipal OAuth2User principal) {
3940
return Collections.singletonMap("name", principal.getAttribute("name"));
4041
}
4142

42-
@Override
43-
protected void configure(HttpSecurity http) throws Exception {
43+
@Bean
44+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4445
// @formatter:off
4546
http
4647
.authorizeRequests(a -> a
@@ -57,6 +58,7 @@ protected void configure(HttpSecurity http) throws Exception {
5758
.logoutSuccessUrl("/").permitAll()
5859
)
5960
.oauth2Login();
61+
return http.build();
6062
// @formatter:on
6163
}
6264

0 commit comments

Comments
 (0)