gpt4 book ai didi

java - Spring Boot 应用程序中的 Websocket - 获取 403 Forbidden

转载 作者:IT老高 更新时间:2023-10-28 20:55:22 26 4
gpt4 key购买 nike

Spring Boot 应用程序中的 Websocket - 得到 403 禁止

当我在 Eclipse 中运行它时,我可以使用 sockjs/stompjs 从客户端连接到 websocket(没有 spring boot)。

但是当我为 websocket 代码创建一个 Spring boot jar(gradlew build) 并运行 java -jar websocket-code.jar 时,连接到 websocket 时出现 403 错误。

我没有对 websocket 进行身份验证。我有一个 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 ");

在客户端的 Firebug 中

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 消息。

最佳答案

我有一个类似的问题,并在 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
}

关于java - Spring Boot 应用程序中的 Websocket - 获取 403 Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32874421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com