Web-Socket 鉴权示例

This commit is contained in:
click33
2022-02-11 02:57:11 +08:00
parent fbc139b430
commit 40a5cdc6dc
16 changed files with 700 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package com.pj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import cn.dev33.satoken.SaManager;
/**
* Sa-Token 整合 WebSocket 鉴权示例
* @author kong
*
*/
@SpringBootApplication
public class SaTokenWebSocketSpringApplication {
/*
* 1、访问登录接口拿到会话Token
* http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
*
* 2、找一个WebSocket在线测试页面进行连接
* 例如:
* https://www.bejson.com/httputil/websocket/
* 然后连接地址:
* ws://localhost:8081/ws-connect?satoken=2e6db38f-1e78-40bc-aa8f-e8f1f77fbef5
*/
public static void main(String[] args) {
SpringApplication.run(SaTokenWebSocketSpringApplication.class, args);
System.out.println("\n启动成功Sa-Token配置如下" + SaManager.getConfig());
}
}

View File

@@ -0,0 +1,48 @@
package com.pj.test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
/**
* 登录测试
* @author kong
*
*/
@RestController
@RequestMapping("/acc/")
public class LoginController {
// 测试登录 ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
@RequestMapping("doLogin")
public SaResult doLogin(String name, String pwd) {
// 此处仅作模拟示例,真实项目需要从数据库中查询数据进行比对
if("zhang".equals(name) && "123456".equals(pwd)) {
StpUtil.login(10001);
return SaResult.ok("登录成功").set("token", StpUtil.getTokenValue());
}
return SaResult.error("登录失败");
}
// 查询登录状态 ---- http://localhost:8081/acc/isLogin
@RequestMapping("isLogin")
public SaResult isLogin() {
return SaResult.ok("是否登录:" + StpUtil.isLogin());
}
// 查询 Token 信息 ---- http://localhost:8081/acc/tokenInfo
@RequestMapping("tokenInfo")
public SaResult tokenInfo() {
return SaResult.data(StpUtil.getTokenInfo());
}
// 测试注销 ---- http://localhost:8081/acc/logout
@RequestMapping("logout")
public SaResult logout() {
StpUtil.logout();
return SaResult.ok();
}
}

View File

@@ -0,0 +1,83 @@
package com.pj.ws;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
/**
* 处理 WebSocket 连接
*
* @author kong
* @date: 2022-2-11
*/
public class MyWebSocketHandler extends TextWebSocketHandler {
/**
* 固定前缀
*/
private static final String USER_ID = "user_id_";
/**
* 存放Session集合方便推送消息
*/
private static ConcurrentHashMap<String, WebSocketSession> webSocketSessionMaps = new ConcurrentHashMap<>();
// 监听:连接开启
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
// put到集合方便后续操作
String userId = session.getAttributes().get("userId").toString();
webSocketSessionMaps.put(USER_ID + userId, session);
// 给个提示
String tips = "Web-Socket 连接成功sid=" + session.getId() + "userId=" + userId;
System.out.println(tips);
sendMessage(session, tips);
}
// 监听:连接关闭
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
// 从集合移除
String userId = session.getAttributes().get("userId").toString();
webSocketSessionMaps.remove(USER_ID + userId);
// 给个提示
String tips = "Web-Socket 连接关闭sid=" + session.getId() + "userId=" + userId;
System.out.println(tips);
}
// 收到消息
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
System.out.println("sid为" + session.getId() + ",发来:" + message);
}
// -----------
// 向指定客户端推送消息
public static void sendMessage(WebSocketSession session, String message) {
try {
System.out.println("向sid为" + session.getId() + ",发送:" + message);
session.sendMessage(new TextMessage(message));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// 向指定用户推送消息
public static void sendMessage(long userId, String message) {
WebSocketSession session = webSocketSessionMaps.get(USER_ID + userId);
if(session != null) {
sendMessage(session, message);
}
}
}

View File

@@ -0,0 +1,30 @@
package com.pj.ws;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
/**
* WebSocket 相关配置
*
* @author kong
* @date: 2022-2-11
*/
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
// 注册 WebSocket 处理器
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
webSocketHandlerRegistry
// WebSocket 连接处理器
.addHandler(new MyWebSocketHandler(), "/ws-connect")
// WebSocket 拦截器
.addInterceptors(new WebSocketInterceptor())
// 允许跨域
.setAllowedOrigins("*");
}
}

View File

@@ -0,0 +1,45 @@
package com.pj.ws;
import java.util.Map;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import cn.dev33.satoken.stp.StpUtil;
/**
* WebSocket 握手的前置拦截器
*
* @author kong
* @date: 2022-2-11
*/
public class WebSocketInterceptor implements HandshakeInterceptor {
// 握手之前触发 (return true 才会握手成功 )
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler,
Map<String, Object> attr) {
System.out.println("---- 握手之前触发 " + StpUtil.getTokenValue());
// 未登录情况下拒绝握手
if(StpUtil.isLogin() == false) {
System.out.println("---- 未授权客户端,连接失败");
return false;
}
// 标记 userId握手成功
attr.put("userId", StpUtil.getLoginIdAsLong());
return true;
}
// 握手之后触发
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception exception) {
System.out.println("---- 握手之后触发 ");
}
}