Skip to content

Commit 1a5b388

Browse files
Updated Spring boot version to 3.2.0
1 parent 6588b8b commit 1a5b388

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

src/main/java/com/ksoot/problem/demo/config/SecurityConfiguration.java

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
88
import org.springframework.lang.Nullable;
9+
import org.springframework.security.config.Customizer;
910
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
1011
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
1112
import org.springframework.security.config.web.server.ServerHttpSecurity;
@@ -25,50 +26,58 @@
2526
@EnableReactiveMethodSecurity // Allow method annotations like @PreAuthorize
2627
class SecurityConfiguration {
2728

28-
private final String[] SWAGGER_URLS = new String[]{"/swagger-resources/**", "/swagger-ui/**", "/swagger-ui.*", "/v3/api-docs", "/v3/api-docs/**", "/webjars/**"};
29+
private final String[] SWAGGER_URLS = new String[]{"/swagger-resources/**", "/swagger-ui/**", "/swagger-ui.*", "/v3/api-docs", "/v3/api-docs/**", "/webjars/**"};
2930

30-
private boolean actuatorBypassSecurity = true;
31+
private boolean actuatorBypassSecurity = true;
3132

32-
private final ServerAuthenticationEntryPoint authenticationEntryPoint;
33+
private final ServerAuthenticationEntryPoint authenticationEntryPoint;
3334

34-
private final ServerAccessDeniedHandler accessDeniedHandler;
35+
private final ServerAccessDeniedHandler accessDeniedHandler;
3536

36-
private final ActuatorEndpointProperties actuatorEndpointProperties;
37+
private final ActuatorEndpointProperties actuatorEndpointProperties;
3738

38-
SecurityConfiguration(@Nullable final ServerAuthenticationEntryPoint authenticationEntryPoint,
39-
@Nullable final ServerAccessDeniedHandler accessDeniedHandler,
40-
@Nullable final ActuatorEndpointProperties actuatorEndpointProperties) {
41-
this.authenticationEntryPoint = authenticationEntryPoint;
42-
this.accessDeniedHandler = accessDeniedHandler;
43-
this.actuatorEndpointProperties = actuatorEndpointProperties;
44-
}
39+
SecurityConfiguration(@Nullable final ServerAuthenticationEntryPoint authenticationEntryPoint,
40+
@Nullable final ServerAccessDeniedHandler accessDeniedHandler,
41+
@Nullable final ActuatorEndpointProperties actuatorEndpointProperties) {
42+
this.authenticationEntryPoint = authenticationEntryPoint;
43+
this.accessDeniedHandler = accessDeniedHandler;
44+
this.actuatorEndpointProperties = actuatorEndpointProperties;
45+
}
4546

46-
@Bean
47-
SecurityWebFilterChain securityWebFilterChain(final ServerHttpSecurity http) {
48-
http.csrf().disable().authorizeExchange()
49-
.pathMatchers(this.actuatorBypassSecurity && this.actuatorEndpointProperties != null
50-
? ArrayUtils.addAll(SWAGGER_URLS, this.actuatorEndpointProperties.getPaths())
51-
: SWAGGER_URLS).permitAll()
52-
.pathMatchers("/problems/**").permitAll()
53-
.anyExchange().authenticated()
54-
.and().oauth2ResourceServer()
55-
.jwt().jwtAuthenticationConverter(this.jwtAuthenticationConverter())
56-
.jwtDecoder(new JwtStringDecoder());
47+
@Bean
48+
SecurityWebFilterChain securityWebFilterChain(final ServerHttpSecurity http) {
49+
http.csrf(ServerHttpSecurity.CsrfSpec::disable)
50+
.authorizeExchange((exchanges) -> exchanges
51+
.pathMatchers(this.actuatorBypassSecurity && this.actuatorEndpointProperties != null
52+
? ArrayUtils.addAll(SWAGGER_URLS, this.actuatorEndpointProperties.getPaths())
53+
: SWAGGER_URLS).permitAll()
54+
.pathMatchers("/problems/**").permitAll()
55+
.anyExchange().authenticated()
56+
).oauth2ResourceServer(
57+
resourceServerCustomizer ->
58+
resourceServerCustomizer.jwt(
59+
jwtCustomizer ->
60+
jwtCustomizer
61+
.jwtAuthenticationConverter(this.jwtAuthenticationConverter())
62+
.jwtDecoder(new JwtStringDecoder())));
5763

58-
if (this.authenticationEntryPoint != null) {
59-
http.exceptionHandling().authenticationEntryPoint(this.authenticationEntryPoint);
60-
}
61-
if (this.accessDeniedHandler != null) {
62-
http.exceptionHandling().accessDeniedHandler(this.accessDeniedHandler);
64+
if (this.authenticationEntryPoint != null) {
65+
http.exceptionHandling(
66+
exceptionHandling ->
67+
exceptionHandling.authenticationEntryPoint(this.authenticationEntryPoint));
68+
}
69+
if (this.accessDeniedHandler != null) {
70+
http.exceptionHandling(
71+
exceptionHandling -> exceptionHandling.accessDeniedHandler(this.accessDeniedHandler));
72+
}
73+
return http.build();
6374
}
64-
return http.build();
65-
}
6675

67-
private ReactiveJwtAuthenticationConverter jwtAuthenticationConverter() {
68-
ReactiveJwtAuthenticationConverter jwtAuthenticationConverter = new ReactiveJwtAuthenticationConverter();
69-
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(
70-
new ReactiveJwtGrantedAuthoritiesConverterAdapter(JwtUtils.jwtGrantedAuthoritiesConverter()));
71-
jwtAuthenticationConverter.setPrincipalClaimName(JwtUtils.PRINCIPLE_NAME_CLAIM_ID);
72-
return jwtAuthenticationConverter;
73-
}
76+
private ReactiveJwtAuthenticationConverter jwtAuthenticationConverter() {
77+
ReactiveJwtAuthenticationConverter jwtAuthenticationConverter = new ReactiveJwtAuthenticationConverter();
78+
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(
79+
new ReactiveJwtGrantedAuthoritiesConverterAdapter(JwtUtils.jwtGrantedAuthoritiesConverter()));
80+
jwtAuthenticationConverter.setPrincipalClaimName(JwtUtils.PRINCIPLE_NAME_CLAIM_ID);
81+
return jwtAuthenticationConverter;
82+
}
7483
}

0 commit comments

Comments
 (0)