当前位置:网站首页>Use facet to record operation log

Use facet to record operation log

2022-07-07 04:03:00 Friendship years

@Aspect
@Component
public class SysLogAspect {
    @Autowired
    private SysLogService sysLogService;

    /**
     *  Tangent point 
     */
    @Pointcut("@annotation(com.platform.annotation.SysLog)")
    public void logPointCut() {

    }

    /**
     *  Pre notice 
     *
     * @param joinPoint  Connection point 
     */
    @Before("logPointCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        SysLogEntity sysLog = new SysLogEntity();
        SysLog syslog = method.getAnnotation(SysLog.class);
        if (syslog != null) {
            // The description on the note 
            sysLog.setOperation(syslog.value());
        }

        // Requested method name 
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");

        // Requested parameters 
        Object[] args = joinPoint.getArgs();
        String params = JSON.toJSONString(args[0]);
        sysLog.setParams(params);

        // obtain request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        // Set up IP Address 
        sysLog.setIp(IPUtils.getIpAddr(request));

        // user name 
        SysUserEntity sysUserEntity = ShiroUtils.getUserEntity();
        String username = "";
        if ("login".equals(methodName)) {
            username = params;
        }
        if (null != sysUserEntity) {
            username = ShiroUtils.getUserEntity().getUsername();
        }
        sysLog.setUsername(username);

        sysLog.setCreateDate(new Date());
        // Save system logs 
        sysLogService.save(sysLog);
    }

}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {

    String value() default " The operation log ";
}

@SysLog(" Delete menu ")
@RequestMapping("/delete")
@RequiresPermissions("sys:menu:delete")
public R delete(@RequestBody Long[] menuIds) {
    for (Long menuId : menuIds) {
        if (menuId.longValue() <= 30) {
            return R.error(" system menu , Can't delete ");
        }
    }
    sysMenuService.deleteBatch(menuIds);

    return R.ok();
}

原网站

版权声明
本文为[Friendship years]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062108261974.html