jhipster/generator-jhipster

ReactiveSecurityContextHolder.getContext() is empty , unable to getCurrentUserLogin() in any service except Gateway

deceivingrakesh opened this issue · 12 comments

Overview of the issue

Unable to fetch current loggedin user from a reactive microservice using the getCurrentUserLogin() method , except it works in Gateway

Motivation for or Use Case

Need to use current logged in user details in different services

Reproduce the error

When I changed the method getCurrentUserLogin() in SecurityUtils class to

 public static Mono<String> getCurrentUserLogin() {
        return ReactiveSecurityContextHolder.getContext().switchIfEmpty(Mono.error(new IllegalStateException("ReactiveSecurityContext is empty")))
            .map(SecurityContext::getAuthentication)
            .flatMap(authentication -> Mono.justOrEmpty(extractPrincipal(authentication)));
    }

the context is empty .

Related issues
Suggest a Fix
JHipster Version(s)

8.7.3

Browsers and Operating System
  • Tickets opened without reproduction steps or that doesn't follows the template recommendation will be closed.
  • Checking this box is mandatory (this is just to show you read everything)

@deceivingrakesh can you provide a PR with the change?

I don't have the solution for the problem yet , I'm still not able to get current logged in userId from microservices , other than gateway by using the getCurrentUserLogin() , when I debugged I got to know that ReactiveSecurityContextHolder.getContext() is returning empty inside the getCurrentUserLogin() method .

Is there any update on this issue , as of now I'm using Principal object in controller to get UserId .