File tree Expand file tree Collapse file tree
main/java/com/baeldung/reactive/security
test/java/com/baeldung/reactive/security Expand file tree Collapse file tree Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 55import 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 () {
Original file line number Diff line number Diff line change 1616public 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}
Original file line number Diff line number Diff line change 1515public 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}
You can’t perform that action at this time.
0 commit comments