#@AuthenticationPrincipal is an empty object in @MessageMapping handler

1 messages · Page 1 of 1 (latest)

random mica
#

I'm creating a websocket + REST application in spring using the provided STOMP support.

I'm also using JWT authentication and doing what's said in http://docs.spring.io/spring-framework/reference/web/websocket/stomp/authentication-token-based.html

but when i access the @AuthenticationPrincipal in a @MessagingMapping handler, it's an empty object.

i tried setting the authentication in the SecurityContext too but it's always null. It works with regular REST endpoints though.

The handlers

@RestController
public class HelloWorldController {
    
    @GetMapping("/greeting")
    public String getMethodName(@AuthenticationPrincipal User user) {
        return "HELLO " + user.getUsername(); // Works fine
    }

    @MessageMapping("/meeting")
    public String hello(@Payload String name, @AuthenticationPrincipal User user) {
        return "Hello, " + name + "! your email is " + user.getEmail(); // Hello, <name>! your email is null
    }

}```

But im able to access the principal using the `SimpMessageHeaderAcessor` but i need to type cast twice in each message handler.
Is there a way to obtain the currently authenticated user in a `@MessageMapping` handler?
shrewd marlinBOT
#

This post has been reserved for your question.

Hey @random mica! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.