mirror of
https://gitee.com/republicline/rax-remote-v2.git
synced 2025-08-24 05:04:57 +08:00
add: 切换医院,展示不同医院的数据, todo: 日志模块的功能, 记录日志信息,包含ws协议中的连接和异常等
This commit is contained in:
parent
50d51b5f13
commit
33821e5af5
|
@ -0,0 +1,47 @@
|
||||||
|
package com.rax.admin.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import com.rax.admin.immu.RoleRecord;
|
||||||
|
import com.rax.common.core.constant.CacheConstants;
|
||||||
|
import com.rax.common.core.util.RedisUtils;
|
||||||
|
import com.rax.common.security.service.RaxUser;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* project_name:remote-control-backend
|
||||||
|
* time:2024/9/2 14:05
|
||||||
|
*/
|
||||||
|
public class AuthUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户的当前医院信息
|
||||||
|
*/
|
||||||
|
public static Long getCurrentHospital(RaxUser user) {
|
||||||
|
String key = CacheConstants.CURRENT_HOSPITAL + ":" + user.getId();
|
||||||
|
if (RedisUtils.hasKey(key)) {
|
||||||
|
return Convert.toLong(RedisUtils.get(key));
|
||||||
|
}else {
|
||||||
|
RedisUtils.set(key, user.getHospitalId());
|
||||||
|
return user.getHospitalId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前用户是否拥有,adminRole权限
|
||||||
|
*/
|
||||||
|
public static boolean authAdmin(RaxUser user) {
|
||||||
|
List<String> roleCodeList = user.getRoleCodeList();
|
||||||
|
return roleCodeList.contains(RoleRecord.ADMIN_ROLE_CODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断用户的角色标识信息
|
||||||
|
*/
|
||||||
|
public static boolean hasRoleCode(RaxUser user, String roleCode) {
|
||||||
|
List<String> roleCodeList = user.getRoleCodeList();
|
||||||
|
return roleCodeList.contains(roleCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
package com.rax.vital.common.util;
|
||||||
|
|
||||||
|
import com.rax.admin.api.entity.SysLog;
|
||||||
|
import com.rax.common.security.service.RaxUser;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
|
import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
||||||
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
||||||
|
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
||||||
|
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* project_name:remote-control-backend
|
||||||
|
* time:2024/9/3 15:17
|
||||||
|
* 根据解析到的Token, 构造一个日志对象, 添加日志表中
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SysLoggerBuilder {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OAuth2AuthorizationService authorizationService;
|
||||||
|
|
||||||
|
|
||||||
|
public SysLog buildSysLog(String token, String title, String logType, WebSocketSession session) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
// 从session中获取一些信息
|
||||||
|
String ip = session.getRemoteAddress() != null ? session.getRemoteAddress().toString() : "unknown";
|
||||||
|
String uri = session.getUri() != null ? session.getUri().toString() : "unknown";
|
||||||
|
String userAgent = session.getHandshakeHeaders().getFirst("User-Agent") != null ?
|
||||||
|
session.getHandshakeHeaders().getFirst("User-Agent") : "unknown";
|
||||||
|
String params = session.getUri() != null ?
|
||||||
|
session.getUri().getQuery() : "unknown";
|
||||||
|
|
||||||
|
// 解析token
|
||||||
|
OAuth2Authorization byToken = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN);
|
||||||
|
Map<String, Object> stringObjectMap = parseToken(token);
|
||||||
|
Long hospitalId = (Long) stringObjectMap.get("hospitalId");
|
||||||
|
String userName = (String) stringObjectMap.get("userName");
|
||||||
|
|
||||||
|
|
||||||
|
// 填充属性
|
||||||
|
SysLog sysLog = new SysLog();
|
||||||
|
sysLog.setLogType(logType);
|
||||||
|
sysLog.setTitle(title);
|
||||||
|
sysLog.setCreateTime(LocalDateTime.now());
|
||||||
|
sysLog.setRemoteAddr(ip);
|
||||||
|
sysLog.setDelFlag("0");
|
||||||
|
sysLog.setParams(params);
|
||||||
|
sysLog.setUserAgent(userAgent);
|
||||||
|
sysLog.setRequestUri(uri);
|
||||||
|
sysLog.setHospitalId(hospitalId);
|
||||||
|
sysLog.setCreateBy(userName);
|
||||||
|
return sysLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SysLog buildSysLog(String title, String logType, ServerHttpRequest request) {
|
||||||
|
|
||||||
|
// 获取信息
|
||||||
|
HttpHeaders headers = request.getHeaders();
|
||||||
|
String req_url = headers.getFirst("origin");
|
||||||
|
String agent = headers.getFirst("User-Agent");
|
||||||
|
|
||||||
|
// 获取解析token
|
||||||
|
String query = request.getURI().getQuery();
|
||||||
|
String token = GetHttpParamUtil.getParam(query, "token");
|
||||||
|
Map<String, Object> stringObjectMap = parseToken(token);
|
||||||
|
|
||||||
|
String userName = (String) stringObjectMap.get("userName");
|
||||||
|
Long hospitalId = (Long) stringObjectMap.get("hospitalId");
|
||||||
|
|
||||||
|
// 填充属性
|
||||||
|
SysLog sysLog = new SysLog();
|
||||||
|
sysLog.setLogType(logType);
|
||||||
|
sysLog.setTitle(title);
|
||||||
|
sysLog.setDelFlag("0");
|
||||||
|
sysLog.setCreateBy(userName);
|
||||||
|
sysLog.setParams(query);
|
||||||
|
sysLog.setRemoteAddr(req_url);
|
||||||
|
sysLog.setMethod("ws");
|
||||||
|
sysLog.setHospitalId(hospitalId);
|
||||||
|
sysLog.setCreateTime(LocalDateTime.now());
|
||||||
|
return sysLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Map<String,Object> parseToken(String token) {
|
||||||
|
HashMap<String, Object> map = new HashMap<>();
|
||||||
|
OAuth2Authorization byToken = authorizationService.findByToken(token, OAuth2TokenType.ACCESS_TOKEN);
|
||||||
|
String userName = byToken.getPrincipalName();
|
||||||
|
map.put("userName", userName);
|
||||||
|
OAuth2Authorization.Token<OAuth2AccessToken> accessToken = byToken.getAccessToken();
|
||||||
|
RaxUser o = (RaxUser) accessToken.getClaims().get("user_info");
|
||||||
|
Long hospitalId = o.getHospitalId();
|
||||||
|
map.put("hospitalId", hospitalId);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user