You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package ru.digitalbanana.demoresourceserver.config;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.Ordered;
  6. import org.springframework.core.annotation.Order;
  7. import org.springframework.messaging.Message;
  8. import org.springframework.messaging.MessageChannel;
  9. import org.springframework.messaging.simp.config.ChannelRegistration;
  10. import org.springframework.messaging.simp.config.MessageBrokerRegistry;
  11. import org.springframework.messaging.simp.stomp.StompCommand;
  12. import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
  13. import org.springframework.messaging.support.ChannelInterceptor;
  14. import org.springframework.messaging.support.MessageHeaderAccessor;
  15. import org.springframework.security.core.Authentication;
  16. import org.springframework.security.oauth2.jwt.Jwt;
  17. import org.springframework.security.oauth2.jwt.JwtDecoder;
  18. import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
  19. import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
  20. import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
  21. import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
  22. import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
  23. /**
  24. * Web Socket configuration
  25. * Created by dima on 8/12/16.
  26. */
  27. @Configuration
  28. @Order(Ordered.HIGHEST_PRECEDENCE + 99)
  29. @EnableWebSocketMessageBroker
  30. public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
  31. @Autowired
  32. private JwtDecoder jwtDecoder;
  33. @Override
  34. public void configureClientInboundChannel(ChannelRegistration registration) {
  35. registration.interceptors(new ChannelInterceptor() {
  36. @Override
  37. public Message<?> preSend(Message<?> message, MessageChannel channel) {
  38. StompHeaderAccessor accessor =
  39. MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
  40. if (accessor!=null && StompCommand.CONNECT.equals(accessor.getCommand())) {
  41. List<String> authorization = accessor.getNativeHeader("Authorization");
  42. String accessToken = authorization != null ? authorization.get(0).split(" ")[1] : "";
  43. Jwt jwt = jwtDecoder.decode(accessToken);
  44. JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
  45. Authentication authentication = converter.convert(jwt);
  46. accessor.setUser(authentication);
  47. }
  48. return message;
  49. }
  50. });
  51. }
  52. @Override
  53. public void configureMessageBroker(MessageBrokerRegistry registry) {
  54. registry.enableSimpleBroker("/simple");
  55. registry.setApplicationDestinationPrefixes("/app");
  56. }
  57. @Override
  58. public void registerStompEndpoints(StompEndpointRegistry registry) {
  59. registry
  60. .addEndpoint("/ws")
  61. .setAllowedOriginPatterns("*")
  62. .withSockJS();
  63. }
  64. @Override
  65. public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
  66. registration.setSendBufferSizeLimit(1024 * 512 * 1024); // default : 512 * 1024
  67. }
  68. }