rax-remote-2/vital-signs/src/main/java/com/rax/vital/config/WebSocketConfig.java

85 lines
3.5 KiB
Java
Raw Normal View History

2024-02-21 11:47:40 +08:00
package com.rax.vital.config;
2024-03-15 16:55:59 +08:00
import org.springframework.beans.factory.annotation.Autowired;
2024-02-21 11:47:40 +08:00
import org.springframework.context.annotation.Configuration;
2024-03-15 13:30:31 +08:00
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.config.ChannelRegistration;
2024-02-21 11:47:40 +08:00
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
2024-03-15 13:30:31 +08:00
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;
2024-03-15 16:55:59 +08:00
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
2024-02-21 11:47:40 +08:00
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
2024-03-15 16:55:59 +08:00
import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler;
2024-02-21 11:47:40 +08:00
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
2024-03-15 16:55:59 +08:00
@Autowired
private OAuth2AuthorizationService authorizationService;
2024-02-21 11:47:40 +08:00
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
2024-03-15 13:30:31 +08:00
registry.addEndpoint("/rax/chat", "/rax/ai-medicine", "/rax/doctor-medicine", "/rax/vital-signs", "/rax/SurgeryData")
.setAllowedOrigins("*");
2024-03-15 16:55:59 +08:00
// 错误处理
registry.setErrorHandler(new StompSubProtocolErrorHandler() {
@Override
public Message<byte[]> handleClientMessageProcessingError(Message<byte[]> clientMessage, Throwable ex) {
return super.handleClientMessageProcessingError(clientMessage, ex);
}
@Override
public Message<byte[]> handleErrorMessageToClient(Message<byte[]> errorMessage) {
return super.handleErrorMessageToClient(errorMessage);
}
@Override
protected Message<byte[]> handleInternal(StompHeaderAccessor errorHeaderAccessor, byte[] errorPayload, Throwable cause, StompHeaderAccessor clientHeaderAccessor) {
return super.handleInternal(errorHeaderAccessor, errorPayload, cause, clientHeaderAccessor);
}
});
2024-02-21 11:47:40 +08:00
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/front");
2024-03-01 13:01:35 +08:00
registry.setUserDestinationPrefix("/topic/user");
2024-02-21 11:47:40 +08:00
}
2024-03-15 16:55:59 +08:00
/**
* stomp未登录验证
* @param registration
*/
2024-03-15 13:30:31 +08:00
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
2024-03-15 16:55:59 +08:00
String token = accessor.getNativeHeader("access_token").get(0);
OAuth2Authorization authorization = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN);
if (authorization == null) {
throw new AccessDeniedException("Access is denied");
} else {
accessor.setUser(authorization.getAttribute("java.security.Principal"));
}
2024-03-15 13:30:31 +08:00
}
return message;
}
});
}
2024-02-21 11:47:40 +08:00
}