Commit eb198dd0 by weisong
parents 44d42ef9 259394fc
......@@ -111,6 +111,11 @@
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.21</version>
</dependency>
</dependencies>
......
......@@ -33,7 +33,7 @@ public class SysCaptchaServiceImpl implements SysCaptchaService {
// 保存到缓存
String redisKey = RedisKeys.getCaptchaKey(key);
//redisTemplate.opsForValue().set(redisKey, captcha.text(), EXPIRE_MINUTES, TimeUnit.MINUTES);
redisTemplate.opsForValue().set(redisKey, captcha.text(), EXPIRE_MINUTES, TimeUnit.MINUTES);
// 封装返回数据
SysCaptchaVO captchaVO = new SysCaptchaVO();
......
package com.cnooc.expert.system.operatelog.aspect;
import com.alibaba.fastjson.JSON;
import com.cnooc.expert.system.operatelog.dto.LogBody;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.cnooc.expert.common.exception.BusinessException;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@Aspect
@Component
@Order(1)
@Slf4j
public class LogAspectj {
private static final String EXCEPTION_CODE = "500";
private String host="";
@Pointcut("execution(* com.cnooc.expert.controller.auth.*.*(..)) ||"+
"execution(* com.cnooc.expert.controller.expert.*.*(..)) || " +
"execution(* com.cnooc.expert.controller.portal.*.*(..)) || " +
"execution(* com.cnooc.expert.controller.workflow.*.*(..))")
public void loginLog() {
}
/**
* 抛出异常后通知(@AfterThrowing):方法抛出异常退出时执行的通知
* 注意在这里不能使用ProceedingJoinPoint
* 不然会报错ProceedingJoinPoint is only supported for around advice
* throwing注解为错误信息
*
* @param joinPoint
* @param ex
*/
@AfterThrowing(value = "loginLog()", throwing = "ex")
public void afterThrowingMethod(JoinPoint joinPoint, Exception ex) throws Exception {
Map<String,String> resp = new HashMap<>();
if (ex instanceof BusinessException) {
BusinessException BusinessEx = (BusinessException) ex;
resp.put("HttpCode", BusinessEx.getErrorCode()+"");
resp.put("Message", BusinessEx.getMessage());
}else {
resp.put("HttpCode", EXCEPTION_CODE);
resp.put("Message", ex.getMessage());
}
System.out.println("进入afterThrowingMethod方法=========");
this.sendLog( resp);
}
/**
* 返回后通知(@AfterReturning):在某连接点(joinpoint)
* 正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回
* 方法执行完毕之后
* 注意在这里不能使用ProceedingJoinPoint
* 不然会报错ProceedingJoinPoint is only supported for around advice
* crmAspect()指向需要控制的方法
* returning 注解返回值
*
* @param joinPoint
* @param returnValue 返回值
* @throws Exception
*/
@AfterReturning(value = "loginLog()", returning = "returnValue")
public void doAfterReturning(JoinPoint joinPoint, Object returnValue) throws Exception {
System.out.println("进入doAfterReturning方法========");
this.sendLog(returnValue);
}
/**
* 获取当前的request
* 这里如果报空指针异常是因为单独使用spring获取request
* 需要在配置文件里添加监听
* <listener>
* <listener-class>
* org.springframework.web.context.request.RequestContextListener
* </listener-class>
* </listener>
*
* @return
*/
private HttpServletRequest getHttpServletRequest() {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
return request;
}
private String getCurrentUser() {
//这里需要添加代码
return "";
}
/**
* mysql
* 新增 processRecord
*
* @param returnValue
*/
private void sendLog(Object returnValue) {
try {
System.out.println("进入sendLog方法========");
HttpServletRequest httpServletRequest = getHttpServletRequest();
String method = httpServletRequest.getMethod();
String account = getCurrentUser();
LogBody logBody = new LogBody();
Enumeration<String> headers = httpServletRequest.getHeaderNames();
List<String> headerList = Collections.list(headers);
log.info("[sendLog]enter, headerList:{}", headerList);
logBody.setOperationTs(System.currentTimeMillis());
logBody.setRequestMethod(method);
logBody.setIp(this.getRemoteIP(httpServletRequest));
//请求路径:域名+路径
logBody.setRequestPath(host+httpServletRequest.getRequestURI());
if(null != returnValue && JSON.toJSONString(returnValue).length() > 5000){
logBody.setResponseContent("返回消息体大于5000字符");
}else {
logBody.setResponseContent(JSON.toJSONString(returnValue));
}
System.out.println("logBody对象信息如下=========");
System.out.println(logBody);
//将数据发送到kafka,这里需要加代码逻辑
} catch (Exception e) {
e.printStackTrace();
}
}
public String getRemoteIP(HttpServletRequest request) {
if (request == null) {
return "unknown";
}
String ip = request.getHeader("x-Original-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("x-real-ip");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
if (ip.indexOf(":") > 0) {
ip = ip.substring(ip.lastIndexOf(":"));
}
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(",")).trim();
}
return ip;
}
}
package com.cnooc.expert.system.operatelog.dto;
import lombok.Data;
@Data
public class LogBody {
/**
* 业务模块标识
*/
private String appId;
/**
* 业务模块名称
*/
private String appName;
/**
* 用户账号
*/
private String account;
/**
* 用户名称
*/
private String userName;
/**
* 操作所在IP地址(用户操作实际ip非系统代理ip)
*/
private String ip;
/**
* 用户操作时间戳(毫秒)服务器时间
*/
private long operationTs;
/**
* 用户所属单位id(取到部门一级)
*/
private Long domainId;
/**
* 用户所属单位名称(取到部门一级)
*/
private String domainName;
/**
* 请求方法
*/
private String requestMethod;
/**
* 请求路径path
*/
private String requestPath;
/**
* 请求参数
*/
private String requestArgs;
/**
* 返回参数,文件下载无需该字段
*/
private String responseContent;
/**
* 身份类型(内部用户、内部供应商、外部供应商、内部专家、外部专家)
*/
private String role;
/**
* 操作事件类型(1:登录,2:登出,3:添加,4:编辑,5:删除,6:查询,7:下载,8:打印,9:导入,10:导出,11:启动,12:停用,13:其它)
*/
private Integer action;
/**
* 操作记录id,使用UUID.randomUUID().toString()生成
*/
private String operateRecordId;
/**
* imos的用户id
*/
private String userId;
}
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