[*]WebSocket (STOMP) endpoint (with message broker)
[*]Clients connect via STOMP and authenticate with JWT (sent as a STOMP Authorization header in the CONNECT frame)
[*]Spring Security with @PreAuthorize on @MessageMapping methods
< /ul>
У меня есть канал Interceptor, который: < /p>
Код: Выделить всё
- On CONNECT, parses the JWT from the Authorization header
- Validates JWT and creates an Authentication object (with authorities)
- Calls StompHeaderAccessor.setUser(authentication)
< /code>
Мой контроллер выглядит как: < /p>
@MessageMapping("/chat/message.send")
@PreAuthorize("hasAuthority('SCOPE_WRITE')")
public void sendMessage(@Payload MessageRequestDto dto) {
}
< /code>
Но всякий раз, когда получается отправка топа, я получаю:
AuthenticationCredentialSnotFoundException: объект аутентификации не был найден в SecurityContext < /p>
из -за: < /p>
private Authentication getAuthenticationOrThrow() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated())
throw new AuthenticationCredentialsNotFoundException("No authenticated user found in SecurityContext");
return authentication;
}
I также пытался (как предложено Spring /Stackoverflow ответов), добавив второй канал Internetceptor, который, для каждого входящего сообщения, Sopies Accesster.Getuser. Я подтвердил в журналах, что Accesster.getUser () является действительным объектом аутентификации во время обработки сообщений, с правильными органами.
Подробнее здесь: https://stackoverflow.com/questions/796 ... oblem-auth