spring websocket 实践

8月 31, 2022 |

示例下载: test-websocket.tar

添加maven 依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-websocket</artifactId>
	<version>2.7.1</version>
</dependency>

websocket nginx 配置

使用UserPrincipal 实现websocket认证

前后端分离使用的是token, 而不是传统的session模式, 所以需要实现HttpServletRequest#getUserPrincipal 让websocket在建立连接的时候能获取到凭证

WebSocket配置

WebSocketHandlerDecorator跟踪用户对应的websocket session

参考文献

websocket.adoc (有可能是github的路径映射改变了,blob路径不可用)

Posted in: spring practise

Comments are closed.