Swagger 增加 tenant-id 头

This commit is contained in:
YunaiV 2022-02-27 02:40:24 +08:00
parent fc509837a1
commit 2505d61b08
8 changed files with 24 additions and 6 deletions

View File

@ -9,7 +9,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ExampleBuilder;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.*; import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spi.service.contexts.SecurityContext;
@ -55,9 +57,12 @@ public class YudaoSwaggerAutoConfiguration {
.paths(PathSelectors.any()) .paths(PathSelectors.any())
.build() .build()
.securitySchemes(securitySchemes()) .securitySchemes(securitySchemes())
.globalRequestParameters(globalRequestParameters())
.securityContexts(securityContexts()); .securityContexts(securityContexts());
} }
// ========== apiInfo ==========
/** /**
* API 摘要信息 * API 摘要信息
*/ */
@ -70,6 +75,8 @@ public class YudaoSwaggerAutoConfiguration {
.build(); .build();
} }
// ========== securitySchemes ==========
/** /**
* 安全模式这里配置通过请求头 Authorization 传递 token 参数 * 安全模式这里配置通过请求头 Authorization 传递 token 参数
*/ */
@ -98,4 +105,12 @@ public class YudaoSwaggerAutoConfiguration {
return new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")}; return new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")};
} }
// ========== globalRequestParameters ==========
private static List<RequestParameter> globalRequestParameters() {
RequestParameterBuilder tenantParameter = new RequestParameterBuilder().name("tenant-id").description("租户编号")
.in(ParameterType.HEADER).example(new ExampleBuilder().value(1L).build());
return Collections.singletonList(tenantParameter.build());
}
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.pay.test; package cn.iocoder.yudao.module.pay.test;
import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
import cn.iocoder.yudao.framework.test.config.RedisTestConfiguration;
import org.redisson.spring.starter.RedissonAutoConfiguration; import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;

View File

@ -44,7 +44,7 @@ public interface TenantMapper extends BaseMapperX<TenantDO> {
return selectOne(TenantDO::getName, name); return selectOne(TenantDO::getName, name);
} }
default Integer selectCountByPackageId(Long packageId) { default Long selectCountByPackageId(Long packageId) {
return selectCount(TenantDO::getPackageId, packageId); return selectCount(TenantDO::getPackageId, packageId);
} }

View File

@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -71,6 +72,7 @@ public class MenuServiceImpl implements MenuService {
@Resource @Resource
private PermissionService permissionService; private PermissionService permissionService;
@Resource @Resource
@Lazy // 延迟避免循环依赖报错
private TenantService tenantService; private TenantService tenantService;
@Resource @Resource

View File

@ -95,7 +95,7 @@ public interface TenantService extends TenantFrameworkService {
* @param packageId 租户套餐编号 * @param packageId 租户套餐编号
* @return 租户数量 * @return 租户数量
*/ */
Integer getTenantCountByPackageId(Long packageId); Long getTenantCountByPackageId(Long packageId);
/** /**
* 获得使用指定套餐的租户数组 * 获得使用指定套餐的租户数组

View File

@ -301,7 +301,7 @@ public class TenantServiceImpl implements TenantService {
} }
@Override @Override
public Integer getTenantCountByPackageId(Long packageId) { public Long getTenantCountByPackageId(Long packageId) {
return tenantMapper.selectCountByPackageId(packageId); return tenantMapper.selectCountByPackageId(packageId);
} }

View File

@ -102,7 +102,7 @@ public class TenantPackageServiceImplTest extends BaseDbUnitTest {
// 准备参数 // 准备参数
Long id = dbTenantPackage.getId(); Long id = dbTenantPackage.getId();
// mock 租户未使用该套餐 // mock 租户未使用该套餐
when(tenantService.getTenantCountByPackageId(eq(id))).thenReturn(0); when(tenantService.getTenantCountByPackageId(eq(id))).thenReturn(0L);
// 调用 // 调用
tenantPackageService.deleteTenantPackage(id); tenantPackageService.deleteTenantPackage(id);
@ -127,7 +127,7 @@ public class TenantPackageServiceImplTest extends BaseDbUnitTest {
// 准备参数 // 准备参数
Long id = dbTenantPackage.getId(); Long id = dbTenantPackage.getId();
// mock 租户在使用该套餐 // mock 租户在使用该套餐
when(tenantService.getTenantCountByPackageId(eq(id))).thenReturn(1); when(tenantService.getTenantCountByPackageId(eq(id))).thenReturn(1L);
// 调用, 并断言异常 // 调用, 并断言异常
assertServiceException(() -> tenantPackageService.deleteTenantPackage(id), TENANT_PACKAGE_USED); assertServiceException(() -> tenantPackageService.deleteTenantPackage(id), TENANT_PACKAGE_USED);

View File

@ -431,7 +431,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
tenantMapper.insert(dbTenant2);// @Sql: 先插入出一条存在的数据 tenantMapper.insert(dbTenant2);// @Sql: 先插入出一条存在的数据
// 调用 // 调用
Integer count = tenantService.getTenantCountByPackageId(1L); Long count = tenantService.getTenantCountByPackageId(1L);
assertEquals(1, count); assertEquals(1, count);
} }