Reactive access to SecurityContext

In order to access the SecurityContext in the new reactive Spring Security module, we have a new class called ReactiveSecurityContextHolder.

ReactiveSecurityContextHolder provides access to the current SecurityContext in a reactive manner over a static getContext method, which returns Mono<SecurityContext>. This means that we can write the following code in order to access SecurityContext in the application:

@RestController                                                    // (1)@RequestMapping("/api/v1")                                         //public class SecuredProfileController {                            //       @GetMapping("/profiles")                                        // (2)   @PreAuthorize("hasRole(USER)")                                  // (2.1)   public Mono<Profile> getProfile() {                             // (2.2)      return ReactiveSecurityContextHolder                         // (2.3)         .getContext()                                             // (2.4)         .map(SecurityContext ...

Get Hands-On Reactive Programming in Spring 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.