提交 960ffcd9 authored 作者: 马晨俊's avatar 马晨俊

mcj:aop记录方法时间

上级 d4904820
...@@ -24,7 +24,7 @@ import java.util.List; ...@@ -24,7 +24,7 @@ import java.util.List;
@Aspect @Aspect
@Slf4j @Slf4j
@Component @Component
public class AuthIntercept { public class AopIntercept {
private User user; private User user;
...@@ -32,27 +32,18 @@ public class AuthIntercept { ...@@ -32,27 +32,18 @@ public class AuthIntercept {
this.user = user; this.user = user;
} }
@Pointcut("@annotation(com.zjty.inspect.aop.AuthAnnotation)") @Pointcut("execution(* com.zjty.inspect.inspect.Inspector.inspect(..))")
public void doPointCut() { public void doPointCut() {
} }
@Around("doPointCut()") @Around("doPointCut()")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable { public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
AuthAnnotation annotation = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(AuthAnnotation.class); long startMillis = System.currentTimeMillis();
//注解配置权限code
String[] methodAuthCodes = annotation.code();
//用户拥有权限code
List<String> authorityCode = user.getAuthorityCode();
int i =0;
for (String methodAuthCode : methodAuthCodes) {
if(authorityCode.contains(methodAuthCode)){
i++;
}
}
if(i==0){
return ResponseEntity.status(403).build();
}
Object proceed = joinPoint.proceed(); Object proceed = joinPoint.proceed();
long endMillis = System.currentTimeMillis();
long time = (endMillis - startMillis);
log.info("评估总耗时{}毫秒",time);
log.info("PersonAspect2 ==> before method : {}", joinPoint.getSignature().getName()); log.info("PersonAspect2 ==> before method : {}", joinPoint.getSignature().getName());
log.info("注解的类型名称为{}",joinPoint.getSignature().getDeclaringTypeName()); log.info("注解的类型名称为{}",joinPoint.getSignature().getDeclaringTypeName());
log.info("方法修饰符个数为{}",joinPoint.getSignature().getModifiers()); log.info("方法修饰符个数为{}",joinPoint.getSignature().getModifiers());
......
package com.zjty.inspect.config; package com.zjty.inspect.config;
import com.zjty.inspect.aop.AuthIntercept; import com.zjty.inspect.aop.AopIntercept;
import com.zjty.inspect.entity.ServerResponse; import com.zjty.inspect.entity.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -22,7 +22,7 @@ public class LoginInterceptor implements HandlerInterceptor { ...@@ -22,7 +22,7 @@ public class LoginInterceptor implements HandlerInterceptor {
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Autowired @Autowired
private AuthIntercept authIntercept; private AopIntercept aopIntercept;
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
String sessionId = request.getHeader("sessionId"); String sessionId = request.getHeader("sessionId");
...@@ -34,7 +34,7 @@ public class LoginInterceptor implements HandlerInterceptor { ...@@ -34,7 +34,7 @@ public class LoginInterceptor implements HandlerInterceptor {
response.setStatus(403); response.setStatus(403);
return false; return false;
} }
authIntercept.setUser(user.getData()); aopIntercept.setUser(user.getData());
return true; return true;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论