【发布时间】:2021-01-20 20:50:10
【问题描述】:
我试图在用户被管理员禁止时强制取消订阅,并在该用户被禁止时阻止该用户订阅。禁令状态保存在我的数据库中。我目前有一个拦截器在preSend 进行阻塞,但是当我抛出异常时连接关闭。无论如何在不关闭套接字连接的情况下拒绝订阅消息吗?
@Override
public Message<?> preSend(@NonNull Message<?> message, @NonNull MessageChannel channel) {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) {
checkIsBanned(headerAccessor.getDestination(), headerAccessor.getUser());
}
return message;
}
private void checkIsBanned(String destination, Principal principal) {
if (destination == null || !destination.matches(CHAT_TOPIC_REGEX)) {
return;
}
String errorMessage = resourceBundle.getMessage("err.channel.banned");
if (!(principal instanceof OAuth2Authentication) || !(((OAuth2Authentication) principal).getPrincipal() instanceof SUserDetails)) {
throw new MessagingException(errorMessage);
}
String channelId = destination.split("/")[3];
Long profileId = getProfileId(principal);
if (profileId == null) {
throw new MessagingException(errorMessage);
}
channelUserRepo.findByChannelIdAndUserId(channelId, profileId).filter(ChannelUser::isBanned).orElseThrow(() -> new MessagingException(errorMessage));
}
private Long getProfileId(Principal principal) {
return ((SUserDetails) ((OAuth2Authentication) principal).getPrincipal()).getProfileId();
}
当管理员禁止用户时,我还尝试通过从userRegistry 获取用户并删除他的订阅来强制取消订阅,但这没有用。有什么办法也可以吗?
【问题讨论】:
标签: spring spring-boot spring-websocket