mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 优化 webscoket 配置与异常拦截
This commit is contained in:
		@@ -16,15 +16,11 @@ import org.springframework.core.task.VirtualThreadTaskExecutor;
 | 
			
		||||
@AutoConfiguration
 | 
			
		||||
public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 设置 Undertow 的 websocket 缓冲池
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void customize(UndertowServletWebServerFactory factory) {
 | 
			
		||||
        // 默认不直接分配内存 如果项目中使用了 websocket 建议直接分配
 | 
			
		||||
        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
 | 
			
		||||
            WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
 | 
			
		||||
            webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 512));
 | 
			
		||||
            webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(true, 1024));
 | 
			
		||||
            deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
 | 
			
		||||
            // 使用虚拟线程
 | 
			
		||||
            if (SpringUtils.isVirtual()) {
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,7 @@ public class PlusWebSocketInterceptor implements HandshakeInterceptor {
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
 | 
			
		||||
        try {
 | 
			
		||||
            // 检查是否登录 是否有token
 | 
			
		||||
            LoginUser loginUser = LoginHelper.getLoginUser();
 | 
			
		||||
 | 
			
		||||
@@ -52,6 +53,10 @@ public class PlusWebSocketInterceptor implements HandshakeInterceptor {
 | 
			
		||||
 | 
			
		||||
            attributes.put(LOGIN_USER_KEY, loginUser);
 | 
			
		||||
            return true;
 | 
			
		||||
        } catch (NotLoginException e) {
 | 
			
		||||
            log.error("WebSocket 认证失败'{}',无法访问系统资源", e.getMessage());
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user