Affects: 5.1.9
This is related to issue #21797. I am creating this here as I haven't been able to hear anything on Stackoverflow and Gitter.
I am migrating a Spring MVC library to Spring WebFlux. There is a feature that lets our clients annotate their controller methods to perform custom validation (and applying some business rules) on incoming headers before granting access to the API.
In Spring MVC we had that accomplished by using HandlerInterceptorAdapter. Since WebFlux doesn't have anything similar I was trying out the solution as suggested by @rstoyanchev in issue #21797. However it doesn't work for this use case since the method handler info is only available in onSuccess operator and its too late to get the info for annotation processing.
I was trying out the other approach suggested there using @ModelAttribute
method on a @ControllerAdvice
but that only work if the annotation is applied to the Controller class in our case the annotation is applied on methods of controller class.
I wanted to know if annotation processing is going to be supported the way it . has been with MVC or what's the recommended way to implement annotations in WebFlux
Here is a sample https://github.com/ranarula/handleInterceptor with the issue
Comment From: rstoyanchev
Indeed the suggestion under #21797 was for that particular use case. Spring Security has similar support for controller method @PreAuthorize
and that's implemented through an AOP method interceptor where the ServerWebExchange
can be accessed through the Reactor Context. That's an option but certainly not as straight forward as a HandlerInterceptor
.
I was trying out the other approach suggested there using
@ModelAttribute
method on a@ControllerAdvice
but that only work if the annotation is applied to the Controller class in our case the annotation is applied on methods of controller class.
Could you clarify? Such a method should be able to access the target handler through an exchange attribute. That would be a HandlerMethod
and that should give you access to annotations on the method.
Comment From: ranarula
Thanks @rstoyanchev for your response. If you please look at my sample aspect I am unable to get the subcriberContext using ServerWebExchangeContextFilter
.
Can you please point what I could be doing wrong there??
Comment From: rstoyanchev
@ranarula you can only get access to the Reactor context when you're part of the same execution chain. However the code you have is isolated and not connected. You can take a look at Spring Security's implementation of the same, but again an @ModelAttribute
method is going to be a lot simpler.
Comment From: ranarula
Thank . you @rstoyanchev - I am taking a look at the spring security and I too had realized about the missing execution chain. Thanks for your pointers.
I will also explore the @ModelAttribute approach
Comment From: rstoyanchev
Closing this for now. Feel free to comment further.
Comment From: namila007
@ranarula did you found a solution for your repo ? I was trying to access reactive context on AOP, but couldn't pass WebFilterChain for it