优化平板连接websocket
This commit is contained in:
@@ -21,7 +21,7 @@ public class WebSocketConfig implements WebSocketConfigurer {
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(webSocketHandler, "/ws") // 客户端连接端点
|
||||
registry.addHandler(webSocketHandler, "/ws/tablet_ws") // 客户端连接端点
|
||||
.setAllowedOrigins("*") // 允许跨域
|
||||
.addInterceptors(new AuthHandshakeInterceptor(jwtUtil)); // 握手拦截器(如JWT校验)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ public class AuthHandshakeInterceptor implements HandshakeInterceptor {
|
||||
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
|
||||
String authHeader = request.getHeaders().getFirst("Authorization");
|
||||
String deviceId = request.getHeaders().getFirst("Device-ID");
|
||||
String userId = request.getHeaders().getFirst("User-ID");
|
||||
|
||||
if (request instanceof ServletServerHttpRequest) {
|
||||
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
|
||||
@@ -32,13 +31,14 @@ public class AuthHandshakeInterceptor implements HandshakeInterceptor {
|
||||
String sn = httpRequest.getParameter("sn");
|
||||
attributes.put("sn", sn);
|
||||
log.info("Intercepted - sn: " + sn);
|
||||
}
|
||||
|
||||
if (authHeader == null || !authHeader.startsWith("Bearer ") || userId == null) {
|
||||
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
||||
return false;
|
||||
}
|
||||
String token = authHeader.substring(7); // 去掉 "Bearer " 前缀
|
||||
return jwtUtil.validateAccessToken(userId, token, deviceId); // 自定义校验逻辑
|
||||
return jwtUtil.validateDeviceToken(token, deviceId, sn); // 自定义校验逻辑
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user