|
@@ -0,0 +1,119 @@
|
|
|
|
+package com.qmth.boot.api.config;
|
|
|
|
+
|
|
|
|
+import com.qmth.boot.api.annotation.Aac;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.aop.support.AopUtils;
|
|
|
|
+import org.springframework.context.ApplicationListener;
|
|
|
|
+import org.springframework.context.event.ContextRefreshedEvent;
|
|
|
|
+import org.springframework.core.annotation.AnnotationUtils;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
+import org.springframework.web.method.HandlerMethod;
|
|
|
|
+
|
|
|
|
+import javax.validation.constraints.Null;
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.LinkedList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+public class ApiConfigService implements ApplicationListener<ContextRefreshedEvent> {
|
|
|
|
+
|
|
|
|
+ protected static final Logger log = LoggerFactory.getLogger(ApiConfigService.class);
|
|
|
|
+
|
|
|
|
+ private ApiProperties apiProperties;
|
|
|
|
+
|
|
|
|
+ private Map<Method, AacConfig> configMap;
|
|
|
|
+
|
|
|
|
+ private Map<String, Object> uriPermissionMap;
|
|
|
|
+
|
|
|
|
+ public ApiConfigService(ApiProperties apiProperties) {
|
|
|
|
+ this.apiProperties = apiProperties;
|
|
|
|
+ this.configMap = new HashMap<>();
|
|
|
|
+ this.uriPermissionMap = new HashMap<>();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public AacConfig getAacConfig(HandlerMethod handlerMethod) {
|
|
|
|
+ return configMap.get(handlerMethod.getMethod());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public synchronized void onApplicationEvent(ContextRefreshedEvent event) {
|
|
|
|
+ if (event.getApplicationContext().getParent() == null) {
|
|
|
|
+ Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(RestController.class);
|
|
|
|
+ for (Object bean : beans.values()) {
|
|
|
|
+ init(bean);
|
|
|
|
+ }
|
|
|
|
+ beans = event.getApplicationContext().getBeansWithAnnotation(Controller.class);
|
|
|
|
+ for (Object bean : beans.values()) {
|
|
|
|
+ init(bean);
|
|
|
|
+ }
|
|
|
|
+ log.info("ApiConfigService inited, aac method count={}, uri permission count={}", configMap.size(),
|
|
|
|
+ uriPermissionMap.size());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void init(Object bean) {
|
|
|
|
+ Class<?> clazz = AopUtils.getTargetClass(bean);
|
|
|
|
+ String[] parentUri = null;
|
|
|
|
+ RequestMapping parentMapping = AnnotationUtils.findAnnotation(clazz, RequestMapping.class);
|
|
|
|
+ if (parentMapping != null) {
|
|
|
|
+ parentUri = parentMapping.value();
|
|
|
|
+ }
|
|
|
|
+ if (parentUri == null || parentUri.length == 0) {
|
|
|
|
+ parentUri = new String[] { "" };
|
|
|
|
+ }
|
|
|
|
+ Method[] methods = clazz.getMethods();
|
|
|
|
+ for (Method method : methods) {
|
|
|
|
+ String[] uri = new String[] { "" };
|
|
|
|
+ RequestMapping rm = AnnotationUtils.findAnnotation(method, RequestMapping.class);
|
|
|
|
+ PostMapping post = AnnotationUtils.findAnnotation(method, PostMapping.class);
|
|
|
|
+ GetMapping get = AnnotationUtils.findAnnotation(method, GetMapping.class);
|
|
|
|
+ PutMapping put = AnnotationUtils.findAnnotation(method, PutMapping.class);
|
|
|
|
+ DeleteMapping delete = AnnotationUtils.findAnnotation(method, DeleteMapping.class);
|
|
|
|
+ if (rm != null) {
|
|
|
|
+ uri = rm.value();
|
|
|
|
+ } else if (post != null) {
|
|
|
|
+ uri = post.value();
|
|
|
|
+ } else if (get != null) {
|
|
|
|
+ uri = get.value();
|
|
|
|
+ } else if (put != null) {
|
|
|
|
+ uri = put.value();
|
|
|
|
+ } else if (delete != null) {
|
|
|
|
+ uri = delete.value();
|
|
|
|
+ } else {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ initMethod(clazz, method, parentUri, uri);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void initMethod(Class<?> clazz, Method method, String[] prefixs, String[] uris) {
|
|
|
|
+ AacConfig ac = new AacConfig(apiProperties);
|
|
|
|
+ ac.merge(new AacConfig(AnnotationUtils.findAnnotation(clazz, Aac.class)));
|
|
|
|
+ ac.merge(new AacConfig(AnnotationUtils.findAnnotation(method, Aac.class)));
|
|
|
|
+ configMap.put(method, ac);
|
|
|
|
+
|
|
|
|
+ Aac aac = AnnotationUtils.findAnnotation(method, Aac.class);
|
|
|
|
+ if (aac != null && !aac.permit().clazz().equals(Null.class)) {
|
|
|
|
+ List<String> uriList = concat(prefixs, uris);
|
|
|
|
+ for (String uri : uriList) {
|
|
|
|
+ uriPermissionMap.put(uri, aac.permit());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ log.debug("Api method inited, {}:{}", clazz.getName(), method.getName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<String> concat(String[] prefixs, String[] uris) {
|
|
|
|
+ List<String> result = new LinkedList<>();
|
|
|
|
+ for (String prefix : prefixs) {
|
|
|
|
+ for (String uri : uris) {
|
|
|
|
+ result.add(prefix.concat(uri));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+}
|