[JAVA-29010] Upgrade spring-reactive-security to Spring Boot 3 (#16153)

This commit is contained in:
Amit Pandey 2024-03-22 21:51:03 +05:30 committed by GitHub
parent 04b7d05a76
commit da41c860a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 33 additions and 22 deletions

View File

@ -24,7 +24,7 @@
<module>spring-reactive-client-2</module> <module>spring-reactive-client-2</module>
<module>spring-reactive-filters</module> <module>spring-reactive-filters</module>
<module>spring-reactive-oauth</module> <module>spring-reactive-oauth</module>
<module>spring-reactive-security</module> <!--<module>spring-reactive-security</module> Uncomment after the parent module gets upgraded to Boot 3-->
<module>spring-reactive-data-couchbase</module> <module>spring-reactive-data-couchbase</module>
<module>spring-reactive</module> <module>spring-reactive</module>
<module>spring-reactive-exceptions</module> <module>spring-reactive-exceptions</module>

View File

@ -10,9 +10,10 @@
<description>spring boot security sample project about new features</description> <description>spring boot security sample project about new features</description>
<parent> <parent>
<groupId>com.baeldung.spring.reactive</groupId> <groupId>com.baeldung</groupId>
<artifactId>spring-reactive-modules</artifactId> <artifactId>parent-boot-3</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent> </parent>
<dependencies> <dependencies>
@ -34,8 +35,8 @@
<version>${reactor-spring.version}</version> <version>${reactor-spring.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.json.bind</groupId> <groupId>jakarta.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId> <artifactId>jakarta.json.bind-api</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
@ -51,6 +52,7 @@
<dependency> <dependency>
<groupId>org.apache.johnzon</groupId> <groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-jsonb</artifactId> <artifactId>johnzon-jsonb</artifactId>
<version>${johnzon-jsonb.version}</version>
</dependency> </dependency>
<!-- utils --> <!-- utils -->
<dependency> <dependency>
@ -63,6 +65,11 @@
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>${jakarta.json-api.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
@ -117,6 +124,8 @@
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version> <geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
<reactor-test.version>3.1.6.RELEASE</reactor-test.version> <reactor-test.version>3.1.6.RELEASE</reactor-test.version>
<reactor.version>3.4.29</reactor.version> <reactor.version>3.4.29</reactor.version>
<jakarta.json-api.version>2.0.1</jakarta.json-api.version>
<johnzon-jsonb.version>2.0.0</johnzon-jsonb.version>
</properties> </properties>
</project> </project>

View File

@ -2,10 +2,13 @@ package com.baeldung.reactive.authresolver;
import java.util.Collections; import java.util.Collections;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.ReactiveAuthenticationManager; import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authentication.ReactiveAuthenticationManagerResolver; import org.springframework.security.authentication.ReactiveAuthenticationManagerResolver;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.SecurityWebFiltersOrder; import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.config.web.server.ServerHttpSecurity;
@ -24,12 +27,10 @@ public class CustomWebSecurityConfig {
@Bean @Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http return http
.authorizeExchange() .csrf(csrfSpec -> csrfSpec.disable())
.pathMatchers("/**") .authorizeExchange(auth -> auth.pathMatchers(HttpMethod.GET,"/**")
.authenticated() .authenticated())
.and() .httpBasic(httpBasicSpec -> httpBasicSpec.disable())
.httpBasic()
.disable()
.addFilterAfter(authenticationWebFilter(), SecurityWebFiltersOrder.REACTOR_CONTEXT) .addFilterAfter(authenticationWebFilter(), SecurityWebFiltersOrder.REACTOR_CONTEXT)
.build(); .build();
} }

View File

@ -27,7 +27,7 @@ public class CorsGlobalConfigApplication {
@Bean @Bean
public SecurityWebFilterChain corsGlobalSpringSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain corsGlobalSpringSecurityFilterChain(ServerHttpSecurity http) {
http.csrf().disable(); http.csrf(csrfSpec -> csrfSpec.disable());
return http.build(); return http.build();
} }
} }

View File

@ -27,7 +27,7 @@ public class CorsWebFilterApplication {
@Bean @Bean
public SecurityWebFilterChain corsWebfilterSpringSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain corsWebfilterSpringSecurityFilterChain(ServerHttpSecurity http) {
http.csrf().disable(); http.csrf(csrfSpec -> csrfSpec.disable());
return http.build(); return http.build();
} }

View File

@ -3,6 +3,7 @@ package com.baeldung.webflux;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService; import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
@ -27,15 +28,13 @@ public class EmployeeWebSecurityConfig {
@Bean @Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.csrf() http.csrf(csrfSpec -> csrfSpec.disable())
.disable() .authorizeExchange(auth -> auth
.authorizeExchange()
.pathMatchers(HttpMethod.POST, "/employees/update") .pathMatchers(HttpMethod.POST, "/employees/update")
.hasRole("ADMIN") .hasRole("ADMIN")
.pathMatchers("/**") .pathMatchers("/**")
.permitAll() .permitAll())
.and() .httpBasic(Customizer.withDefaults());
.httpBasic();
return http.build(); return http.build();
} }

View File

@ -1,19 +1,21 @@
package com.baeldung.reactive.authresolver; package com.baeldung.reactive.authresolver;
import java.util.Base64; import java.util.Base64;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = {AuthResolverApplication.class, AuthResolverController.class, CustomWebSecurityConfig.class})
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@AutoConfigureWebTestClient(timeout = "36000000")
public class AuthResolverIntegrationTest { public class AuthResolverIntegrationTest {
@Autowired @Autowired
private WebTestClient testClient; private WebTestClient testClient;