Can you explain where the advice handling @PreAuthorize("hasRole('ADMIN')")
retrieves the SecurityContext
in a Reactive application?
The following Spring Security example is a good illustration of this kind of usage: https://github.com/spring-projects/spring-security/tree/5.0.0.M4/samples/javaconfig/hellowebflux-method
After checking the Spring Security Webflux source code, I've found some implementations of SecurityContextRepository
but the load method needs the ServerWebExchange
as a parameter.
I'm trying to understand how to replace SecurityContextHolder.getContext().getAuthentication()
call in a standard service (because ThreadLocal
is no longer an option in a Reactive Application), but I don't understand how to replace this with a call to a SecurityContextRepository
without a reference on the ServerWebExchange
.
Spring Security provides method level security using @PreAuthorize and @PostAuthorize annotations. This is expression-based access control. The @PreAuthorize can check for authorization before entering into method. The @PreAuthorize authorizes on the basis of role or the argument which is passed to the method.
The @PreAuthorize annotation checks the given expression before entering the method, whereas the @PostAuthorize annotation verifies it after the execution of the method and could alter the result.
Overview. Spring 5 includes Spring WebFlux, which provides reactive programming support for web applications. In this tutorial, we'll create a small reactive REST application using the reactive web components RestController and WebClient. We'll also look at how to secure our reactive endpoints using Spring Security.
@PostAuthorize can be authorized on the basis of logged in roles, return object by method and passed argument to the method. For the returned object spring security provides built-in keyword i.e. returnObject. Define @PreAuthorize and @PostAuthorize in the interface of the service layer.
You're right, ThreadLocal is no longer an option because the processing of a request is not tied to a particular thread. Currently, Spring Security is storing the authentication information as a ServerWebExchange attribute, so tied to the current request/response pair.
The @PreAuthorize can check for authorization before entering into method. The @PreAuthorize authorizes on the basis of role or the argument which is passed to the method. The @PostAuthorize checks for authrorisation after method execution.
The @PreAuthorize can check for authorization before entering into method. The @PreAuthorize authorizes on the basis of role or the argument which is passed to the method.
The ReactiveSecurityContextHolder
provides the authentication in a reactive way, and is analogous to SecurityContextHolder
.
Its getContext()
method provides a Mono<SecurityContext>
, just like SecurityContextHolder.getContext()
provides a SecurityContext
.
ReactiveSecurityContextHolder
.getContext()
.map(context ->
context.getAuthentication()
You're right, ThreadLocal
is no longer an option because the processing of a request is not tied to a particular thread.
Currently, Spring Security is storing the authentication information as a ServerWebExchange
attribute, so tied to the current request/response pair. But you still need that information when you don't have direct access to the current exchange, like @PreAuthorize
.
The authentication information is stored in the Reactive pipeline itself (so accessible from your Mono
or Flux
), which is a very interesting Reactor feature - managing a context tied to a particular Subscriber
(in a web application, the HTTP client is pulling data from the server and acts as such).
I'm not aware of an equivalent of SecurityContextHolder
, or some shortcut method to get the Authentication information from the context.
See more about Reactor Context feature in the reference documentation. You can also see an example of that being used in Spring Security here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With