|
@@ -6,9 +6,11 @@ import com.yaoxiang.diagnosis.util.SecurityUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.Signature;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -38,30 +40,51 @@ public class AuthCheckAspect {
|
|
|
@Resource
|
|
|
private AuthService authService;
|
|
|
|
|
|
- @Pointcut("@annotation(com.yaoxiang.diagnosis.authority.AuthCheck) && @annotation(authCheck)")
|
|
|
- public void pointcut(AuthCheck authCheck) {
|
|
|
+ @Pointcut("@within(com.yaoxiang.diagnosis.authority.AuthCheck) || @annotation(com.yaoxiang.diagnosis.authority.AuthCheck)")
|
|
|
+ public void pointcut() {
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- @Around(value = "pointcut(authCheck)", argNames = "joinPoint,authCheck")
|
|
|
- public Object around(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
|
|
|
+ @Around(value = "pointcut()")
|
|
|
+ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
+ AuthCheck authCheck = resolve(joinPoint);
|
|
|
String principal = getPrincipal();
|
|
|
String role = authCheck.role();
|
|
|
String authority = authCheck.authority();
|
|
|
boolean access = authService.check(principal, role, authority);
|
|
|
logger.info("check auth for principal={},role={},authority={},access={}", principal, role, authority, access);
|
|
|
if (!access) {
|
|
|
- throw new AuthCheckException("权限检测失败");
|
|
|
+ throw new AuthCheckException("Auth check fail.");
|
|
|
}
|
|
|
- Object result = null;
|
|
|
+ Object result;
|
|
|
try {
|
|
|
result = joinPoint.proceed();
|
|
|
} catch (Exception e) {
|
|
|
- logger.error("Server error,message={}.", e.getMessage());
|
|
|
+ logger.error("server error,class={},method={},message={}.", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e.getMessage());
|
|
|
+ logger.error("server error detail", e);
|
|
|
throw e;
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ private AuthCheck resolve(ProceedingJoinPoint joinPoint) {
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+ AuthCheck authCheck = method.getAnnotation(AuthCheck.class);
|
|
|
+ if (authCheck == null) {
|
|
|
+ authCheck = joinPoint.getTarget().getClass().getAnnotation(AuthCheck.class);
|
|
|
+ if (authCheck == null) {
|
|
|
+ for (Class<?> cls : joinPoint.getClass().getInterfaces()) {
|
|
|
+ authCheck = cls.getAnnotation(AuthCheck.class);
|
|
|
+ if (authCheck != null) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return authCheck;
|
|
|
+ }
|
|
|
+
|
|
|
private String getPrincipal() {
|
|
|
return SecurityUtil.getCurrentUser().getUsername();
|
|
|
}
|