Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket in Spring Boot app - Getting 403 Forbidden

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).

But when I create a Spring boot jar(gradlew build) for the websocket code and run the java -jar websocket-code.jar I get a 403 error connecting to the websocket.

I have no authentication for the websockets. I have a CORS filters and think have all headers right in request/response.

Below is my 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' } 

Update:

Added a CORS filter with response.setHeader("Access-Control-Allow-Origin", "http://localhost:8089");

In firebug on Client side

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 

Origin I am requesting from is in the Allow-origin list. Still getting Request Rejected message in the logs.

like image 716
user3755282 Avatar asked Sep 30 '15 19:09

user3755282


People also ask

What is the use of WebSocket in spring boot?

WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. WebSocket are bi-directional - Using WebSocket either client or server can initiate sending a message. WebSocket are Full Duplex - The client and server communication is independent of each other.

Can you upgrade only to WebSocket spring boot?

If you hit the link with your browser, you will probably get an error Can "Upgrade" only to "WebSocket" . This is because browsers not open WebSockets by default, this needs a proper client. Since we not yet implemented a real client it is hard to verify our implementation.

What is spring WebSockets?

WebSockets is a bidirectional, full-duplex, persistent connection between a web browser and a server. Once a WebSocket connection is established, the connection stays open until the client or server decides to close this connection.


1 Answers

I had a similar issue and fixed it in the WebSocketConfig by setting the allowed origins to "*".

@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 } 
like image 156
Dustin Shimono Avatar answered Oct 04 '22 10:10

Dustin Shimono