fix: 去除聊天室的token校验, 自由使用

This commit is contained in:
republicline 2024-10-15 16:19:51 +08:00
parent 419f9e20d1
commit 793e3e014d
3 changed files with 54 additions and 43 deletions

View File

@ -32,13 +32,16 @@ public class ChatHandler implements WebSocketHandler {
@Override @Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
String decode = URLDecoder.decode(session.getUri().getQuery()); String query = session.getUri().getQuery();
String decode = URLDecoder.decode(query);
// System.out.println("query = " + query);
Map params = GetHttpParamUtil.getParams(decode); Map params = GetHttpParamUtil.getParams(decode);
String token = (String) params.get("token"); String token = (String) params.get("token");
// String token = "azIc_An5dRViceuMN5B5_S-k-1YH8gTCkzc9A6d8mJYguTOEOXWHLaojcpqNNrhYG_9QSUV_y9LpYCN01SqY-GHcXJoMEqCxU-3Qudvkizll3T6mOWK-MJDODMZHETGj";
OAuth2Authorization authorization = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN); OAuth2Authorization authorization = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN);
if (authorization != null) { if (authorization != null) {
String username = authorization.getPrincipalName(); String username = authorization.getPrincipalName();
// System.out.println("username = " + username);
String payload = (String) message.getPayload(); String payload = (String) message.getPayload();
JSONObject jsonObject = JSONObject.parseObject(payload); JSONObject jsonObject = JSONObject.parseObject(payload);

View File

@ -157,17 +157,17 @@ public class ChatServiceImpl implements ChatService {
} }
@Override @Override
public void sendMessageMysql(String username, String patientName, String idNum, String date, WebSocketSession session, String msg) { public void sendMessageMysql(String username, String patientName, String idNum, String date, WebSocketSession session, String msg) throws SQLException {
CustomDataSource dataSource = datasourceMap.get(session.getId()); CustomDataSource dataSource = datasourceMap.get(session.getId());
// String databaseName = patientName + idNum; // String databaseName = patientName + idNum;
String databaseName = idNum; String databaseName = idNum;
ArrayList<Map> history = new ArrayList<>(); ArrayList<Map> history = new ArrayList<>();
if (dataSource == null) { if (dataSource == null) {
dataSource = new MySQLSource(mysqlHost, mysqlPassword, mysqlUsername, databaseName); dataSource = new MySQLSource(mysqlHost, mysqlPassword, mysqlUsername, databaseName);
boolean status = dataSource.open(); boolean status = dataSource.open();
if (status) { if (status) {
datasourceMap.put(session.getId(), dataSource); datasourceMap.put(session.getId(), dataSource);
sessionDatabaseMap.put(session.getId(), databaseName); sessionDatabaseMap.put(session.getId(), databaseName);
@ -183,26 +183,30 @@ public class ChatServiceImpl implements ChatService {
} }
String sql = "select content, create_time \"creatTime\", create_user \"createUser\", create_name \"createName\" from t_chat where deleted = 0 and revoked = 0 order by create_time asc ";
Connection connection = dataSource.getConnection(); Connection connection = dataSource.getConnection();
try { DatabaseMetaData metaData = connection.getMetaData();
Statement statement = connection.createStatement(); String tableName = "t_chat";
ResultSet resultSet = statement.executeQuery(sql); ResultSet tablesx = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
while (resultSet.next()) { String sql = "select content, create_time \"creatTime\", create_user \"createUser\", create_name \"createName\" from t_chat where deleted = 0 and revoked = 0 order by create_time asc ";
Map map = new HashMap(); if (tablesx.next()) {
map.put("content", resultSet.getString("content")); try {
map.put("creatTime", resultSet.getString("creatTime")); Statement statement = connection.createStatement();
map.put("createUser", resultSet.getString("createUser")); ResultSet resultSet = statement.executeQuery(sql);
map.put("createName", resultSet.getString("createName")); while (resultSet.next()) {
history.add(map); Map map = new HashMap();
map.put("content", resultSet.getString("content"));
map.put("creatTime", resultSet.getString("creatTime"));
map.put("createUser", resultSet.getString("createUser"));
map.put("createName", resultSet.getString("createName"));
history.add(map);
}
JSONObject param = new JSONObject();
param.put("history", history);
param.put("msgType", "msg");
session.sendMessage(new TextMessage(param.toJSONString().getBytes()));
} catch (Exception e) {
e.printStackTrace();
} }
JSONObject param = new JSONObject();
param.put("history", history);
param.put("msgType", "msg");
session.sendMessage(new TextMessage(param.toJSONString().getBytes()));
} catch (Exception e) {
e.printStackTrace();
} }
} else { } else {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
@ -221,27 +225,31 @@ public class ChatServiceImpl implements ChatService {
if (StringUtils.hasText(msg)) { if (StringUtils.hasText(msg)) {
JSONObject param = new JSONObject(); JSONObject param = new JSONObject();
SysUser sysUser = SysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
Connection connection = dataSource.getConnection(); Connection connection = dataSource.getConnection();
try { DatabaseMetaData metaData = connection.getMetaData();
Statement statement = connection.createStatement(); String tableName = "t_chat";
String sql = "CREATE TABLE `t_chat` (\n" + ResultSet tablesx = metaData.getTables(null, null, tableName, new String[]{"TABLE"});
" `id` int NOT NULL AUTO_INCREMENT,\n" + System.out.println("tablesx = " + tablesx.next());
" `content` varchar(5000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" + if (!tablesx.next()) {
" `create_time` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" + try {
" `create_user` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" + Statement statement = connection.createStatement();
" `create_name` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" + String sql = "CREATE TABLE `t_chat` (\n" +
" `deleted` bit(1) NULL DEFAULT NULL,\n" + " `id` int NOT NULL AUTO_INCREMENT,\n" +
" `revoked` bit(1) NULL DEFAULT NULL,\n" + " `content` varchar(5000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) USING BTREE\n" + " `create_time` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;"; " `create_user` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
statement.execute(sql); " `create_name` varchar(600) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,\n" +
} catch (SQLException e) { " `deleted` bit(1) NULL DEFAULT NULL,\n" +
e.printStackTrace(); " `revoked` bit(1) NULL DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) USING BTREE\n" +
") ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;";
statement.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
} }
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
try { try {
preparedStatement = connection.prepareStatement("INSERT INTO t_chat (content, create_time, create_user, create_name, deleted, revoked) VALUES (?, ?, ?, ?, ?, ?)"); preparedStatement = connection.prepareStatement("INSERT INTO t_chat (content, create_time, create_user, create_name, deleted, revoked) VALUES (?, ?, ?, ?, ?, ?)");
@ -252,9 +260,9 @@ public class ChatServiceImpl implements ChatService {
param.put("createTime", now); param.put("createTime", now);
preparedStatement.setString(3, username); preparedStatement.setString(3, username);
param.put("createUser", username); param.put("createUser", username);
String name = sysUser.getName(); // String name = sysUser.getName();
preparedStatement.setString(4, name); preparedStatement.setString(4, username);
param.put("createName", name); param.put("createName", username);
preparedStatement.setInt(5, 0); preparedStatement.setInt(5, 0);
preparedStatement.setInt(6, 0); preparedStatement.setInt(6, 0);
param.put("msgType", "msg"); param.put("msgType", "msg");

View File

@ -100,7 +100,7 @@ mybatis-plus:
jdbc-type-for-null: 'null' jdbc-type-for-null: 'null'
call-setters-on-nulls: true call-setters-on-nulls: true
shrink-whitespaces-in-sql: true shrink-whitespaces-in-sql: true
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
--- ---
spring: spring: