Browse Source

修改AuthCheck,支持注解到类上

4228306 5 years ago
parent
commit
5879e1f20f

+ 1 - 1
src/main/java/com/yaoxiang/diagnosis/authority/AuthCheck.java

@@ -7,7 +7,7 @@ import java.lang.annotation.*;
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.METHOD)
+@Target({ElementType.METHOD,ElementType.TYPE})
 public @interface AuthCheck {
 
     String role() default "";

+ 30 - 7
src/main/java/com/yaoxiang/diagnosis/authority/AuthCheckAspect.java

@@ -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();
     }

+ 23 - 0
src/main/java/com/yaoxiang/diagnosis/controller/AuthTestController.java

@@ -0,0 +1,23 @@
+package com.yaoxiang.diagnosis.controller;
+
+import com.yaoxiang.diagnosis.authority.AuthCheck;
+import com.yaoxiang.diagnosis.model.AuthUser;
+import com.yaoxiang.diagnosis.util.SecurityUtil;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@AuthCheck(role = "test")
+public class AuthTestController {
+
+    @GetMapping("test/info1")
+    public AuthUser auth() {
+        return SecurityUtil.getCurrentUser();
+    }
+
+    @GetMapping("test/info2")
+    @AuthCheck(role = "super_student")
+    public AuthUser auth2() {
+        throw new RuntimeException("hello");
+    }
+}

+ 2 - 4
src/main/java/com/yaoxiang/diagnosis/controller/TestController.java

@@ -78,11 +78,10 @@ public class TestController {
         return name + " , Now is " + new Date() + "  and visit times : " + cnt;
     }
 
-    @GetMapping("/info")
+    @GetMapping("/test/info")
     @AuthCheck(role = "test")
     public AuthUser auth() {
-        AuthUser user = SecurityUtil.getCurrentUser();
-        return user;
+        return SecurityUtil.getCurrentUser();
     }
 
 //    @PreAuthorize("hasRole('ROLE_ADMIN')")
@@ -99,7 +98,6 @@ public class TestController {
 //        return result;
 //    }
 
-    @PreAuthorize("hasRole('ROLE_ADMIN')")
     @GetMapping("/temp1")
     public void temp1() {
         List<Question> questions = questionRepo.findByPid(7);