72 行
3.1 KiB
Java
72 行
3.1 KiB
Java
|
|
package com.xuqm.im.config;
|
||
|
|
|
||
|
|
import com.xuqm.common.security.JwtUtil;
|
||
|
|
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.authentication.UsernamePasswordAuthenticationToken;
|
||
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||
|
|
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||
|
|
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||
|
|
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||
|
|
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
@Configuration
|
||
|
|
@EnableWebSocketMessageBroker
|
||
|
|
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||
|
|
|
||
|
|
private final JwtUtil jwtUtil;
|
||
|
|
|
||
|
|
public WebSocketConfig(JwtUtil jwtUtil) {
|
||
|
|
this.jwtUtil = jwtUtil;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||
|
|
registry.enableSimpleBroker("/topic", "/queue");
|
||
|
|
registry.setApplicationDestinationPrefixes("/app");
|
||
|
|
registry.setUserDestinationPrefix("/user");
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||
|
|
registry.addEndpoint("/ws/im")
|
||
|
|
.setAllowedOriginPatterns("*")
|
||
|
|
.withSockJS();
|
||
|
|
registry.addEndpoint("/ws/im")
|
||
|
|
.setAllowedOriginPatterns("*");
|
||
|
|
}
|
||
|
|
|
||
|
|
@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 (accessor != null && StompCommand.CONNECT.equals(accessor.getCommand())) {
|
||
|
|
String token = accessor.getFirstNativeHeader("Authorization");
|
||
|
|
if (token != null && token.startsWith("Bearer ")) {
|
||
|
|
token = token.substring(7);
|
||
|
|
if (jwtUtil.isValid(token)) {
|
||
|
|
String userId = jwtUtil.getSubject(token);
|
||
|
|
UsernamePasswordAuthenticationToken auth =
|
||
|
|
new UsernamePasswordAuthenticationToken(userId, null,
|
||
|
|
List.of(new SimpleGrantedAuthority("ROLE_USER")));
|
||
|
|
accessor.setUser(auth);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return message;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|