|
|
@ -15,6 +15,8 @@ |
|
|
*/ |
|
|
*/ |
|
|
package org.thingsboard.server.config; |
|
|
package org.thingsboard.server.config; |
|
|
|
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.context.annotation.Bean; |
|
|
import org.springframework.context.annotation.Bean; |
|
|
import org.springframework.context.annotation.Configuration; |
|
|
import org.springframework.context.annotation.Configuration; |
|
|
import org.springframework.http.HttpStatus; |
|
|
import org.springframework.http.HttpStatus; |
|
|
@ -40,11 +42,15 @@ import java.util.Map; |
|
|
@Configuration |
|
|
@Configuration |
|
|
@TbCoreComponent |
|
|
@TbCoreComponent |
|
|
@EnableWebSocket |
|
|
@EnableWebSocket |
|
|
|
|
|
@RequiredArgsConstructor |
|
|
|
|
|
@Slf4j |
|
|
public class WebSocketConfiguration implements WebSocketConfigurer { |
|
|
public class WebSocketConfiguration implements WebSocketConfigurer { |
|
|
|
|
|
|
|
|
public static final String WS_PLUGIN_PREFIX = "/api/ws/plugins/"; |
|
|
public static final String WS_PLUGIN_PREFIX = "/api/ws/plugins/"; |
|
|
private static final String WS_PLUGIN_MAPPING = WS_PLUGIN_PREFIX + "**"; |
|
|
private static final String WS_PLUGIN_MAPPING = WS_PLUGIN_PREFIX + "**"; |
|
|
|
|
|
|
|
|
|
|
|
private final WebSocketHandler wsHandler; |
|
|
|
|
|
|
|
|
@Bean |
|
|
@Bean |
|
|
public ServletServerContainerFactoryBean createWebSocketContainer() { |
|
|
public ServletServerContainerFactoryBean createWebSocketContainer() { |
|
|
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); |
|
|
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); |
|
|
@ -55,7 +61,11 @@ public class WebSocketConfiguration implements WebSocketConfigurer { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
|
|
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { |
|
|
registry.addHandler(wsHandler(), WS_PLUGIN_MAPPING).setAllowedOriginPatterns("*") |
|
|
if (!(wsHandler instanceof TbWebSocketHandler)) { |
|
|
|
|
|
log.error("TbWebSocketHandler expected but [{}] provided", wsHandler); |
|
|
|
|
|
throw new RuntimeException("TbWebSocketHandler expected but " + wsHandler + " provided"); |
|
|
|
|
|
} |
|
|
|
|
|
registry.addHandler(wsHandler, WS_PLUGIN_MAPPING).setAllowedOriginPatterns("*") |
|
|
.addInterceptors(new HttpSessionHandshakeInterceptor(), new HandshakeInterceptor() { |
|
|
.addInterceptors(new HttpSessionHandshakeInterceptor(), new HandshakeInterceptor() { |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@ -82,11 +92,6 @@ public class WebSocketConfiguration implements WebSocketConfigurer { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
|
public WebSocketHandler wsHandler() { |
|
|
|
|
|
return new TbWebSocketHandler(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected SecurityUser getCurrentUser() throws ThingsboardException { |
|
|
protected SecurityUser getCurrentUser() throws ThingsboardException { |
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
|
if (authentication != null && authentication.getPrincipal() instanceof SecurityUser) { |
|
|
if (authentication != null && authentication.getPrincipal() instanceof SecurityUser) { |
|
|
|