Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds cors support #477

Merged
merged 8 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ server:
port: 8082
servlet:
context-path: /
allowedOriginsUrls: ""
```

#### Change the Context-Path
Expand All @@ -120,6 +121,17 @@ server:

It is then available under http://localhost:8082/monitor.

#### Cross Origin Requests

To enable cors, add the allowed origins (`;` seperated) in the following property:
schmetzyannick marked this conversation as resolved.
Show resolved Hide resolved

```
server:
allowedOriginsUrls: http://remote-hoste:8082;https://remote-hoste:8082
```

It is then available under http://localhost:8082/monitor.
schmetzyannick marked this conversation as resolved.
Show resolved Hide resolved

#### Customize the Look & Feel

You can customize the look & feel of the Zeebe Simple Monitor (aka. white-labeling). For example, to change the logo or
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/zeebe/monitor/WebSocketConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.zeebe.monitor;

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 @@ -9,6 +10,9 @@
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Value("${server.allowedOriginsUrls}")
private String allowedOriginsUrls;

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/");
Expand All @@ -17,6 +21,11 @@ public void configureMessageBroker(MessageBrokerRegistry config) {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/notifications").withSockJS();
if (this.allowedOriginsUrls != null && this.allowedOriginsUrls.length() > 0) {
schmetzyannick marked this conversation as resolved.
Show resolved Hide resolved
String[] allowedOriginsUrlArr = this.allowedOriginsUrls.split(";");
registry.addEndpoint("/notifications").setAllowedOrigins(allowedOriginsUrlArr).withSockJS();
schmetzyannick marked this conversation as resolved.
Show resolved Hide resolved
} else {
registry.addEndpoint("/notifications").withSockJS();
}
}
}
27 changes: 23 additions & 4 deletions src/main/java/io/zeebe/monitor/ZeebeSimpleMonitorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@

import com.samskivert.mustache.Mustache;
import io.camunda.zeebe.spring.client.EnableZeebeClient;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
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.data.web.config.EnableSpringDataWebSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableZeebeClient
@EnableScheduling
@EnableAsync
@EnableSpringDataWebSupport
public class ZeebeSimpleMonitorApp {
@Value("${server.allowedOriginsUrls}")
private String allowedOriginsUrls;

public static void main(final String... args) {
SpringApplication.run(ZeebeSimpleMonitorApp.class, args);
Expand All @@ -60,4 +65,18 @@ public Mustache.Compiler configureFallbackValueForMissingVariablesInMustacheTemp
Mustache.TemplateLoader templateLoader) {
return Mustache.compiler().defaultValue("⍰").withLoader(templateLoader);
}

@Bean
public WebMvcConfigurer corsConfigurer() {
String urls = this.allowedOriginsUrls;
schmetzyannick marked this conversation as resolved.
Show resolved Hide resolved
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
if (urls != null && urls.length() > 0) {
schmetzyannick marked this conversation as resolved.
Show resolved Hide resolved
String[] allowedOriginsUrlArr = urls.split(";");
registry.addMapping("/**").allowedOrigins(allowedOriginsUrlArr);
}
}
};
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ server:
port: 8082
servlet:
context-path: /
allowedOriginsUrls: ""
schmetzyannick marked this conversation as resolved.
Show resolved Hide resolved

logging:
level:
Expand Down