programing

보안 웹 소켓 서버(tomcat)를 통해 각 소켓(HTTPS)이 있는 스프링 웹 소켓(WSS)

iphone6s 2023. 8. 10. 18:33
반응형

보안 웹 소켓 서버(tomcat)를 통해 각 소켓(HTTPS)이 있는 스프링 웹 소켓(WSS)

저는 Spring web socket + stopp + SockJsClient를 사용하여 서버 측에서 Angular 클라이언트 애플리케이션으로 메시지를 보내는 작업을 하고 있습니다.

My Socket 서버는 8080 포트에서 실행되는 스프링 부트 응용 프로그램입니다.

ws/http 프로토콜보다 잘 작동합니다.하지만 이제 소켓 서버에서 SSL을 사용하도록 설정했습니다.

소켓 서버 구성.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/topic");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/sync").setAllowedOrigins("*").withSockJS();
}}

WS를 통한 Java Client의 작업 코드

List<Transport> transports = new ArrayList<Transport>(1);
transports.add(new RestTemplateXhrTransport());
SockJsClient sockJsClient = new SockJsClient(transports);
sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new StringMessageConverter());
String url = "ws://my-socket.server.com/sync";
StompSessionHandler sessionHandler = new MyStompSessionHandler(senderId, syncUrl, content);
stompClient.connect(url, sessionHandler);

HTTP를 통한 Angular 클라이언트의 작업 코드

const Stomp = StompJs.Stomp;
const socket = new SockJS('http://my-socket.server.com/sync');
this.stompClient = Stomp.over(socket);
this.stompClient.connect({}, (res, err) => {}

이제 별도의 스프링 부트 서버에서 실행되는 웹 소켓 서버를 통해 SSL을 구현했습니다.그리고 서버와 클라이언트 쪽에서 프로토콜을 업데이트합니다. ws tows http to https.

또한 SSL 컨텍스트를 추가하기 위해 다음을 시도합니다.

StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient();
List<Transport> transports = new ArrayList<Transport>(1);
Map<String, Object> userProperties = new HashMap<String, Object>();
userProperties.put("org.apache.tomcat.websocket.SSL_CONTEXT", SSLContext.getDefault());
simpleWebSocketClient.setUserProperties(userProperties);
transports.add(new WebSocketTransport(simpleWebSocketClient));

다음 스택 링크에서 참조했지만 운이 없습니다 :(

HTTPS(SSL)를 사용한 보안 웹 소켓

SSL과 함께 Spring WebSocket Client를 사용하는 방법은 무엇입니까?

제발 제가 그것에서 벗어날 수 있도록 도와주세요.

감사합니다 :)

저도 같은 문제가 발생했습니다. 나중에 보안 구성 파일에서 엔드포인트를 허용하지 않습니다. 단지 앤트매처에서 웹 소켓 엔드포인트를 허용합니다.

우리는 같은 문제를 통과하고, 먼저 SockJS로 제거합니다.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfigurer implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/");
        config.setApplicationDestinationPrefixes("/js");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/").setAllowedOrigins("*");
    }
}

프론트 엔드는 VUE에 있지만 JS와 마찬가지로 연결이 동일합니다.

    WEB_SOCKET_URL_PREFIX: 'ws://localhost:8080/'

    connect(resolve) {
        const URL = '/somePath';
        this.webSocket = new WebSocket(HttpConfig.WEB_SOCKET_URL_PREFIX);
        this.stompClient = Stomp.over(this.webSocket, { debug: false });

        this.stompClient.connect({}, () => {
            this.stompClient.subscribe(URL, data => {
                console.log(data);
            });
        });
    }

그리고 당신의 자바 코드에서 당신은 수정을 보낼 수 있습니다.


    private final SimpMessagingTemplate simpMessagingTemplate;

public void someMethod(){

...
 simpMessagingTemplate.convertAndSend("/somePath", "someContent");

...
}

언급URL : https://stackoverflow.com/questions/56662596/spring-websocket-wss-with-angular-sockjs-https-over-secure-web-socket-server

반응형