|
@@ -0,0 +1,66 @@
|
|
|
+package com.pig4cloud.pigx.act.config;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.messaging.Message;
|
|
|
+import org.springframework.messaging.MessageChannel;
|
|
|
+import org.springframework.messaging.simp.config.ChannelRegistration;
|
|
|
+import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
|
|
+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;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
|
|
+import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
|
|
|
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
|
|
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
|
|
+import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author new
|
|
|
+ * <p>
|
|
|
+ * WebSocket配置类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Configuration
|
|
|
+@AllArgsConstructor
|
|
|
+@EnableWebSocketMessageBroker
|
|
|
+public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
|
|
+ private RemoteTokenServices tokenService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void registerStompEndpoints(StompEndpointRegistry registry) {
|
|
|
+ registry.addEndpoint("/ws")
|
|
|
+ .setAllowedOrigins("*")
|
|
|
+ .withSockJS();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void configureMessageBroker(MessageBrokerRegistry registry) {
|
|
|
+ registry.setUserDestinationPrefix("/task/");
|
|
|
+ }
|
|
|
+
|
|
|
+ @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())) {
|
|
|
+ String tokens = accessor.getFirstNativeHeader("Authorization");
|
|
|
+ log.info("webSocket token is {}", tokens);
|
|
|
+ if (StrUtil.isBlank(tokens)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ OAuth2Authentication auth2Authentication = tokenService.loadAuthentication(tokens.split(" ")[1]);
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(auth2Authentication);
|
|
|
+ accessor.setUser(() -> (String) auth2Authentication.getPrincipal());
|
|
|
+ }
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|