In the previous article, I introduced the log interception configuration of AOP. What I continue to do is how to correctly configure the interception information
First, define an annotation information and required parameters
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface logAnnotation {
/**
* @param 模块名字
*/
String modelName();
/**
* @param 操作内容描述
*/
String option();
/**
* @param TerminalConstantClass.SYS__OPTIONLEVEL_0 = "common"(一般操作,记录保留时间短)
* @param TerminalConstantClass.SYS__OPTIONLEVEL_1 = "sensitive"(敏感操作,记录保留时间长)
*/
String optionLevel();
}
At the same time, specify the pointcut in the defined aspect class
@Pointcut(value = "within(com.ustcinfo.fccos.terminal.web..*) && @annotation(com.ustcinfo.fccos.terminal.web.AOP.logAnnotation)")
public void operationLogcontrollerAspect() {
}
Represents that all subfolders in the web directory have an annotation type of logAnnotation, which are entry points
Define pointcut behavior
@Before(value = "operationLogcontrollerAspect() && @annotation(log)")
public void doBefore(JoinPoint joinPoint,logAnnotation log) {
System.out.println("****************");
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = request.getRemoteAddr();
HttpSession session = request.getSession();
LoginUserRightsModel loginUser = getLoginInfos(session);
String userId = loginUser.getUserId();
System.out.println(joinPoint.getSignature().getName());
System.out.println(log.option());
System.out.println("目标方法内的参数为" + Arrays.asList(joinPoint.getArgs()));
} catch (Exception e) {
e.printStackTrace();
logger.error("记录操作日志发生异常:\n" + e.getMessage());
}
}
joinPoint.getArgs()---------Get the input parameter information of the request
log.option()-------Get the option content in the annotation custom attribute
joinPoint.getSignature().getName()-----Get the method name of the annotation. . . . .
2018-05-09 14:22:16.101 INFO [MyFilter] 当前admin用户id:3--3
2018-05-09 14:22:16.102 INFO [MyFilter] 查找权限信息
2018-05-09 14:22:16.109 INFO [MyFilter] 找到对应的权限配置信息,admin--[LoginUserRightsModel [userId=admin, isGodownManage=false, proviceCode=80001, cityName=安徽省, cityCode=80001, countyName=null, countyCode=80001, godownCodeM=null, godownNameM=null, godownLevelM=null, godownTypeM=null, level=省级, godownDpt=null]]
****************
getInstallerInfos
查询具体装维的归属信息
目标方法内的参数为[AA150]
2018-05-09 14:22:16.185 DEBUG [getInfosByuserId] ==> Preparing: SELECT `user_id`,`user_name`,`region_id`,`region_name`,`county_id`,`county_name`,`potrolgroup_id`,`potrolgroup_name` FROM device2.`tb_usrgrp_rela` WHERE `user_id`=?
2018-05-09 14:22:16.213 DEBUG [getInfosByuserId] ==> Parameters: AA150(String)
2018-05-09 14:22:16.253 DEBUG [getInfosByuserId] <== Total: 1
The above is the aspect log information