Merge pull request #14709 from hmdrzsharifi/bael-6473
Bael 6473: Upgrade article https://www.baeldung.com/spring-security-5-reactive
This commit is contained in:
commit
c29ced2b9b
@ -6,9 +6,10 @@
|
|||||||
<artifactId>spring-reactive</artifactId>
|
<artifactId>spring-reactive</artifactId>
|
||||||
|
|
||||||
<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>
|
||||||
@ -90,6 +91,18 @@
|
|||||||
</profile>
|
</profile>
|
||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<reactor.version>3.4.16</reactor.version>
|
<reactor.version>3.4.16</reactor.version>
|
||||||
<reactor-kafka.version>1.3.10</reactor-kafka.version>
|
<reactor-kafka.version>1.3.10</reactor-kafka.version>
|
||||||
|
@ -24,10 +24,10 @@ public class ConsumerDebuggingApplication {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityWebFilterChain debuggingConsumerSpringSecurityFilterChain(ServerHttpSecurity http) {
|
public SecurityWebFilterChain debuggingConsumerSpringSecurityFilterChain(ServerHttpSecurity http) {
|
||||||
http.authorizeExchange()
|
http.authorizeExchange(exchanges -> exchanges
|
||||||
.anyExchange()
|
.anyExchange()
|
||||||
.permitAll();
|
.permitAll());
|
||||||
http.csrf().disable();
|
http.csrf(csrf -> csrf.disable());
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,9 @@ public class ServerDebuggingApplication {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityWebFilterChain debuggingServerSpringSecurityFilterChain(ServerHttpSecurity http) {
|
public SecurityWebFilterChain debuggingServerSpringSecurityFilterChain(ServerHttpSecurity http) {
|
||||||
http.authorizeExchange()
|
http.authorizeExchange(exchanges -> exchanges
|
||||||
.anyExchange()
|
.anyExchange()
|
||||||
.permitAll();
|
.permitAll());
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.reactive.security;
|
package com.baeldung.reactive.security;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
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.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;
|
||||||
@ -12,18 +13,19 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||||
|
|
||||||
@EnableWebFluxSecurity
|
@EnableWebFluxSecurity
|
||||||
|
@Configuration
|
||||||
@EnableReactiveMethodSecurity
|
@EnableReactiveMethodSecurity
|
||||||
public class SecurityConfig {
|
public class SecurityConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
|
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
|
||||||
return http.authorizeExchange()
|
return http
|
||||||
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
|
.authorizeExchange(exchanges -> exchanges
|
||||||
.anyExchange().authenticated()
|
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
|
||||||
.and()
|
.anyExchange().authenticated())
|
||||||
.formLogin()
|
.formLogin(formLogin -> formLogin
|
||||||
.and()
|
.loginPage("/login"))
|
||||||
.csrf().disable()
|
.csrf(csrf -> csrf.disable())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,11 +13,11 @@ import reactor.netty.http.server.HttpServer;
|
|||||||
|
|
||||||
@ComponentScan(basePackages = {"com.baeldung.reactive.security"})
|
@ComponentScan(basePackages = {"com.baeldung.reactive.security"})
|
||||||
@EnableWebFlux
|
@EnableWebFlux
|
||||||
public class SpringSecurity5Application {
|
public class SpringSecurity6Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try (AnnotationConfigApplicationContext context =
|
try (AnnotationConfigApplicationContext context =
|
||||||
new AnnotationConfigApplicationContext(SpringSecurity5Application.class)) {
|
new AnnotationConfigApplicationContext(SpringSecurity6Application.class)) {
|
||||||
context.getBean(DisposableServer.class).onDispose().block();
|
context.getBean(DisposableServer.class).onDispose().block();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,9 +16,8 @@ public class WebClientApplication {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
|
public SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
|
||||||
http.csrf().disable()
|
http.csrf(csrf -> csrf.disable())
|
||||||
.authorizeExchange()
|
.authorizeExchange(exchanges -> exchanges.anyExchange().permitAll());
|
||||||
.anyExchange().permitAll();
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.baeldung.reactive.webflux.annotation;
|
package com.baeldung.reactive.webflux.annotation;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
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;
|
||||||
@ -12,6 +14,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||||
|
|
||||||
@EnableWebFluxSecurity
|
@EnableWebFluxSecurity
|
||||||
|
@Configuration
|
||||||
public class EmployeeWebSecurityConfig {
|
public class EmployeeWebSecurityConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -27,12 +30,11 @@ public class EmployeeWebSecurityConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||||
http
|
http
|
||||||
.csrf().disable()
|
.csrf(csrf -> csrf.disable())
|
||||||
.authorizeExchange()
|
.authorizeExchange(exchanges -> exchanges
|
||||||
.pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN")
|
.pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN")
|
||||||
.pathMatchers("/**").permitAll()
|
.pathMatchers("/**").permitAll())
|
||||||
.and()
|
.httpBasic(Customizer.withDefaults());
|
||||||
.httpBasic();
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ import org.springframework.context.ApplicationContext;
|
|||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
@SpringBootTest(classes = SpringSecurity5Application.class)
|
@SpringBootTest(classes = SpringSecurity6Application.class)
|
||||||
class SecurityIntegrationTest {
|
class SecurityIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -7,7 +7,7 @@ import io.netty.handler.timeout.WriteTimeoutHandler;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
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.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
import org.springframework.boot.web.server.LocalServerPort;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
import org.springframework.core.codec.CodecException;
|
import org.springframework.core.codec.CodecException;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
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.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
import org.springframework.boot.web.server.LocalServerPort;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
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.boot.test.context.SpringBootTest.WebEnvironment;
|
||||||
import org.springframework.boot.web.server.LocalServerPort;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
|
@ -14,10 +14,7 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
|
|||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@WebFluxTest
|
@WebFluxTest
|
||||||
class WebClientRequestsWithParametersUnitTest {
|
class WebClientRequestsWithParametersUnitTest {
|
||||||
@ -51,6 +48,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.uri("/products")
|
.uri("/products")
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products");
|
verifyCalledUrl("/products");
|
||||||
@ -64,6 +62,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build(2))
|
.build(2))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/2");
|
verifyCalledUrl("/products/2");
|
||||||
@ -77,6 +76,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build(2, 13))
|
.build(2, 13))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/2/attributes/13");
|
verifyCalledUrl("/products/2/attributes/13");
|
||||||
@ -93,6 +93,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019");
|
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019");
|
||||||
@ -109,6 +110,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build("AndroidPhone", "black", "13/04/2019"))
|
.build("AndroidPhone", "black", "13/04/2019"))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13%2F04%2F2019");
|
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13%2F04%2F2019");
|
||||||
@ -123,6 +125,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/?tag%5B%5D=Snapdragon&tag%5B%5D=NFC");
|
verifyCalledUrl("/products/?tag%5B%5D=Snapdragon&tag%5B%5D=NFC");
|
||||||
@ -137,6 +140,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/?category=Phones&category=Tablets");
|
verifyCalledUrl("/products/?category=Phones&category=Tablets");
|
||||||
@ -151,6 +155,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/?category=Phones,Tablets");
|
verifyCalledUrl("/products/?category=Phones,Tablets");
|
||||||
@ -176,6 +181,7 @@ class WebClientRequestsWithParametersUnitTest {
|
|||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
|
.onErrorResume(e -> Mono.empty())
|
||||||
.block();
|
.block();
|
||||||
|
|
||||||
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019");
|
verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user