Spring切面编程(AOP)
2021-09-27 本文已影响0人
AC编程
一、按指定注解切人
@Slf4j
@Aspect
@Component
public class MemberLoginAOPComponent {
@AfterReturning(value = "@annotation(com.alanchen.annotation.SignMemberLoginLog)", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result){
Result<Oauth2TokenDto> loginResult = (Result<Oauth2TokenDto>) result;
if (loginResult.getCode() != ResultCode.SUCCESS.getCode() || null == loginResult.getData()) {
return;
}
Object[] objects = joinPoint.getArgs();
Map<String, Object> mapParam = new HashMap<>();
if (null != objects) {
mapParam = BeanUtil.beanToMap(objects[0]);
}
Oauth2TokenDto oauth2TokenDto = loginResult.getData();
// 其他逻辑
}
}
二、按方法切入
@Component
@Aspect
@Slf4j
public class MicrospotAspect {
@Pointcut(
"execution(public * com.alanchen.*.controller.*Controller.*(..))" +
"|| execution(public * com.alanchen.*.controller.op.BackendMemberCertMerController.*(..))" +
"&& !execution(public * com.alanchen.*.controller.ApiLogController.*(..))" +
"&& !execution(public * com.alanchen.*.controller.SensitiveWordController.selectList(..))" +
"&& !execution(public * com.alanchen.*.controller.SensitiveWordController.test(..))" +
"&& !execution(public * com.alanchen.*.*Controller.UserController.*(..))"
)
public void aspectCut() {}
/**
* 正常执行
*
* @param joinPoint 切入点
* @param result
* @throws Throwable 异常信息
*/
@AfterReturning(value = "aspectCut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) throws Exception {
if(result == null){
return;
}
if(result instanceof Result){
Result<Object> createResult = (Result<Object>) result;
if(createResult.getCode() !=ResultCode.SUCCESS.getCode()){
return;
}
}else{
return;
}
try{
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != requestAttributes) {
HttpServletRequest request = requestAttributes.getRequest();
String requestUri = request.getRequestURI();
// 取到参数
}
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
}