✨ Introducing new features. 单元测试实现支持 租户隔离
✨ Introducing new features. 单元测试实现支持 租户隔离
This commit is contained in:
1 parent
5caa9254d9
commit
abf486b73e
3 files changed
+25
-4
No files matched your search
@@ -15,6 +15,10 @@
|
||||
<description>pigx oauth 2.0 单元测试工具类</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-data</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-security</artifactId>
|
||||
|
||||
+12
-1
@@ -2,9 +2,11 @@ package com.pig4cloud.pigx.test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
|
||||
import com.pig4cloud.pigx.test.annotation.WithMockOAuth2User;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
@@ -30,8 +32,13 @@ import org.springframework.web.client.RestTemplate;
|
||||
*/
|
||||
public class WithMockSecurityContextFactory implements WithSecurityContextFactory<WithMockOAuth2User> {
|
||||
|
||||
private final String TOKEN_URL = "http://pigx-auth/oauth/token";
|
||||
|
||||
@Override
|
||||
public SecurityContext createSecurityContext(WithMockOAuth2User oAuth2User) {
|
||||
// 0. 初始化环境
|
||||
TenantContextHolder.setTenantId(oAuth2User.tenant());
|
||||
|
||||
// 1. 请求认证中心获取token
|
||||
String token = getToken(oAuth2User);
|
||||
|
||||
@@ -71,8 +78,12 @@ public class WithMockSecurityContextFactory implements WithSecurityContextFactor
|
||||
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, requestHeaders);
|
||||
|
||||
RestTemplate restTemplate = SpringContextHolder.getBean(RestTemplate.class);
|
||||
|
||||
// 优先获取配置文件URI 配置
|
||||
String url = StrUtil.isBlank(clientProperties.getAccessTokenUri()) ? TOKEN_URL : clientProperties
|
||||
.getAccessTokenUri();
|
||||
String result = restTemplate
|
||||
.exchange(clientProperties.getAccessTokenUri(), HttpMethod.POST, requestEntity, String.class).getBody();
|
||||
.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody();
|
||||
return JSONUtil.parseObj(result).getStr("access_token");
|
||||
}
|
||||
|
||||
|
||||
+9
-3
@@ -1,11 +1,12 @@
|
||||
package com.pig4cloud.pigx.test.annotation;
|
||||
|
||||
import com.pig4cloud.pigx.test.WithMockSecurityContextFactory;
|
||||
import org.springframework.security.test.context.support.WithSecurityContext;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import com.pig4cloud.pigx.test.WithMockSecurityContextFactory;
|
||||
|
||||
import org.springframework.security.test.context.support.WithSecurityContext;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/9/22
|
||||
@@ -26,4 +27,9 @@ public @interface WithMockOAuth2User {
|
||||
*/
|
||||
String password() default "123456";
|
||||
|
||||
/**
|
||||
* 租户编号 默认租户编号1
|
||||
*/
|
||||
int tenant() default 1;
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user