I would like to send message to /queue/errors/{userId}, when user is not in the group and also I would like to reject message to group. Problem is how to send message to /queue/errors/{userId} without sending them to /queue/group/{groupId}. While I return null when user is not in the group I can't send message to /queue/errors/{userId} because all messages are reject. I have logs:
2025-07-13T10:02:47.759Z WARN 1 --- [chat-service] [nio-8082-exec-2] o.w.c.c.WebSocketAuthChannelInterceptor : User not in group!
2025-07-13 12:02:47 2025-07-13T10:02:47.761Z DEBUG 1 --- [chat-service] [nio-8082-exec-2] o.s.m.s.b.SimpleBrokerMessageHandler : Processing MESSAGE destination=/queue/errors/11 session=null payload=You are not a member of group!
2025-07-13 12:02:47 2025-07-13T10:02:47.764Z DEBUG 1 --- [chat-service] [nio-8082-exec-2] o.s.m.s.ExecutorSubscribableChannel : WebSocketAuthChannelInterceptor returned null from preSend, i.e. precluding the send.
@Slf4j
@Component
@RequiredArgsConstructor
public class WebSocketAuthChannelInterceptor implements ChannelInterceptor {
private final JwtService jwtService;
private final GroupService groupService;
private SimpMessagingTemplate messagingTemplate;
@Autowired
public void setMessagingTemplate(@Lazy SimpMessagingTemplate messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
assert accessor != null;
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
String token = accessor.getFirstNativeHeader("Authorization");
if (token == null || !token.startsWith("Bearer ")) {
log.warn("No token provided or invalid format!");
return null;
}
token = token.substring(7);
if (!jwtService.isTokenValid(token)) {
log.warn("Invalid token");
return null;
}
String groupId = accessor.getFirstNativeHeader("groupId");
long userId = jwtService.extractId(token);
log.info("Group id: {}", groupId);
if (!groupService.validateUserInGroup(groupId, userId)) {
log.warn("User not in group!");
sendErrorToUser(userId, "You are not a member of group!");
return null;
}
String email = jwtService.extractEmail(token);
List<String> roles = jwtService.extractRoles(token);
Authentication authentication = new UsernamePasswordAuthenticationToken(
email, null, mapRolesToAuthorities(roles)
);
accessor.setUser(authentication);
SecurityContextHolder.getContext().setAuthentication(authentication);
log.info("User is authorized by WebSocket: {}", email);
return message;
}
return message;
}
private Collection<? extends GrantedAuthority> mapRolesToAuthorities(List<String> roles) {
return roles.stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" + role))
.collect(Collectors.toList());
}
private void sendErrorToUser(long userId, String errorMsg) {
String destination = "/queue/errors/" + userId;
messagingTemplate.convertAndSend(destination, errorMsg);
}
}