由于在权限方面我是用了shiro进行管理,所以我可以通过shiro的subject获得到我需求的“人名”
String username = ((MemberEntity) SecurityUtils.getSubject().getPrincipal()).getName();
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod();
if ("新增合同".equals(syslog.value())) {
//处理参数
Object[] args = joinPoint.getArgs();
//处理为Json字符串
String params = new Gson().toJson(args);
//处理字符串的[]
String replace = params.replace("[", "");
String s = replace.replace("]", "");
System.out.println(s);
//处理为Json对象
JSONObject jsonObject = JSON.parseObject(s);
//得到合同ID号
Object contractId = jsonObject.get("contractId");
//得到炼厂计划ID
Object filiale_plan_id = jsonObject.get("filialePlanId");
Integer ID = Integer.valueOf(String.valueOf(filiale_plan_id));
//得到合同号
Object contracts = jsonObject.get("contracts");
//得到合同价格
Object agreement_price = jsonObject.get("agreementPrice");
//得到资源大体流向
Object resource_stream = jsonObject.get("resourceStream");
log.append(username).append("新增了合同,合同号为:").append(contracts).append(",合同价格为:").append(agreement_price);
sysLog.setParams(log.toString());
sysLog.setFilialePlanId(ID);
}
案例代码:
@Aspect
@Component
public class SysLogAspect {
@Pointcut("@annotation(io.ref.common.annotation.SysLog)")
public void logPointCut() {
}
@Before("logPointCut()")
public Object before(JoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
//执行方法
Object result = point.getTarget();
//执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
//保存日志
saveSysLog(point, time);
return result;
}
private void saveSysLog(JoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SysLogEntity sysLog = new SysLogEntity();
SysLog syslog = method.getAnnotation(SysLog.class);
if(syslog != null){
//注解上的描述
sysLog.setOperation(syslog.value());
}
//获取request
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
//设置IP地址
sysLog.setIp(IPUtils.getIpAddr(request));
//用户名
String username = ((MemberEntity) SecurityUtils.getSubject().getPrincipal()).getName();
sysLog.setName(username);
原文:https://www.cnblogs.com/SmartCat994/p/13023268.html