Skip to content

Commit ad05eba

Browse files
avenaven
aven
authored and
aven
committed
WebSecurityConfigurerAdapter is deprecated
possible work arounds here https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter
1 parent c6f47f0 commit ad05eba

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

Diff for: click/README.adoc

+17-17
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,36 @@ WARNING: It's not a great idea to return a whole `OAuth2User` in an endpoint sin
7979
There's one final change you'll need to make.
8080

8181
This app will now work fine and authenticate as before, but it's still going to redirect before showing the page.
82-
To make the link visible, we also need to switch off the security on the home page by extending `WebSecurityConfigurerAdapter`:
82+
To make the link visible, we also need to switch off the security on the home page by registering a SecurityFilterChain bean:
8383

8484
.SocialApplication
8585
[source,java]
8686
----
8787
@SpringBootApplication
8888
@RestController
89-
public class SocialApplication extends WebSecurityConfigurerAdapter {
89+
public class SocialApplication {
9090
9191
// ...
9292
93-
@Override
94-
protected void configure(HttpSecurity http) throws Exception {
95-
// @formatter:off
96-
http
97-
.authorizeRequests(a -> a
98-
.antMatchers("/", "/error", "/webjars/**").permitAll()
99-
.anyRequest().authenticated()
100-
)
101-
.exceptionHandling(e -> e
102-
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
103-
)
104-
.oauth2Login();
105-
// @formatter:on
106-
}
93+
@Bean
94+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
95+
// @formatter:off
96+
http
97+
.authorizeRequests(a -> a
98+
.antMatchers("/", "/error", "/webjars/**").permitAll()
99+
.anyRequest().authenticated()
100+
)
101+
.exceptionHandling(e -> e
102+
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
103+
)
104+
.oauth2Login();
105+
return http.build();
106+
// @formatter:on
107+
}
107108
108109
}
109110
----
110111

111-
Spring Boot attaches special meaning to a `WebSecurityConfigurerAdapter` on the class annotated with `@SpringBootApplication`:
112112
It uses it to configure the security filter chain that carries the OAuth 2.0 authentication processor.
113113

114114
The above configuration indicates a whitelist of permitted endpoints, with every other endpoint requiring authentication.

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,38 @@
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.web.bind.annotation.GetMapping;
3031
import org.springframework.web.bind.annotation.RestController;
3132

3233
@SpringBootApplication
3334
@RestController
34-
public class SocialApplication extends WebSecurityConfigurerAdapter {
35+
public class SocialApplication {
3536

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

41-
@Override
42-
protected void configure(HttpSecurity http) throws Exception {
42+
@Bean
43+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4344
// @formatter:off
4445
http
45-
.authorizeRequests(a -> a
46-
.antMatchers("/", "/error", "/webjars/**").permitAll()
47-
.anyRequest().authenticated()
48-
)
49-
.exceptionHandling(e -> e
50-
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
51-
)
52-
.oauth2Login();
46+
.authorizeRequests(a -> a
47+
.antMatchers("/", "/error", "/webjars/**").permitAll()
48+
.anyRequest().authenticated()
49+
)
50+
.exceptionHandling(e -> e
51+
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
52+
)
53+
.oauth2Login();
54+
return http.build();
5355
// @formatter:on
5456
}
55-
5657
public static void main(String[] args) {
5758
SpringApplication.run(SocialApplication.class, args);
5859
}

0 commit comments

Comments
 (0)