Ver código fonte

使用自动配置方式。配置文件中 login.enable=true 时才启用权限验证

周浩 9 anos atrás
pai
commit
dac2a9bc13

+ 35 - 0
hsweb-web-controller/src/main/java/org/hsweb/web/controller/AopAuthorizeValidatorAutoConfiguration.java

@@ -0,0 +1,35 @@
+package org.hsweb.web.controller;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.hsweb.web.authorize.AopAuthorizeValidator;
+import org.hsweb.web.exception.AuthorizeException;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.annotation.Order;
+
+/**
+ * Created by zhouhao on 16-4-28.
+ */
+@Configuration
+@ConditionalOnProperty(name = "login.enable", havingValue = "true")
+public class AopAuthorizeValidatorAutoConfiguration extends AopAuthorizeValidator {
+
+    @Bean
+    public ControllerAuthorizeValidator controllerAuthorizeValidator() {
+        return new ControllerAuthorizeValidator();
+    }
+
+    @Aspect
+    @Order(1)
+    static class ControllerAuthorizeValidator extends AopAuthorizeValidator {
+        @Around(value = "execution(* org.hsweb.web.controller..*Controller..*(..))")
+        public Object around(ProceedingJoinPoint pjp) throws Throwable {
+            boolean access = super.validate(pjp);
+            if (!access) throw new AuthorizeException("无权限", 403);
+            return pjp.proceed();
+        }
+    }
+}

+ 0 - 24
hsweb-web-controller/src/main/java/org/hsweb/web/controller/AopAuthorizeValidatorConfiguration.java

@@ -1,24 +0,0 @@
-package org.hsweb.web.controller;
-
-import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.annotation.Around;
-import org.aspectj.lang.annotation.Aspect;
-import org.hsweb.web.authorize.AopAuthorizeValidator;
-import org.hsweb.web.exception.AuthorizeException;
-import org.springframework.core.annotation.Order;
-import org.springframework.stereotype.Component;
-
-/**
- * Created by zhouhao on 16-4-28.
- */
-@Aspect
-@Component
-@Order(1)
-public class AopAuthorizeValidatorConfiguration extends AopAuthorizeValidator {
-    @Around(value = "execution(* org.hsweb.web.controller..*Controller..*(..))")
-    public Object around(ProceedingJoinPoint pjp) throws Throwable {
-        boolean access = super.validate(pjp);
-        if (!access) throw new AuthorizeException("无权限", 403);
-        return pjp.proceed();
-    }
-}