diff --git a/libs/bnsCore.jar b/libs/bnsCore.jar new file mode 100644 index 0000000..62d4697 Binary files /dev/null and b/libs/bnsCore.jar differ diff --git a/pom.xml b/pom.xml index 969b022..5087e85 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,10 @@ 2.2.3 + + org.springframework.boot + spring-boot-starter-websocket + io.netty diff --git a/src/main/java/com/bonus/aqd/manager/common/config/SecurityConfig.java b/src/main/java/com/bonus/aqd/manager/common/config/SecurityConfig.java index f5beb70..2861c68 100644 --- a/src/main/java/com/bonus/aqd/manager/common/config/SecurityConfig.java +++ b/src/main/java/com/bonus/aqd/manager/common/config/SecurityConfig.java @@ -101,7 +101,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() // 不进行权限验证的请求或资源(从配置文件中读取) - .antMatchers("/", "/login/**","/favicon.ico").permitAll() + .antMatchers("/", "/login/**","/favicon.ico","/websocket/**","/ws/**").permitAll() // 其他的需要登陆后才能访问 .anyRequest().authenticated() .and() diff --git a/src/main/java/com/bonus/aqd/websocket/config/WebSocketConfig.java b/src/main/java/com/bonus/aqd/websocket/config/WebSocketConfig.java new file mode 100644 index 0000000..7690440 --- /dev/null +++ b/src/main/java/com/bonus/aqd/websocket/config/WebSocketConfig.java @@ -0,0 +1,46 @@ +package com.bonus.aqd.websocket.config; + +import com.bonus.aqd.websocket.handle.AqdSocketHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.WebSocketHandler; +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.web.socket.server.standard.ServletServerContainerFactoryBean; + +/** + *

+ *

+ * + *

+ * + * @author LiYang + * @createTime 2022年07月19日 9:57 上午 + */ +@Configuration +@EnableWebSocket +public class WebSocketConfig implements WebSocketConfigurer { + + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + // 注册 socket处理器 + registry.addHandler(AqdSocketHandler (), "/ws/aqd").setAllowedOrigins("*"); + } + + @Bean + public ServletServerContainerFactoryBean createWebSocketContainer() { + ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); + // 消息缓冲区大小的大小 + container.setMaxTextMessageBufferSize(8192); + container.setMaxBinaryMessageBufferSize(8192); + return container; + } + + + @Bean + public WebSocketHandler AqdSocketHandler () { + return new AqdSocketHandler(); + } + +} diff --git a/src/main/java/com/bonus/aqd/websocket/handle/AqdSocketHandler.java b/src/main/java/com/bonus/aqd/websocket/handle/AqdSocketHandler.java new file mode 100644 index 0000000..43b5d05 --- /dev/null +++ b/src/main/java/com/bonus/aqd/websocket/handle/AqdSocketHandler.java @@ -0,0 +1,52 @@ +package com.bonus.aqd.websocket.handle; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.socket.*; + + +/** + * 安全带文本socket + * @author 黑子 + */ +public class AqdSocketHandler implements org.springframework.web.socket.WebSocketHandler { + + + private final static Logger LOGGER = LoggerFactory.getLogger(AqdSocketHandler.class); + + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + LOGGER.info("Socket 连接成功,sessionId:{}", session.getId()); + } + + @Override + public void handleMessage(WebSocketSession session, WebSocketMessage message) throws Exception { + if (message instanceof TextMessage) { + handlerTextMessage(session, (TextMessage) message); + } else { + LOGGER.error(" Socket 消息处理失败,只接受 文本消息,sessionId:{}", session.getId()); + } + } + + + public void handlerTextMessage(WebSocketSession session, TextMessage message) throws Exception { + final String msg = message.getPayload(); + LOGGER.info(" Socket 收到消息:{}", msg); + } + + @Override + public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { + LOGGER.error(" Socket 处理异常,sessionId:{}, 异常原因:{}", session.getId(), exception.getMessage()); + } + + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { + LOGGER.info(" Socket 关闭,sessionId:{}", session.getId()); + } + + @Override + public boolean supportsPartialMessages() { + return false; + } + + +}