stylefeng/Guns: Guns
    记录日志注解

    1. @BussinessLog(value = "添加部门", key = "simplename", dict = DeptDict.class)
    1. /**
    2. * 日志记录
    3. *
    4. * @author fengshuonan
    5. * @date 2016年12月6日 下午8:48:30
    6. */
    7. @Aspect
    8. @Component
    9. public class LogAop {
    10. private Logger log = LoggerFactory.getLogger(this.getClass());
    11. @Pointcut(value = "@annotation(cn.stylefeng.guns.core.common.annotion.BussinessLog)")
    12. public void cutService() {
    13. }
    14. @Around("cutService()")
    15. public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
    16. //先执行业务
    17. Object result = point.proceed();
    18. try {
    19. handle(point);
    20. }
    21. catch (Exception e) {
    22. log.error("日志记录出错!", e);
    23. }
    24. return result;
    25. }
    26. private void handle(ProceedingJoinPoint point) throws Exception {
    27. //获取拦截的方法名
    28. Signature sig = point.getSignature();
    29. MethodSignature msig = null;
    30. if (!(sig instanceof MethodSignature)) {
    31. throw new IllegalArgumentException("该注解只能用于方法");
    32. }
    33. msig = (MethodSignature) sig;
    34. Object target = point.getTarget();
    35. Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
    36. String methodName = currentMethod.getName();
    37. //如果当前用户未登录,不做日志
    38. ShiroUser user = ShiroKit.getUser();
    39. if (null == user) {
    40. return;
    41. }
    42. //获取拦截方法的参数
    43. String className = point.getTarget().getClass().getName();
    44. Object[] params = point.getArgs();
    45. //获取操作名称
    46. BussinessLog annotation = currentMethod.getAnnotation(BussinessLog.class);
    47. String bussinessName = annotation.value();
    48. String key = annotation.key();
    49. Class dictClass = annotation.dict();
    50. StringBuilder sb = new StringBuilder();
    51. for (Object param : params) {
    52. sb.append(param);
    53. sb.append(" & ");
    54. }
    55. //如果涉及到修改,比对变化
    56. String msg;
    57. if (bussinessName.contains("修改") || bussinessName.contains("编辑")) {
    58. Object obj1 = LogObjectHolder.me().get();
    59. Map<String, String> obj2 = HttpContext.getRequestParameters();
    60. msg = Contrast.contrastObj(dictClass, key, obj1, obj2);
    61. } else {
    62. Map<String, String> parameters = HttpContext.getRequestParameters();
    63. AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance();
    64. msg = Contrast.parseMutiKey(dictMap, key, parameters);
    65. }
    66. //使用线程池,异步记录日志
    67. LogManager.me().executeLog(LogTaskFactory.bussinessLog(user.getId(), bussinessName, className, methodName, msg));
    68. }
    69. }

    编辑页面,保存原始对象

    1. /**
    2. * 跳转到编辑管理员页面
    3. */
    4. @Permission
    5. @RequestMapping( "/user_edit/{userId}" )
    6. public String userEdit( @PathVariable Integer userId, Model model )
    7. {
    8. if ( ToolUtil.isEmpty( userId ) )
    9. {
    10. throw new ServiceException( BizExceptionEnum.REQUEST_NULL );
    11. }
    12. assertAuth( userId );
    13. User user = this.userService.selectById( userId );
    14. model.addAttribute( user );
    15. model.addAttribute( "roleName", ConstantFactory.me().getRoleName( user.getRoleid() ) );
    16. model.addAttribute( "deptName", ConstantFactory.me().getDeptName( user.getDeptid() ) );
    17. //LogObjectHolder就保存原始对象
    18. //Scope 为session,相当于缓存
    19. LogObjectHolder.me().set( user );
    20. return(PREFIX + "user_edit.html");
    21. }