增加 Tenant Redis 的实现

pull/2/head
YunaiV 2021-12-06 10:18:36 +08:00
parent 1ce2c09f47
commit df9b06843f
9 changed files with 117 additions and 16 deletions

View File

@ -17,7 +17,7 @@ public interface WebFilterOrderEnum {
// OrderedRequestContextFilter 默认为 -105用于国际化上下文等等
int TENANT_FILTER = - 100; // 需要保证在 ApiAccessLogFilter 前面
int TENANT_CONTEXT_FILTER = - 100; // 需要保证在 ApiAccessLogFilter 前面
int API_ACCESS_LOG_FILTER = -90; // 需要保证在 RequestBodyCacheFilter 后面

View File

@ -98,4 +98,16 @@ public class RedisKeyDefine {
this(memo, keyTemplate, keyType, valueType, timeoutType, Duration.ZERO);
}
/**
* Key
*
* {@link String#format(String, Object...)}
*
* @param args
* @return Key
*/
public String formatKey(Object... args) {
return String.format(keyTemplate, args);
}
}

View File

@ -28,10 +28,6 @@ public class LoginUser implements UserDetails {
* {@link UserTypeEnum}
*/
private Integer userType;
/**
*
*/
private Long deptId;
/**
*
*/
@ -53,22 +49,28 @@ public class LoginUser implements UserDetails {
*
*/
private Integer status;
/**
*
*/
private Long tenantId;
// ========== UserTypeEnum.ADMIN 独有字段 ==========
// TODO 芋艿:可以通过定义一个 Map<String, String> exts 的方式,去除管理员的字段。不过这样会导致系统比较复杂,所以暂时不去掉先;
/**
*
*/
private Long deptId;
/**
*
*/
private Set<Long> postIds;
/**
* group
*/
// TODO jason这个字段改成 postCodes 明确更好哈
private List<String> groups;
// TODO @芋艿:怎么去掉 deptId
@Override
@JsonIgnore// 避免序列化
public String getPassword() {

View File

@ -33,6 +33,11 @@
<artifactId>yudao-spring-boot-starter-mybatis</artifactId>
</dependency>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-redis</artifactId>
</dependency>
<!-- Job 定时任务相关 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>
@ -44,6 +49,13 @@
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-mq</artifactId>
</dependency>
<!-- Test 测试相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.framework.tenant.config;
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
import cn.iocoder.yudao.framework.tenant.core.web.TenantWebFilter;
import cn.iocoder.yudao.framework.tenant.core.web.TenantContextWebFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
@ -13,10 +13,10 @@ import org.springframework.context.annotation.Bean;
public class YudaoTenantWebAutoConfiguration {
@Bean
public FilterRegistrationBean<TenantWebFilter> tenantWebFilter() {
FilterRegistrationBean<TenantWebFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TenantWebFilter());
registrationBean.setOrder(WebFilterOrderEnum.TENANT_FILTER);
public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter() {
FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TenantContextWebFilter());
registrationBean.setOrder(WebFilterOrderEnum.TENANT_CONTEXT_FILTER);
return registrationBean;
}

View File

@ -0,0 +1,47 @@
package cn.iocoder.yudao.framework.tenant.core.redis;
import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import java.time.Duration;
/**
* RedisKeyDefine
*
* Redis MySQL column WHERE tenant_id = ?
* Redis Key
* 1. Redis Key user:%d user:1 Redis Key user:%d:%d
* 2. Redis DAO 使 {@link #formatKey(Object...)} Redis Key
*
* 使 TenantRedisKeyDefine 使 Redis Key
* 1 2 Key
*
* @author
*/
public class TenantRedisKeyDefine extends RedisKeyDefine {
/**
* KEY
*/
private static final String KEY_TEMPLATE_SUFFIX = ":%d";
public TenantRedisKeyDefine(String memo, String keyTemplate, KeyTypeEnum keyType, Class<?> valueType, Duration timeout) {
super(memo, buildKeyTemplate(keyTemplate), keyType, valueType, timeout);
}
public TenantRedisKeyDefine(String memo, String keyTemplate, KeyTypeEnum keyType, Class<?> valueType, TimeoutTypeEnum timeoutType) {
super(memo, buildKeyTemplate(keyTemplate), keyType, valueType, timeoutType);
}
private static String buildKeyTemplate(String keyTemplate) {
return keyTemplate + KEY_TEMPLATE_SUFFIX;
}
@Override
public String formatKey(Object... args) {
args = ArrayUtil.append(args, TenantContextHolder.getTenantId());
return super.formatKey(args);
}
}

View File

@ -11,7 +11,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Web
* Context Web
* Header tenant-id {@link TenantContextHolder} DB
*
* Q tenant-id
@ -19,7 +19,7 @@ import java.io.IOException;
*
* @author
*/
public class TenantWebFilter extends OncePerRequestFilter {
public class TenantContextWebFilter extends OncePerRequestFilter {
private static final String HEADER_TENANT_ID = "tenant-id";

View File

@ -10,5 +10,6 @@
* 2Spring Security
* TransmittableThreadLocalSecurityContextHolderStrategy
* YudaoSecurityAutoConfiguration#securityContextHolderMethodInvokingFactoryBean()
* 6. Redis Redis Key
*/
package cn.iocoder.yudao.framework.tenant;

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.framework.tenant.core.redis;
import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class TenantRedisKeyDefineTest {
@Test
public void testFormatKey() {
Long tenantId = 30L;
TenantContextHolder.setTenantId(tenantId);
// 准备参数
TenantRedisKeyDefine define = new TenantRedisKeyDefine("", "user:%d:%d", RedisKeyDefine.KeyTypeEnum.HASH,
Object.class, RedisKeyDefine.TimeoutTypeEnum.FIXED);
Long userId = 10L;
Integer userType = 1;
// 调用
String key = define.formatKey(userId, userType);
// 断言
assertEquals("user:10:1:30", key);
}
}