2015-09-30 3 views
16

のWebSocketを取得します。のWebSocket - 私は日食(なし春ブーツ)でこれを実行すると、私はsockjs/stompjsを使用してクライアントからのWebSocketに接続することができます403禁止</p> <p>を取得する - 春ブーツアプリで403禁止

しかし、websocketコード用のSpringブートjar(gradlew build)を作成し、java -jar websocket-code.jarを実行すると、websocketに接続する際に403エラーが発生します。

私はウェブソケットの認証がありません。 私はCORSフィルタを持っており、リクエスト/レスポンスですべてのヘッダーが正しいと思っています。以下は

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' 
} 

更新私のbuild.gradleです:

は response.setHeader( "アクセス制御 - 許可 - 起源"、 "http://localhost:8089")でCORSフィルターを追加しました。放火犯で

クライアント側のIから要求しています

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メッセージが表示されます。

答えて

2

私の2つの環境の違いは、jarsのバージョンです。ので、それはspring_version = 4.0.6.RELEASEあるspring_versionのばねのWebSocket-4.1.7.RELEASEのinspiteをピックアップしパッケージングしながら、バネブートスタータのWebSocket依存の

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

問題を修正したbuild.gradleの依存関係を修正しました。

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

Iは、ばねのWebSocketジャーの最新バージョンでクラスStompWebSocketEndpointRegistrationはused.Haveこれを試していないする必要がありますsetAllowedOrigins方法があると思います。

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

しかし、CORSフィルタは、古いバージョンで作業しています。

+0

これがまだ動作しているかどうかは不明です。私にとっては、Webソケットレイヤーは、証明書が真であることを要求し、CORSに自分のセットを許可しても許可します。私はまだ403が禁じられている – numerical25

47

私も同様の問題があり、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 
} 
+1

感謝を推測コントローラに@SendToによって登録されて

registry.addEndpoint("/your-end-point").setAllowedOrigins("*").withSockJS(); 

「あなたのエンドポイント」を追加してみてください。私はsetAllowedOriginsが新しいリリースのStompWebSocketEndpointRegistrationに追加されたと思います。あなたのソリューションは新しいリリースで動作するはずです。私は古いwebsocket jarとして使用しています。 – user3755282

+0

魔法のように働いた – nurgasemetey

+4

allowOriginsにこのようなワイルドカードを使用することは安全ではありませんか? – niilzon

1

は、私は答えを

関連する問題