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; @@ -22,7 +22,7 @@ import java.util.concurrent.atomic.AtomicReference;
@UtilityClass
public class TenantContextHolder {
private final AtomicReference<String> contextHolder = new AtomicReference();
private final ThreadLocal<String> contextHolder = new ThreadLocal();
public void setTenant(String tenantIds) {
contextHolder.set(tenantIds);
@ -33,6 +33,10 @@ public class TenantContextHolder { @@ -33,6 +33,10 @@ public class TenantContextHolder {
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; @@ -30,9 +30,11 @@ import java.util.UUID;
public class KiccCustomTokenServices implements AuthorizationServerTokenServices, ResourceServerTokenServices,
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;
@ -62,10 +64,12 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices @@ -62,10 +64,12 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
// 若已产生token,过期时删除相关token,执行下边的重新生成逻辑
if (existingAccessToken != null) {
tokenStore.removeAccessToken(existingAccessToken);
if (existingAccessToken.getRefreshToken() != null) {
refreshToken = existingAccessToken.getRefreshToken();
tokenStore.removeRefreshToken(refreshToken);
}
}
if (refreshToken == null) {
@ -144,6 +148,7 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices @@ -144,6 +148,7 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
return accessToken;
}
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
return tokenStore.getAccessToken(authentication);
}
@ -184,10 +189,12 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices @@ -184,10 +189,12 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
return false;
}
@Override
public OAuth2AccessToken readAccessToken(String accessToken) {
return tokenStore.readAccessToken(accessToken);
}
@Override
public OAuth2Authentication loadAuthentication(String accessTokenValue)
throws AuthenticationException, InvalidTokenException {
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
@ -228,6 +235,7 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices @@ -228,6 +235,7 @@ public class KiccCustomTokenServices implements AuthorizationServerTokenServices
return clientAuth.getClientId();
}
@Override
public boolean revokeToken(String tokenValue) {
OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
if (accessToken == null) {

Loading…
Cancel
Save