【发布时间】:2015-12-28 17:32:35
【问题描述】:
Spring Boot 应用程序中的 Websocket - 得到 403 禁止
当我在 Eclipse 中运行它时,我可以使用 sockjs/stompjs 从客户端连接到 websocket(没有 spring boot)。
但是当我为 websocket 代码创建一个 Spring boot jar(gradlew build) 并运行 java -jar websocket-code.jar 时,连接到 websocket 时出现 403 错误。
我没有对 websockets 的身份验证。 我有一个 CORS 过滤器,并认为所有标题都在请求/响应中。
下面是我的 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'
}
更新:
添加了一个 CORS 过滤器 response.setHeader("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] o.s.w.s.s.t.h.DefaultSockJsService : Request rejected, Origin header value http://localhost:8089 not allowed
我请求的来源在允许来源列表中。日志中仍然收到 Request Rejected 消息。
【问题讨论】:
标签: java gradle websocket spring-boot sockjs