|
@@ -9,14 +9,11 @@ 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.bind.annotation.RestController;
|
|
|
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
|
|
@@ -28,12 +25,9 @@ public class ApiConfigService implements ApplicationListener<ContextRefreshedEve
|
|
|
|
|
|
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) {
|
|
@@ -51,69 +45,23 @@ public class ApiConfigService implements ApplicationListener<ContextRefreshedEve
|
|
|
for (Object bean : beans.values()) {
|
|
|
init(bean);
|
|
|
}
|
|
|
- log.info("ApiConfigService inited, aac method count={}, uri permission count={}", configMap.size(),
|
|
|
- uriPermissionMap.size());
|
|
|
+ log.info("ApiConfigService inited, aac method count={}", configMap.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);
|
|
|
+ initMethod(clazz, method);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void initMethod(Class<?> clazz, Method method, String[] prefixs, String[] uris) {
|
|
|
+ private void initMethod(Class<?> clazz, Method method) {
|
|
|
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;
|
|
|
- }
|
|
|
}
|