Browse Source

👣 重构底层多租户

master
wangxiang 3 years ago
parent
commit
e7449ee1f8
  1. 6
      kicc-common/kicc-common-data/src/main/java/com/cloud/kicc/common/data/util/TenantContextHolder.java
  2. 12
      kicc-common/kicc-common-security/src/main/java/com/cloud/kicc/common/security/override/KiccCustomTokenServices.java

6
kicc-common/kicc-common-data/src/main/java/com/cloud/kicc/common/data/util/TenantContextHolder.java

@ -22,7 +22,7 @@ import java.util.concurrent.atomic.AtomicReference;
@UtilityClass @UtilityClass
public class TenantContextHolder { public class TenantContextHolder {
private final AtomicReference<String> contextHolder = new AtomicReference(); private final ThreadLocal<String> contextHolder = new ThreadLocal();
public void setTenant(String tenantIds) { public void setTenant(String tenantIds) {
contextHolder.set(tenantIds); contextHolder.set(tenantIds);
@ -33,6 +33,10 @@ public class TenantContextHolder {
return StrUtil.isNotBlank(contextHolder.get()) ? contextHolder.get() : tenantIds; return StrUtil.isNotBlank(contextHolder.get()) ? contextHolder.get() : tenantIds;
} }
public void clearTenant() {
contextHolder.remove();
}
/** /**
* 获取用户 * 获取用户
* 如果当前不存在用户,正常情况多租户拼接查询会报错 * 如果当前不存在用户,正常情况多租户拼接查询会报错

12
kicc-common/kicc-common-security/src/main/java/com/cloud/kicc/common/security/override/KiccCustomTokenServices.java

@ -30,9 +30,11 @@ import java.util.UUID;
public class KiccCustomTokenServices implements AuthorizationServerTokenServices, ResourceServerTokenServices, public class KiccCustomTokenServices implements AuthorizationServerTokenServices, ResourceServerTokenServices,
ConsumerTokenServices, InitializingBean { ConsumerTokenServices, InitializingBean {
private int refreshTokenValiditySeconds = 60 * 60 * 24 * 30; // default 30 days. // default 30 days.
private int refreshTokenValiditySeconds = 60 * 60 * 24 * 30;
private int accessTokenValiditySeconds = 60 * 60 * 12; // default 12 hours. // default 12 hours.
private int accessTokenValiditySeconds = 60 * 60 * 12;
private boolean supportRefreshToken = false; private boolean supportRefreshToken = false;
@ -62,10 +64,12 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
// 若已产生token,过期时删除相关token,执行下边的重新生成逻辑 // 若已产生token,过期时删除相关token,执行下边的重新生成逻辑
if (existingAccessToken != null) { if (existingAccessToken != null) {
tokenStore.removeAccessToken(existingAccessToken); tokenStore.removeAccessToken(existingAccessToken);
if (existingAccessToken.getRefreshToken() != null) { if (existingAccessToken.getRefreshToken() != null) {
refreshToken = existingAccessToken.getRefreshToken(); refreshToken = existingAccessToken.getRefreshToken();
tokenStore.removeRefreshToken(refreshToken); tokenStore.removeRefreshToken(refreshToken);
} }
} }
if (refreshToken == null) { if (refreshToken == null) {
@ -144,6 +148,7 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
return accessToken; return accessToken;
} }
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) { public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
return tokenStore.getAccessToken(authentication); return tokenStore.getAccessToken(authentication);
} }
@ -184,10 +189,12 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
return false; return false;
} }
@Override
public OAuth2AccessToken readAccessToken(String accessToken) { public OAuth2AccessToken readAccessToken(String accessToken) {
return tokenStore.readAccessToken(accessToken); return tokenStore.readAccessToken(accessToken);
} }
@Override
public OAuth2Authentication loadAuthentication(String accessTokenValue) public OAuth2Authentication loadAuthentication(String accessTokenValue)
throws AuthenticationException, InvalidTokenException { throws AuthenticationException, InvalidTokenException {
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue); OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
@ -228,6 +235,7 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
return clientAuth.getClientId(); return clientAuth.getClientId();
} }
@Override
public boolean revokeToken(String tokenValue) { public boolean revokeToken(String tokenValue) {
OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue); OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
if (accessToken == null) { if (accessToken == null) {

Loading…
Cancel
Save