stylefeng/Guns: Guns
记录日志注解
@BussinessLog(value = "添加部门", key = "simplename", dict = DeptDict.class)
/**
* 日志记录
*
* @author fengshuonan
* @date 2016年12月6日 下午8:48:30
*/
@Aspect
@Component
public class LogAop {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Pointcut(value = "@annotation(cn.stylefeng.guns.core.common.annotion.BussinessLog)")
public void cutService() {
}
@Around("cutService()")
public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
//先执行业务
Object result = point.proceed();
try {
handle(point);
}
catch (Exception e) {
log.error("日志记录出错!", e);
}
return result;
}
private void handle(ProceedingJoinPoint point) throws Exception {
//获取拦截的方法名
Signature sig = point.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Object target = point.getTarget();
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
String methodName = currentMethod.getName();
//如果当前用户未登录,不做日志
ShiroUser user = ShiroKit.getUser();
if (null == user) {
return;
}
//获取拦截方法的参数
String className = point.getTarget().getClass().getName();
Object[] params = point.getArgs();
//获取操作名称
BussinessLog annotation = currentMethod.getAnnotation(BussinessLog.class);
String bussinessName = annotation.value();
String key = annotation.key();
Class dictClass = annotation.dict();
StringBuilder sb = new StringBuilder();
for (Object param : params) {
sb.append(param);
sb.append(" & ");
}
//如果涉及到修改,比对变化
String msg;
if (bussinessName.contains("修改") || bussinessName.contains("编辑")) {
Object obj1 = LogObjectHolder.me().get();
Map<String, String> obj2 = HttpContext.getRequestParameters();
msg = Contrast.contrastObj(dictClass, key, obj1, obj2);
} else {
Map<String, String> parameters = HttpContext.getRequestParameters();
AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance();
msg = Contrast.parseMutiKey(dictMap, key, parameters);
}
//使用线程池,异步记录日志
LogManager.me().executeLog(LogTaskFactory.bussinessLog(user.getId(), bussinessName, className, methodName, msg));
}
}
编辑页面,保存原始对象
/**
* 跳转到编辑管理员页面
*/
@Permission
@RequestMapping( "/user_edit/{userId}" )
public String userEdit( @PathVariable Integer userId, Model model )
{
if ( ToolUtil.isEmpty( userId ) )
{
throw new ServiceException( BizExceptionEnum.REQUEST_NULL );
}
assertAuth( userId );
User user = this.userService.selectById( userId );
model.addAttribute( user );
model.addAttribute( "roleName", ConstantFactory.me().getRoleName( user.getRoleid() ) );
model.addAttribute( "deptName", ConstantFactory.me().getDeptName( user.getDeptid() ) );
//LogObjectHolder就保存原始对象
//Scope 为session,相当于缓存
LogObjectHolder.me().set( user );
return(PREFIX + "user_edit.html");
}