Skip to content

Commit 4abe624

Browse files
authored
Merge pull request #12265 from hkhan/JAVA-8154-webflux-security
[JAVA-8154] Code clean up for reactive security
2 parents bacfc3e + 2a85438 commit 4abe624

5 files changed

Lines changed: 70 additions & 64 deletions

File tree

spring-reactive/src/main/java/com/baeldung/reactive/security/GreetController.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.reactive.security;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
import reactor.core.publisher.Mono;
6+
7+
import java.security.Principal;
8+
9+
@RestController
10+
public class GreetingController {
11+
12+
private final GreetingService greetingService;
13+
14+
public GreetingController(GreetingService greetingService) {
15+
this.greetingService = greetingService;
16+
}
17+
18+
@GetMapping("/")
19+
public Mono<String> greet(Mono<Principal> principal) {
20+
return principal
21+
.map(Principal::getName)
22+
.map(name -> String.format("Hello, %s", name));
23+
}
24+
25+
@GetMapping("/admin")
26+
public Mono<String> greetAdmin(Mono<Principal> principal) {
27+
return principal
28+
.map(Principal::getName)
29+
.map(name -> String.format("Admin access: %s", name));
30+
}
31+
32+
@GetMapping("/greetingService")
33+
public Mono<String> greetingService() {
34+
return greetingService.greet();
35+
}
36+
37+
}

spring-reactive/src/main/java/com/baeldung/reactive/security/GreetService.java renamed to spring-reactive/src/main/java/com/baeldung/reactive/security/GreetingService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import reactor.core.publisher.Mono;
66

77
@Service
8-
public class GreetService {
8+
public class GreetingService {
99

1010
@PreAuthorize("hasRole('ADMIN')")
1111
public Mono<String> greet() {

spring-reactive/src/main/java/com/baeldung/reactive/security/SecurityConfig.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,37 @@
1616
public class SecurityConfig {
1717

1818
@Bean
19-
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
19+
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
2020
return http.authorizeExchange()
21-
.pathMatchers("/admin")
22-
.hasAuthority("ROLE_ADMIN")
23-
.anyExchange()
24-
.authenticated()
25-
.and()
26-
.formLogin()
27-
.and()
28-
.csrf()
29-
.disable()
30-
.build();
21+
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
22+
.anyExchange().authenticated()
23+
.and()
24+
.formLogin()
25+
.and()
26+
.csrf().disable()
27+
.build();
3128
}
3229

3330
@Bean
3431
public MapReactiveUserDetailsService userDetailsService() {
3532
UserDetails user = User
36-
.withUsername("user")
37-
.password(passwordEncoder().encode("password"))
38-
.roles("USER")
39-
.build();
33+
.withUsername("user")
34+
.password(passwordEncoder().encode("password"))
35+
.roles("USER")
36+
.build();
4037

4138
UserDetails admin = User
42-
.withUsername("admin")
43-
.password(passwordEncoder().encode("password"))
44-
.roles("ADMIN")
45-
.build();
39+
.withUsername("admin")
40+
.password(passwordEncoder().encode("password"))
41+
.roles("ADMIN")
42+
.build();
4643

4744
return new MapReactiveUserDetailsService(user, admin);
4845
}
49-
46+
5047
@Bean
5148
public PasswordEncoder passwordEncoder() {
52-
return new BCryptPasswordEncoder();
49+
return new BCryptPasswordEncoder();
5350
}
5451

5552
}

spring-reactive/src/test/java/com/baeldung/reactive/security/SecurityIntegrationTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,32 @@
1515
public class SecurityIntegrationTest {
1616

1717
@Autowired
18-
ApplicationContext context;
18+
private ApplicationContext context;
1919

20-
private WebTestClient rest;
20+
private WebTestClient webTestClient;
2121

2222
@BeforeEach
2323
public void setup() {
24-
this.rest = WebTestClient.bindToApplicationContext(this.context).configureClient().build();
24+
webTestClient = WebTestClient.bindToApplicationContext(context)
25+
.configureClient()
26+
.build();
2527
}
2628

2729
@Test
2830
public void whenNoCredentials_thenRedirectToLogin() {
29-
this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection();
31+
webTestClient.get()
32+
.uri("/")
33+
.exchange()
34+
.expectStatus().is3xxRedirection();
3035
}
3136

3237
@Test
3338
@WithMockUser
3439
public void whenHasCredentials_thenSeesGreeting() {
35-
this.rest.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello, user");
40+
webTestClient.get()
41+
.uri("/")
42+
.exchange()
43+
.expectStatus().isOk()
44+
.expectBody(String.class).isEqualTo("Hello, user");
3645
}
3746
}

0 commit comments

Comments
 (0)