【同步】BOOT 和 CLOUD 的功能

pull/207/head
YunaiV 2025-08-31 10:25:40 +08:00
parent 34aea48b7c
commit edb5cdd372
5 changed files with 62 additions and 49 deletions

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.framework.tenant.config; package cn.iocoder.yudao.framework.tenant.config;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.redis.config.YudaoCacheProperties; import cn.iocoder.yudao.framework.redis.config.YudaoCacheProperties;
@ -8,7 +9,6 @@ import cn.iocoder.yudao.framework.security.core.service.SecurityFrameworkService
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnoreAspect; import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnoreAspect;
import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor; import cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJobAspect;
import cn.iocoder.yudao.framework.tenant.core.mq.rabbitmq.TenantRabbitMQInitializer; import cn.iocoder.yudao.framework.tenant.core.mq.rabbitmq.TenantRabbitMQInitializer;
import cn.iocoder.yudao.framework.tenant.core.mq.redis.TenantRedisMessageInterceptor; import cn.iocoder.yudao.framework.tenant.core.mq.redis.TenantRedisMessageInterceptor;
import cn.iocoder.yudao.framework.tenant.core.mq.rocketmq.TenantRocketMQInitializer; import cn.iocoder.yudao.framework.tenant.core.mq.rocketmq.TenantRocketMQInitializer;
@ -20,7 +20,6 @@ import cn.iocoder.yudao.framework.tenant.core.web.TenantContextWebFilter;
import cn.iocoder.yudao.framework.tenant.core.web.TenantVisitContextInterceptor; import cn.iocoder.yudao.framework.tenant.core.web.TenantVisitContextInterceptor;
import cn.iocoder.yudao.framework.web.config.WebProperties; import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -46,8 +45,10 @@ import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.util.pattern.PathPattern; import org.springframework.web.util.pattern.PathPattern;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@ -93,41 +94,13 @@ public class YudaoTenantAutoConfiguration {
// ========== WEB ========== // ========== WEB ==========
@Bean @Bean
public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter(TenantProperties tenantProperties) { public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter() {
FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TenantContextWebFilter()); registrationBean.setFilter(new TenantContextWebFilter());
registrationBean.setOrder(WebFilterOrderEnum.TENANT_CONTEXT_FILTER); registrationBean.setOrder(WebFilterOrderEnum.TENANT_CONTEXT_FILTER);
addIgnoreUrls(tenantProperties);
return registrationBean; return registrationBean;
} }
/**
* Controller {@link TenantIgnore} URL
*
* @param tenantProperties
*/
private void addIgnoreUrls(TenantProperties tenantProperties) {
// 获得接口对应的 HandlerMethod 集合
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping)
applicationContext.getBean("requestMappingHandlerMapping");
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = requestMappingHandlerMapping.getHandlerMethods();
// 获得有 @TenantIgnore 注解的接口
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethodMap.entrySet()) {
HandlerMethod handlerMethod = entry.getValue();
if (!handlerMethod.hasMethodAnnotation(TenantIgnore.class)) {
continue;
}
// 添加到忽略的 URL 中
if (entry.getKey().getPatternsCondition() != null) {
tenantProperties.getIgnoreUrls().addAll(entry.getKey().getPatternsCondition().getPatterns());
}
if (entry.getKey().getPathPatternsCondition() != null) {
tenantProperties.getIgnoreUrls().addAll(
convertList(entry.getKey().getPathPatternsCondition().getPatterns(), PathPattern::getPatternString));
}
}
}
@Bean @Bean
public TenantVisitContextInterceptor tenantVisitContextInterceptor(TenantProperties tenantProperties, public TenantVisitContextInterceptor tenantVisitContextInterceptor(TenantProperties tenantProperties,
SecurityFrameworkService securityFrameworkService) { SecurityFrameworkService securityFrameworkService) {
@ -155,18 +128,40 @@ public class YudaoTenantAutoConfiguration {
GlobalExceptionHandler globalExceptionHandler, GlobalExceptionHandler globalExceptionHandler,
TenantFrameworkService tenantFrameworkService) { TenantFrameworkService tenantFrameworkService) {
FilterRegistrationBean<TenantSecurityWebFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<TenantSecurityWebFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties, registrationBean.setFilter(new TenantSecurityWebFilter(webProperties, tenantProperties, getTenantIgnoreUrls(),
globalExceptionHandler, tenantFrameworkService)); globalExceptionHandler, tenantFrameworkService));
registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER); registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER);
return registrationBean; return registrationBean;
} }
// ========== Job ========== /**
* Controller {@link TenantIgnore} URL
@Bean *
@ConditionalOnClass(name = "com.xxl.job.core.handler.annotation.XxlJob") * @return URL
public TenantJobAspect tenantJobAspect(TenantFrameworkService tenantFrameworkService) { */
return new TenantJobAspect(tenantFrameworkService); private Set<String> getTenantIgnoreUrls() {
Set<String> ignoreUrls = new HashSet<>();
// 获得接口对应的 HandlerMethod 集合
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping)
applicationContext.getBean("requestMappingHandlerMapping");
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = requestMappingHandlerMapping.getHandlerMethods();
// 获得有 @TenantIgnore 注解的接口
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethodMap.entrySet()) {
HandlerMethod handlerMethod = entry.getValue();
if (!handlerMethod.hasMethodAnnotation(TenantIgnore.class) // 方法级
&& !handlerMethod.getBeanType().isAnnotationPresent(TenantIgnore.class)) { // 接口级
continue;
}
// 添加到忽略的 URL 中
if (entry.getKey().getPatternsCondition() != null) {
ignoreUrls.addAll(entry.getKey().getPatternsCondition().getPatterns());
}
if (entry.getKey().getPathPatternsCondition() != null) {
ignoreUrls.addAll(
convertList(entry.getKey().getPathPatternsCondition().getPatterns(), PathPattern::getPatternString));
}
}
return ignoreUrls;
} }
// ========== MQ ========== // ========== MQ ==========

View File

@ -12,15 +12,16 @@ import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
import cn.iocoder.yudao.framework.web.config.WebProperties; import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.framework.web.core.filter.ApiRequestFilter; import cn.iocoder.yudao.framework.web.core.filter.ApiRequestFilter;
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import jakarta.servlet.FilterChain; import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
/** /**
* Security Web * Security Web
@ -35,17 +36,26 @@ public class TenantSecurityWebFilter extends ApiRequestFilter {
private final TenantProperties tenantProperties; private final TenantProperties tenantProperties;
/**
* URL
*
* <a href="https://gitee.com/zhijiantianya/yudao-cloud/issues/ICUQL9"> @TenantIgnore Controller </>
*/
private final Set<String> ignoreUrls;
private final AntPathMatcher pathMatcher; private final AntPathMatcher pathMatcher;
private final GlobalExceptionHandler globalExceptionHandler; private final GlobalExceptionHandler globalExceptionHandler;
private final TenantFrameworkService tenantFrameworkService; private final TenantFrameworkService tenantFrameworkService;
public TenantSecurityWebFilter(TenantProperties tenantProperties, public TenantSecurityWebFilter(WebProperties webProperties,
WebProperties webProperties, TenantProperties tenantProperties,
Set<String> ignoreUrls,
GlobalExceptionHandler globalExceptionHandler, GlobalExceptionHandler globalExceptionHandler,
TenantFrameworkService tenantFrameworkService) { TenantFrameworkService tenantFrameworkService) {
super(webProperties); super(webProperties);
this.tenantProperties = tenantProperties; this.tenantProperties = tenantProperties;
this.ignoreUrls = ignoreUrls;
this.pathMatcher = new AntPathMatcher(); this.pathMatcher = new AntPathMatcher();
this.globalExceptionHandler = globalExceptionHandler; this.globalExceptionHandler = globalExceptionHandler;
this.tenantFrameworkService = tenantFrameworkService; this.tenantFrameworkService = tenantFrameworkService;
@ -103,7 +113,8 @@ public class TenantSecurityWebFilter extends ApiRequestFilter {
private boolean isIgnoreUrl(HttpServletRequest request) { private boolean isIgnoreUrl(HttpServletRequest request) {
String apiUri = request.getRequestURI().substring(request.getContextPath().length()); String apiUri = request.getRequestURI().substring(request.getContextPath().length());
// 快速匹配,保证性能 // 快速匹配,保证性能
if (CollUtil.contains(tenantProperties.getIgnoreUrls(), apiUri)) { if (CollUtil.contains(tenantProperties.getIgnoreUrls(), apiUri)
|| CollUtil.contains(ignoreUrls, apiUri)) {
return true; return true;
} }
// 逐个 Ant 路径匹配 // 逐个 Ant 路径匹配
@ -112,6 +123,11 @@ public class TenantSecurityWebFilter extends ApiRequestFilter {
return true; return true;
} }
} }
for (String url : ignoreUrls) {
if (pathMatcher.match(url, apiUri)) {
return true;
}
}
return false; return false;
} }

View File

@ -165,7 +165,8 @@ public class YudaoWebSecurityConfigurerAdapter {
// 获得有 @PermitAll 注解的接口 // 获得有 @PermitAll 注解的接口
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethodMap.entrySet()) { for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethodMap.entrySet()) {
HandlerMethod handlerMethod = entry.getValue(); HandlerMethod handlerMethod = entry.getValue();
if (!handlerMethod.hasMethodAnnotation(PermitAll.class)) { if (!handlerMethod.hasMethodAnnotation(PermitAll.class) // 方法级
&& !handlerMethod.getBeanType().isAnnotationPresent(PermitAll.class)) { // 接口级
continue; continue;
} }
Set<String> urls = new HashSet<>(); Set<String> urls = new HashSet<>();

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.core.enums;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.yudao.framework.common.core.ArrayValuable; import cn.iocoder.yudao.framework.common.core.ArrayValuable;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@ -57,7 +58,7 @@ public enum IotDeviceMessageMethodEnum implements ArrayValuable<String> {
/** /**
* reply * reply
*/ */
public static final Set<String> REPLY_DISABLED = Set.of( public static final Set<String> REPLY_DISABLED = SetUtils.asSet(
STATE_UPDATE.getMethod(), STATE_UPDATE.getMethod(),
OTA_PROGRESS.getMethod() // 参考阿里云OTA 升级进度上报,不进行回复 OTA_PROGRESS.getMethod() // 参考阿里云OTA 升级进度上报,不进行回复
); );

View File

@ -610,7 +610,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest {
orderExtensionMapper.insert(orderExtension); orderExtensionMapper.insert(orderExtension);
// 重要:需要将 order 的 extensionId 更新下 // 重要:需要将 order 的 extensionId 更新下
order.setExtensionId(orderExtension.getId()); order.setExtensionId(orderExtension.getId());
orderMapper.updateById(order); orderMapper.updateById(new PayOrderDO().setId(order.getId()).setExtensionId(orderExtension.getId()));
// 准备参数 // 准备参数
PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setId(10L)); PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setId(10L));
PayOrderRespDTO notify = randomPojo(PayOrderRespDTO.class, PayOrderRespDTO notify = randomPojo(PayOrderRespDTO.class,
@ -622,7 +622,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest {
// 断言 PayOrderExtensionDO :数据未更新,因为它是 SUCCESS // 断言 PayOrderExtensionDO :数据未更新,因为它是 SUCCESS
assertPojoEquals(orderExtension, orderExtensionMapper.selectOne(null)); assertPojoEquals(orderExtension, orderExtensionMapper.selectOne(null));
// 断言 PayOrderDO :数据未更新,因为它是 SUCCESS // 断言 PayOrderDO :数据未更新,因为它是 SUCCESS
assertPojoEquals(order, orderMapper.selectOne(null)); assertPojoEquals(order, orderMapper.selectOne(null), "updateTime", "updater");
// 断言,调用 // 断言,调用
verify(notifyService, never()).createPayNotifyTask(anyInt(), anyLong()); verify(notifyService, never()).createPayNotifyTask(anyInt(), anyLong());
} }
@ -664,7 +664,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest {
"updateTime", "updater"); "updateTime", "updater");
// 断言,调用 // 断言,调用
verify(notifyService).createPayNotifyTask(eq(PayNotifyTypeEnum.ORDER.getType()), verify(notifyService).createPayNotifyTask(eq(PayNotifyTypeEnum.ORDER.getType()),
eq(orderExtension.getOrderId())); eq(orderExtension.getOrderId()));
} }
@Test @Test