|
|
@ -1,12 +1,25 @@ |
|
|
|
package ru.digitalbanana.demoresourceserver.config; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
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.Authentication; |
|
|
|
import org.springframework.security.oauth2.jwt.Jwt; |
|
|
|
import org.springframework.security.oauth2.jwt.JwtDecoder; |
|
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter; |
|
|
|
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 org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; |
|
|
|
|
|
|
|
/** |
|
|
|
* Web Socket configuration |
|
|
|
* Created by dima on 8/12/16. |
|
|
@ -15,6 +28,30 @@ import org.springframework.web.socket.config.annotation.WebSocketTransportRegist |
|
|
|
@EnableWebSocketMessageBroker |
|
|
|
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private JwtDecoder jwtDecoder; |
|
|
|
|
|
|
|
@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())) { |
|
|
|
List<String> authorization = accessor.getNativeHeader("X-Authorization"); |
|
|
|
|
|
|
|
String accessToken = authorization.get(0);//.split(" ")[1]; |
|
|
|
Jwt jwt = jwtDecoder.decode(accessToken); |
|
|
|
JwtAuthenticationConverter converter = new JwtAuthenticationConverter(); |
|
|
|
Authentication authentication = converter.convert(jwt); |
|
|
|
accessor.setUser(authentication); |
|
|
|
} |
|
|
|
return message; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void configureMessageBroker(MessageBrokerRegistry registry) { |
|
|
|
registry.setApplicationDestinationPrefixes("/app"); |
|
|
|