Spring Boot应用程序中的Websocket – 获取403禁止
Spring Boot应用程序中的Websocket – 获取403禁止
当我在eclipse(没有弹簧启动)中运行时,我可以使用sockjs / stompjs从客户端连接到websocket。
但是当我为websocket代码创建一个Spring启动jar(gradlew build)并运行java -jar websocket-code.jar时,我得到一个连接到websocket的403错误。
我没有websockets的身份validation。 我有一个CORSfilter,并认为请求/响应中的所有标题正确。
下面是我的build.gradle
apply plugin: 'java' apply plugin: 'spring-boot' apply plugin: 'war' sourceCompatibility = 1.7 version = '1.0' repositories { mavenCentral() } buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE") } } configurations { compile.exclude module: "spring-boot-starter-tomcat" } dependencies { compile "org.springframework:spring-web:$spring_version" compile "org.springframework:spring-webmvc:$spring_version" compile "org.springframework:spring-websocket:$spring_version" compile "org.springframework:spring-messaging:$spring_version" compile "org.springframework.boot:spring-boot-starter-websocket" compile "org.springframework.boot:spring-boot-starter-web" compile "com.fasterxml.jackson.core:jackson-databind:2.6.2" compile "com.fasterxml.jackson.core:jackson-core:2.6.2" compile "com.fasterxml.jackson.core:jackson-annotations:2.6.2" compile "org.springframework.amqp:spring-rabbit:1.3.5.RELEASE" compile("org.springframework:spring-tx") compile("org.springframework.boot:spring-boot-starter-web:1.2.6.RELEASE") compile("org.springframework.boot:spring-boot-starter-jetty:1.2.6.RELEASE") testCompile group: 'junit', name: 'junit', version: '4.11' testCompile "org.springframework:spring-test:$spring_version" } task wrapper(type: Wrapper) { gradleVersion = '2.5' }
更新:
添加了带有response.setHeader的CORSfilter(“Access-Control-Allow-Origin”,“ http:// localhost:8089 ”);
在客户端的萤火虫
Request Headers Origin http://localhost:8089 Response Headers Access-Control-Allow-Origin http://localhost:8089 Server Logs 2015-10-02 16:57:31.372 DEBUG 1848 --- [qtp374030679-14] oswssthDefaultSockJsService : Request rejected, Origin header value http://localhost:8089 not allowed
我请求的来源是在允许来源列表中。 仍在日志中收到Request Rejected消息。
我有一个类似的问题,并通过将允许的起源设置为“*”将其修复在WebSocketConfig中。
@Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // the endpoint for websocket connections registry.addEndpoint("/stomp").setAllowedOrigins("*").withSockJS(); } // remaining config not shown as not relevant }
我的2环境之间的差异是jar子的版本。
spring-boot-starter-websocket-1.2.5.RELEASE.jar spring-websocket-4.1.7.RELEASE.jar
由于spring-boot-starter-websocket依赖,在打包时它正在拾取spring-websocket-4.1.7.RELEASE,尽管spring_version是spring_version = 4.0.6.RELEASE。
修改了修复问题的build.gradle中的依赖项。
compile "org.springframework.boot:spring-boot-starter-websocket:1.1.0.RELEASE"
我认为在最新版本的spring-websocket jar中,类StompWebSocketEndpointRegistration具有必须使用的setAllowedOrigins方法。但是没试过这个。
但CORSfilter
response.setHeader("Access-Control-Allow-Origin", "http://localhost:8089");
正在使用旧版本。
尝试添加
registry.addEndpoint("/your-end-point").setAllowedOrigins("*").withSockJS();
我想,“你的终点”是由@SendTo在控制器中注册的