【发布时间】:2020-03-12 17:23:09
【问题描述】:
我正在运行带有暴露 WebSocket 端点的 Spring Boot@2.2.x 服务器。这是我的WebSocketConfiguration:
@Slf4j
@Configuration
public class WebSocketConfiguration {
private static final String WS_PATH = "/ws/notifications";
@Bean
public HandlerMapping webSocketHandlerMapping() {
Map<String, WebSocketHandler> handlersMap = new HashMap<>();
handlersMap.put(WS_PATH, session -> session.send(session.receive()
.map(WebSocketMessage::getPayloadAsText)
.doOnEach(logNext(log::info))
.map(msg -> format("notification for your msg: %s", msg))
.map(session::textMessage)));
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
handlerMapping.setUrlMap(handlersMap);
return handlerMapping;
}
@Bean
public WebSocketHandlerAdapter handlerAdapter(WebSocketService webSocketService) {
return new WebSocketHandlerAdapter(webSocketService);
}
@Bean
public WebSocketService webSocketService() {
return new HandshakeWebSocketService(new ReactorNettyRequestUpgradeStrategy());
}
}
问题是如何使用Basic Authentication 或Bearer Authentication 或access_token 查询参数来实现建立WS 连接的身份验证?
最好的选择是避免使用 Spring Security。
谢谢。
【问题讨论】:
标签: spring-boot websocket spring-webflux spring-websocket java-websocket