Skip to content

Commit

Permalink
Added configurable cors definition
Browse files Browse the repository at this point in the history
  • Loading branch information
dogeared committed Jun 20, 2023
1 parent fff6bf8 commit a186a9d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
target
16 changes: 16 additions & 0 deletions src/main/java/io/snyk/snyklabs/MultiroomChatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.jackson.datatype.VavrModule;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class MultiroomChatApplication {

@Value("#{ @environment['cors.allowed_domains']}")
private String[] corsAllowedDomains;

public static void main(String[] args) {
SpringApplication.run(MultiroomChatApplication.class, args);
}
Expand All @@ -19,4 +25,14 @@ public ObjectMapper jacksonBuilder() {
return mapper.registerModule(new VavrModule());
}

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/ws/**").allowedOrigins(corsAllowedDomains);
}
};
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.snyk.snyklabs.app.websocket;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
Expand All @@ -10,6 +11,9 @@
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Value("#{ @environment['stomp.allowed_domains']}")
private String[] stompAllowedDomains;

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/chat");
Expand All @@ -19,8 +23,7 @@ public void configureMessageBroker(MessageBrokerRegistry config) {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setAllowedOrigins("http://localhost:3000",
"chrome-extension://ggnhohnkfcpcanfekomdkjffnfcjnjam")
.setAllowedOrigins(stompAllowedDomains)
.withSockJS();
}
}

0 comments on commit a186a9d

Please sign in to comment.