|
|
- 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.core.Ordered;
- import org.springframework.core.annotation.Order;
- 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.
- */
- @Configuration
- @Order(Ordered.HIGHEST_PRECEDENCE + 99)
- @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 (accessor!=null && StompCommand.CONNECT.equals(accessor.getCommand())) {
- List<String> authorization = accessor.getNativeHeader("Authorization");
- String accessToken = authorization != null ? 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.enableSimpleBroker("/simple");
- registry.setApplicationDestinationPrefixes("/app");
- }
-
- @Override
- public void registerStompEndpoints(StompEndpointRegistry registry) {
- registry
- .addEndpoint("/ws")
- .setAllowedOriginPatterns("*")
- .withSockJS();
- }
-
- @Override
- public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
- registration.setSendBufferSizeLimit(1024 * 512 * 1024); // default : 512 * 1024
-
- }
- }
|