From ed2f49a894ea779bdfa3d37e6ed60d99f1ba6828 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Tue, 7 Jun 2022 09:33:06 +0100 Subject: [PATCH] Revert "Revert "[JAVA-8154] Code clean up for reactive security"" --- .../reactive/security/GreetController.java | 37 ------------------ .../reactive/security/GreetingController.java | 37 ++++++++++++++++++ ...GreetService.java => GreetingService.java} | 2 +- .../reactive/security/SecurityConfig.java | 39 +++++++++---------- .../security/SecurityIntegrationTest.java | 19 ++++++--- 5 files changed, 70 insertions(+), 64 deletions(-) delete mode 100644 spring-reactive/src/main/java/com/baeldung/reactive/security/GreetController.java create mode 100644 spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingController.java rename spring-reactive/src/main/java/com/baeldung/reactive/security/{GreetService.java => GreetingService.java} (91%) diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetController.java b/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetController.java deleted file mode 100644 index 99b79d88ea..0000000000 --- a/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetController.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.reactive.security; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Mono; - -import java.security.Principal; - -@RestController -public class GreetController { - - private GreetService greetService; - - public GreetController(GreetService greetService) { - this.greetService = greetService; - } - - @GetMapping("/") - public Mono greet(Mono principal) { - return principal - .map(Principal::getName) - .map(name -> String.format("Hello, %s", name)); - } - - @GetMapping("/admin") - public Mono greetAdmin(Mono principal) { - return principal - .map(Principal::getName) - .map(name -> String.format("Admin access: %s", name)); - } - - @GetMapping("/greetService") - public Mono greetService() { - return greetService.greet(); - } - -} diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingController.java b/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingController.java new file mode 100644 index 0000000000..10d6cf4df7 --- /dev/null +++ b/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingController.java @@ -0,0 +1,37 @@ +package com.baeldung.reactive.security; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +import java.security.Principal; + +@RestController +public class GreetingController { + + private final GreetingService greetingService; + + public GreetingController(GreetingService greetingService) { + this.greetingService = greetingService; + } + + @GetMapping("/") + public Mono greet(Mono principal) { + return principal + .map(Principal::getName) + .map(name -> String.format("Hello, %s", name)); + } + + @GetMapping("/admin") + public Mono greetAdmin(Mono principal) { + return principal + .map(Principal::getName) + .map(name -> String.format("Admin access: %s", name)); + } + + @GetMapping("/greetingService") + public Mono greetingService() { + return greetingService.greet(); + } + +} diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetService.java b/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingService.java similarity index 91% rename from spring-reactive/src/main/java/com/baeldung/reactive/security/GreetService.java rename to spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingService.java index 93df64bced..b512f12bae 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetService.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingService.java @@ -5,7 +5,7 @@ import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; @Service -public class GreetService { +public class GreetingService { @PreAuthorize("hasRole('ADMIN')") public Mono greet() { diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/security/SecurityConfig.java b/spring-reactive/src/main/java/com/baeldung/reactive/security/SecurityConfig.java index bb2f2d50e1..67e54ad26a 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/security/SecurityConfig.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/security/SecurityConfig.java @@ -16,40 +16,37 @@ import org.springframework.security.web.server.SecurityWebFilterChain; public class SecurityConfig { @Bean - public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) { + public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) { return http.authorizeExchange() - .pathMatchers("/admin") - .hasAuthority("ROLE_ADMIN") - .anyExchange() - .authenticated() - .and() - .formLogin() - .and() - .csrf() - .disable() - .build(); + .pathMatchers("/admin").hasAuthority("ROLE_ADMIN") + .anyExchange().authenticated() + .and() + .formLogin() + .and() + .csrf().disable() + .build(); } @Bean public MapReactiveUserDetailsService userDetailsService() { UserDetails user = User - .withUsername("user") - .password(passwordEncoder().encode("password")) - .roles("USER") - .build(); + .withUsername("user") + .password(passwordEncoder().encode("password")) + .roles("USER") + .build(); UserDetails admin = User - .withUsername("admin") - .password(passwordEncoder().encode("password")) - .roles("ADMIN") - .build(); + .withUsername("admin") + .password(passwordEncoder().encode("password")) + .roles("ADMIN") + .build(); return new MapReactiveUserDetailsService(user, admin); } - + @Bean public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); + return new BCryptPasswordEncoder(); } } diff --git a/spring-reactive/src/test/java/com/baeldung/reactive/security/SecurityIntegrationTest.java b/spring-reactive/src/test/java/com/baeldung/reactive/security/SecurityIntegrationTest.java index 06644fbf77..0ef828df5a 100644 --- a/spring-reactive/src/test/java/com/baeldung/reactive/security/SecurityIntegrationTest.java +++ b/spring-reactive/src/test/java/com/baeldung/reactive/security/SecurityIntegrationTest.java @@ -15,23 +15,32 @@ import org.springframework.test.web.reactive.server.WebTestClient; public class SecurityIntegrationTest { @Autowired - ApplicationContext context; + private ApplicationContext context; - private WebTestClient rest; + private WebTestClient webTestClient; @BeforeEach public void setup() { - this.rest = WebTestClient.bindToApplicationContext(this.context).configureClient().build(); + webTestClient = WebTestClient.bindToApplicationContext(context) + .configureClient() + .build(); } @Test public void whenNoCredentials_thenRedirectToLogin() { - this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection(); + webTestClient.get() + .uri("/") + .exchange() + .expectStatus().is3xxRedirection(); } @Test @WithMockUser public void whenHasCredentials_thenSeesGreeting() { - this.rest.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello, user"); + webTestClient.get() + .uri("/") + .exchange() + .expectStatus().isOk() + .expectBody(String.class).isEqualTo("Hello, user"); } }