♻️ 重构代码

This commit is contained in:
冷冷 committed 2018-10-04 13:43:50 +08:00
1 parent 4de4f6c349
commit ea6072eb4a
19 files changed
+125 -106

No files matched your search

+1 -1
View File
@@ -37,7 +37,7 @@
<!--工具类核心包 要从线程中获取租户ID-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-security</artifactId>
<artifactId>pigx-common-core</artifactId>
<version>${pigx.version}</version>
</dependency>
<!--缓存依赖-->
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.common.cache;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.common.core.util.TenantUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCache;
@@ -57,7 +57,7 @@ public class RedisAutoCacheManager extends RedisCacheManager {
if (cacheArray.length < 2) {
return super.createRedisCache(name, cacheConfig);
}
String cacheName = cacheArray[0] + ":" + SecurityUtils.getTenantId();
String cacheName = cacheArray[0] + ":" + TenantUtils.getTenantId();
if (cacheConfig != null) {
long cacheAge = Long.getLong(cacheArray[1], -1);
cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(cacheAge));
@@ -67,6 +67,6 @@ public class RedisAutoCacheManager extends RedisCacheManager {
@Override
public Cache getCache(String name) {
return super.getCache(name + ":" + SecurityUtils.getTenantId());
return super.getCache(name + ":" + TenantUtils.getTenantId());
}
}
+6
View File
@@ -77,5 +77,11 @@
<version>${jackson.modules}</version>
<type>pom</type>
</dependency>
<!--TTL-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>${ttl.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.core.util;
import com.alibaba.ttl.TransmittableThreadLocal;
/**
* @author lengleng
* @date 2018/10/4
* 租户工具类
*/
public class TenantUtils {
private static final ThreadLocal<Integer> THREAD_LOCAL_TENANT = new TransmittableThreadLocal<>();
/**
* TTL 设置租户ID
*
* @param tenantId
*/
public static void setTenantId(Integer tenantId) {
THREAD_LOCAL_TENANT.set(tenantId);
}
/**
* 获取TTL中的租户ID
*
* @return
*/
public static Integer getTenantId() {
return THREAD_LOCAL_TENANT.get();
}
}
-6
View File
@@ -51,11 +51,5 @@
<artifactId>pigx-upms-api</artifactId>
<version>${pigx.version}</version>
</dependency>
<!--TTL-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>${ttl.version}</version>
</dependency>
</dependencies>
</project>
@@ -18,7 +18,7 @@
package com.pig4cloud.pigx.common.security.feign;
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.common.core.util.TenantUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
@@ -31,10 +31,10 @@ import lombok.extern.slf4j.Slf4j;
public class PigxFeignTenantInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
if (SecurityUtils.getTenantId() == null) {
if (TenantUtils.getTenantId() == null) {
log.error("TTL 中的 租户ID为空,feign拦截器 >> 增强失败");
return;
}
requestTemplate.header(CommonConstant.TENANT_ID, SecurityUtils.getTenantId().toString());
requestTemplate.header(CommonConstant.TENANT_ID, TenantUtils.getTenantId().toString());
}
}
@@ -19,7 +19,7 @@ package com.pig4cloud.pigx.common.security.filter;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.common.core.util.TenantUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@@ -52,9 +52,9 @@ public class TenantIdTtlFilter extends GenericFilterBean {
log.debug("获取header中的租户ID为:{}", tenantId);
if (StrUtil.isNotBlank(tenantId)) {
SecurityUtils.setTenantId(Integer.parseInt(tenantId));
TenantUtils.setTenantId(Integer.parseInt(tenantId));
} else {
SecurityUtils.setTenantId(1);
TenantUtils.setTenantId(1);
}
filterChain.doFilter(request, response);
@@ -21,7 +21,6 @@ package com.pig4cloud.pigx.common.security.util;
import cn.hutool.core.util.StrUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import org.springframework.security.core.Authentication;
@@ -39,8 +38,6 @@ import java.util.List;
* @author L.cm
*/
public class SecurityUtils {
private static final ThreadLocal<Integer> THREAD_LOCAL_TENANT = new TransmittableThreadLocal<>();
/**
* 获取Authentication
*/
@@ -121,21 +118,4 @@ public class SecurityUtils {
return roleIds;
}
/**
* TTL 设置租户ID
*
* @param tenantId
*/
public static void setTenantId(Integer tenantId) {
THREAD_LOCAL_TENANT.set(tenantId);
}
/**
* 获取TTL中的租户ID
*
* @return
*/
public static Integer getTenantId() {
return THREAD_LOCAL_TENANT.get();
}
}