-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketConfig.java
More file actions
55 lines (47 loc) · 2.44 KB
/
Copy pathWebSocketConfig.java
File metadata and controls
55 lines (47 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.github.codehive.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import com.github.codehive.websocket.CsvProgressWebSocketHandler;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
/**
* Comma-separated list of allowed frontend origins, e.g.:
* -Dcodehive.frontend.origins=http://localhost:3000,https://app.example.com
*/
private static final String[] ALLOWED_ORIGINS =
System.getProperty("codehive.frontend.origins", "http://localhost:3000")
.split("\\s*,\\s*");
private final CsvProgressWebSocketHandler csvProgressHandler;
public WebSocketConfig(CsvProgressWebSocketHandler csvProgressHandler) {
this.csvProgressHandler = csvProgressHandler;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(csvProgressHandler, "/ws/csv-progress")
.setAllowedOrigins(ALLOWED_ORIGINS)
.addInterceptors(new HandshakeInterceptor() {
@Override
public boolean beforeHandshake(ServerHttpRequest request,
ServerHttpResponse response,
WebSocketHandler wsHandler,
java.util.Map<String, Object> attributes) {
// Require an authenticated principal for WebSocket connections.
return request.getPrincipal() != null;
}
@Override
public void afterHandshake(ServerHttpRequest request,
ServerHttpResponse response,
WebSocketHandler wsHandler,
Exception exception) {
// No-op
}
});
}
}