Websocket in Spring Boot app - Getting 403 Forbidden

后端 未结 3 832
长发绾君心
长发绾君心 2021-02-02 09:26

Websocket in Spring Boot app - Getting 403 Forbidden

I can connect to the websocket from client using sockjs/stompjs when I run this in eclipse (no spring boot).

<
3条回答
  •  囚心锁ツ
    2021-02-02 10:13

    Difference between my 2 environment was the version of jars.

    spring-boot-starter-websocket-1.2.5.RELEASE.jar
    spring-websocket-4.1.7.RELEASE.jar
    

    Because of the spring-boot-starter-websocket dependency while packaging it was picking up spring-websocket-4.1.7.RELEASE inspite of the spring_version being spring_version=4.0.6.RELEASE.

    Modified the dependency in build.gradle that fixed the problem.

    compile "org.springframework.boot:spring-boot-starter-websocket:1.1.0.RELEASE"
    

    I think in latest version of spring-websocket jar the class StompWebSocketEndpointRegistration has setAllowedOrigins method that has to be used.Have not tried this.

    But CORS filter with

    response.setHeader("Access-Control-Allow-Origin", "http://localhost:8089");
    

    is working with older version.

提交回复
热议问题