Commit 122deff1 authored by zengtianlai3's avatar zengtianlai3

微调

parent 6123e245
...@@ -15,7 +15,7 @@ public class CorsConfig implements WebMvcConfigurer { ...@@ -15,7 +15,7 @@ public class CorsConfig implements WebMvcConfigurer {
AuthenticationInterceptor authenticationInterceptor; AuthenticationInterceptor authenticationInterceptor;
@Override @Override
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") registry.addMapping("/**")
.allowedOrigins("*") .allowedOrigins("*")
.allowedMethods("*") .allowedMethods("*")
......
...@@ -58,7 +58,7 @@ public class OperateSAMStatusResponseEventHandler { ...@@ -58,7 +58,7 @@ public class OperateSAMStatusResponseEventHandler {
samMonitor.setOnlineCount(onlineCount); samMonitor.setOnlineCount(onlineCount);
samMonitor.setSamCount(samCount); samMonitor.setSamCount(samCount);
operateManager.putSamMonitorMap(serverIp, samMonitor); operateManager.putSamMonitorMap(serverIp, samMonitor);
if (onlineCount / samCount > 0.7) { if ((float)onlineCount / samCount > 0.7) {
int typeId = 3; int typeId = 3;
String title = "SAM不足"; String title = "SAM不足";
String content = "当前在线客户端已超过70%"; String content = "当前在线客户端已超过70%";
......
...@@ -32,7 +32,6 @@ public class LogAspect { ...@@ -32,7 +32,6 @@ public class LogAspect {
@Pointcut("@annotation(iot.sixiang.license.log.MyLog)") @Pointcut("@annotation(iot.sixiang.license.log.MyLog)")
public void logPointCut() { public void logPointCut() {
log.info("------>配置织入点");
} }
/** /**
...@@ -40,9 +39,9 @@ public class LogAspect { ...@@ -40,9 +39,9 @@ public class LogAspect {
* *
* @param joinPoint 切点 * @param joinPoint 切点
*/ */
@AfterReturning(pointcut = "logPointCut()", returning = "jsonResult") @AfterReturning(pointcut = "logPointCut()")
public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) { public void doAfterReturning(JoinPoint joinPoint) {
handleLog(joinPoint, null, jsonResult); handleLog(joinPoint, null);
} }
/** /**
...@@ -53,53 +52,43 @@ public class LogAspect { ...@@ -53,53 +52,43 @@ public class LogAspect {
*/ */
@AfterThrowing(value = "logPointCut()", throwing = "e") @AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e) { public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
handleLog(joinPoint, e, null); handleLog(joinPoint, e);
} }
protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) { private void handleLog(final JoinPoint joinPoint, final Exception e) {
// 获得注解 // 获得MyLog注解
MyLog controllerLog = getAnnotationLog(joinPoint); MyLog controllerLog = getAnnotationLog(joinPoint);
if (controllerLog == null) { if (controllerLog == null) {
return; return;
} }
SysOperLog operLog = new SysOperLog(); SysOperLog operLog = new SysOperLog();
// 操作状态(0正常 1异常)
operLog.setStatus(0); operLog.setStatus(0);
// 操作时间
operLog.setOperTime(new Date()); operLog.setOperTime(new Date());
if (e != null) { if (e != null) {
operLog.setStatus(1); operLog.setStatus(1);
// IotLicenseException为本系统自定义的异常类,读者若要获取异常信息,请根据自身情况变通
operLog.setErrorMsg(StringUtils.substring(((IotLicenseException) e).getMsg(), 0, 2000)); operLog.setErrorMsg(StringUtils.substring(((IotLicenseException) e).getMsg(), 0, 2000));
} }
// UserUtils.getUri();获取方法上的路径 如:/login,本文实现方法如下:
// 1、在拦截器中 String uri = request.getRequestURI();
// 2、用ThreadLocal存放uri,UserUtils.setUri(uri);
// 3、UserUtils.getUri();
String uri = UserUtils.getUri(); String uri = UserUtils.getUri();
operLog.setUri(uri); operLog.setUri(uri);
// operLog.setMethod(className + "." + methodName + "()"); // 处理注解上的参数
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog); getControllerMethodDescription(joinPoint, controllerLog, operLog);
// 保存数据库 // 保存数据库
sysOperLogService.addOperlog(operLog.getTitle(), operLog.getBusinessType(), operLog.getUri(), operLog.getStatus(), operLog.getOptParam(), operLog.getErrorMsg(), operLog.getOperTime()); sysOperLogService.addOperlog(operLog.getTitle(), operLog.getBusinessType(), operLog.getUri(), operLog.getStatus(), operLog.getOptParam(), operLog.getErrorMsg(), operLog.getOperTime());
} }
/** /**
* 获取注解中对方法的描述信息 用于Controller层注解 * 是否存在注解,如果存在就获取,不存在则返回null
* * @param joinPoint
* @param myLog 日志 * @return
* @param operLog 操作日志
* @throws Exception
*/
public void getControllerMethodDescription(JoinPoint joinPoint, MyLog myLog, SysOperLog operLog) {
// 设置action动作
operLog.setBusinessType(myLog.businessType().ordinal());
// 设置标题
operLog.setTitle(myLog.title());
String optParam = getAnnotationValue(joinPoint, myLog.optParam());
operLog.setOptParam(optParam);
}
/**
* 是否存在注解,如果存在就获取
*/ */
private MyLog getAnnotationLog(JoinPoint joinPoint) { private MyLog getAnnotationLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature(); Signature signature = joinPoint.getSignature();
...@@ -111,13 +100,36 @@ public class LogAspect { ...@@ -111,13 +100,36 @@ public class LogAspect {
return null; return null;
} }
public String getAnnotationValue(JoinPoint joinPoint, String name) { /**
* 获取Controller层上MyLog注解中对方法的描述信息
* @param joinPoint 切点
* @param myLog 自定义的注解
* @param operLog 操作日志实体类
*/
private void getControllerMethodDescription(JoinPoint joinPoint, MyLog myLog, SysOperLog operLog) {
// 设置业务类型(0其它 1新增 2修改 3删除)
operLog.setBusinessType(myLog.businessType().ordinal());
// 设置模块标题,eg:登录
operLog.setTitle(myLog.title());
// 对方法上的参数进行处理,处理完:userName=xxx,password=xxx
String optParam = getAnnotationValue(joinPoint, myLog.optParam());
operLog.setOptParam(optParam);
}
/**
* 对方法上的参数进行处理
* @param joinPoint
* @param name
* @return
*/
private String getAnnotationValue(JoinPoint joinPoint, String name) {
String paramName = name; String paramName = name;
// 获取方法中所有的参数 // 获取方法中所有的参数
Map<String, Object> params = getParams(joinPoint); Map<String, Object> params = getParams(joinPoint);
// 参数是否是动态的:#{paramName} // 参数是否是动态的:#{paramName}
if (paramName.matches("^#\\{\\D*\\}")) { if (paramName.matches("^#\\{\\D*\\}")) {
// 获取参数名 // 获取参数名,去掉#{ }
paramName = paramName.replace("#{", "").replace("}", ""); paramName = paramName.replace("#{", "").replace("}", "");
// 是否是复杂的参数类型:对象.参数名 // 是否是复杂的参数类型:对象.参数名
if (paramName.contains(".")) { if (paramName.contains(".")) {
...@@ -128,35 +140,39 @@ public class LogAspect { ...@@ -128,35 +140,39 @@ public class LogAspect {
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(object); JSONObject jsonObject = (JSONObject) JSONObject.toJSON(object);
// 获取值 // 获取值
Object o = jsonObject.get(split[1]); Object o = jsonObject.get(split[1]);
return String.valueOf(o); return String.valueOf(o);
} else {// 简单的动态参数直接返回 } else {// 简单的动态参数直接返回
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
String[] paraNames = paramName.split(","); String[] paraNames = paramName.split(",");
for (int i = 0; i < paraNames.length; i++) { for (String paraName : paraNames) {
String paraName = paraNames[i];
String val = String.valueOf(getValue(params, paraName));
str.append(paraName + "=" + val + ",");
String val = String.valueOf(getValue(params, paraName));
// 组装成 userName=xxx,password=xxx,
str.append(paraName).append("=").append(val).append(",");
} }
// 去掉末尾的,
if (str.toString().endsWith(",")) { if (str.toString().endsWith(",")) {
String substring = str.substring(0, str.length() - 1); String substring = str.substring(0, str.length() - 1);
return substring; return substring;
} else { } else {
return str.toString(); return str.toString();
} }
} }
} }
// 非动态参数直接返回 // 非动态参数直接返回
return name; return name;
} }
/**
* 获取方法上的所有参数,返回Map类型, eg: 键:"userName",值:xxx 键:"password",值:xxx
* @param joinPoint
* @return
*/
public Map<String, Object> getParams(JoinPoint joinPoint) { public Map<String, Object> getParams(JoinPoint joinPoint) {
Map<String, Object> params = new HashMap<>(8); Map<String, Object> params = new HashMap<>(8);
// 通过切点获取方法所有参数值["zhangsan", "123456"]
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
// 通过切点获取方法所有参数名 eg:["userName", "password"]
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String[] names = signature.getParameterNames(); String[] names = signature.getParameterNames();
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
...@@ -165,7 +181,13 @@ public class LogAspect { ...@@ -165,7 +181,13 @@ public class LogAspect {
return params; return params;
} }
public Object getValue(Map<String, Object> map, String paramName) { /**
* 从map中获取键为paramName的值,不存在放回null
* @param map
* @param paramName
* @return
*/
private Object getValue(Map<String, Object> map, String paramName) {
for (Map.Entry<String, Object> entry : map.entrySet()) { for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().equals(paramName)) { if (entry.getKey().equals(paramName)) {
return entry.getValue(); return entry.getValue();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment