Browse Source

Merge remote-tracking branch 'origin/master'

pull/7199/head
JEECG 3 months ago
parent
commit
7e2b4c68ec
  1. 29
      jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ignore/IgnoreAuthPostProcessor.java

29
jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ignore/IgnoreAuthPostProcessor.java

@ -3,16 +3,16 @@ package org.jeecg.config.shiro.ignore;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jeecg.config.shiro.IgnoreAuth; import org.jeecg.config.shiro.IgnoreAuth;
import org.springframework.aop.framework.Advised; import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* 在spring boot初始化时根据@RestController注解获取当前spring容器中的bean * 在spring boot初始化时根据@RestController注解获取当前spring容器中的bean
@ -22,25 +22,21 @@ import java.util.*;
@Slf4j @Slf4j
@Component @Component
@AllArgsConstructor @AllArgsConstructor
public class IgnoreAuthPostProcessor implements ApplicationListener<ContextRefreshedEvent> { public class IgnoreAuthPostProcessor implements InitializingBean {
private RequestMappingHandlerMapping requestMappingHandlerMapping;
private ApplicationContext applicationContext;
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void afterPropertiesSet() throws Exception {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
List<String> ignoreAuthUrls = new ArrayList<>(); List<String> ignoreAuthUrls = new ArrayList<>();
if (event.getApplicationContext().getParent() == null) { Set<Class<?>> restControllers = requestMappingHandlerMapping.getHandlerMethods().values().stream().map(HandlerMethod::getBeanType).collect(Collectors.toSet());
// 只处理根应用上下文的事件,避免在子上下文中重复处理 for (Class<?> restController : restControllers) {
Map<String, Object> restControllers = applicationContext.getBeansWithAnnotation(RestController.class);
for (Object restController : restControllers.values()) {
// 如 online系统的controller并不是spring 默认生成
if (restController instanceof Advised) {
ignoreAuthUrls.addAll(postProcessRestController(restController)); ignoreAuthUrls.addAll(postProcessRestController(restController));
} }
}
}
log.info("Init Token ignoreAuthUrls Config [ 集合 ] :{}", ignoreAuthUrls); log.info("Init Token ignoreAuthUrls Config [ 集合 ] :{}", ignoreAuthUrls);
if (!CollectionUtils.isEmpty(ignoreAuthUrls)) { if (!CollectionUtils.isEmpty(ignoreAuthUrls)) {
@ -53,9 +49,8 @@ public class IgnoreAuthPostProcessor implements ApplicationListener<ContextRefre
log.info("Init Token ignoreAuthUrls Config [ 耗时 ] :" + elapsedTime + "毫秒"); log.info("Init Token ignoreAuthUrls Config [ 耗时 ] :" + elapsedTime + "毫秒");
} }
private List<String> postProcessRestController(Object restController) { private List<String> postProcessRestController(Class<?> clazz) {
List<String> ignoreAuthUrls = new ArrayList<>(); List<String> ignoreAuthUrls = new ArrayList<>();
Class<?> clazz = ((Advised) restController).getTargetClass();
RequestMapping base = clazz.getAnnotation(RequestMapping.class); RequestMapping base = clazz.getAnnotation(RequestMapping.class);
String[] baseUrl = Objects.nonNull(base) ? base.value() : new String[]{}; String[] baseUrl = Objects.nonNull(base) ? base.value() : new String[]{};
Method[] methods = clazz.getDeclaredMethods(); Method[] methods = clazz.getDeclaredMethods();

Loading…
Cancel
Save