mirror of https://gitee.com/stylefeng/roses
【7.3.2】【cache】更新租户缓存代理
parent
000fb3fb89
commit
5541550ce5
|
@ -54,6 +54,7 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
|
||||||
import cn.stylefeng.roses.kernel.auth.api.pojo.sso.SsoLoginCodeRequest;
|
import cn.stylefeng.roses.kernel.auth.api.pojo.sso.SsoLoginCodeRequest;
|
||||||
import cn.stylefeng.roses.kernel.auth.api.pojo.sso.SsoProperties;
|
import cn.stylefeng.roses.kernel.auth.api.pojo.sso.SsoProperties;
|
||||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||||
|
import cn.stylefeng.roses.kernel.cache.api.tenant.TenantCacheProxyFactory;
|
||||||
import cn.stylefeng.roses.kernel.demo.expander.DemoConfigExpander;
|
import cn.stylefeng.roses.kernel.demo.expander.DemoConfigExpander;
|
||||||
import cn.stylefeng.roses.kernel.jwt.JwtTokenOperator;
|
import cn.stylefeng.roses.kernel.jwt.JwtTokenOperator;
|
||||||
import cn.stylefeng.roses.kernel.jwt.api.context.JwtContext;
|
import cn.stylefeng.roses.kernel.jwt.api.context.JwtContext;
|
||||||
|
@ -302,7 +303,8 @@ public class AuthServiceImpl implements AuthServiceApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1.2 判断账号是否密码重试次数过多被冻结
|
// 1.2 判断账号是否密码重试次数过多被冻结
|
||||||
Integer loginErrorCount = loginErrorCountCacheApi.get(loginRequest.getAccount());
|
CacheOperatorApi<Integer> tenantCacheProxy = TenantCacheProxyFactory.createTenantCacheProxy(loginRequest.getTenantCode(), loginErrorCountCacheApi);
|
||||||
|
Integer loginErrorCount = tenantCacheProxy.get(loginRequest.getAccount());
|
||||||
if (loginErrorCount != null && loginErrorCount >= LoginCacheConstants.MAX_ERROR_LOGIN_COUNT) {
|
if (loginErrorCount != null && loginErrorCount >= LoginCacheConstants.MAX_ERROR_LOGIN_COUNT) {
|
||||||
throw new AuthException(AuthExceptionEnum.LOGIN_LOCKED);
|
throw new AuthException(AuthExceptionEnum.LOGIN_LOCKED);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,12 @@ import java.lang.reflect.Proxy;
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2022/11/8 0:45
|
* @date 2022/11/8 0:45
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("all")
|
public class TenantCacheProxy implements InvocationHandler {
|
||||||
public class TenantCacheProxy<T> implements InvocationHandler {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 被代理的缓存操作类
|
* 被代理的缓存操作类
|
||||||
*/
|
*/
|
||||||
private CacheOperatorApi<T> targetCacheObject = null;
|
private CacheOperatorApi<?> targetCacheObject = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 租户编码
|
* 租户编码
|
||||||
|
@ -34,7 +33,7 @@ public class TenantCacheProxy<T> implements InvocationHandler {
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2022/11/8 1:05
|
* @date 2022/11/8 1:05
|
||||||
*/
|
*/
|
||||||
public CacheOperatorApi<T> bindCacheObject(String tenantCode, CacheOperatorApi<T> target) {
|
public <T> CacheOperatorApi<T> bindCacheObject(String tenantCode, CacheOperatorApi<T> target) {
|
||||||
this.targetCacheObject = target;
|
this.targetCacheObject = target;
|
||||||
this.tenantCode = tenantCode;
|
this.tenantCode = tenantCode;
|
||||||
return (CacheOperatorApi<T>) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
|
return (CacheOperatorApi<T>) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package cn.stylefeng.roses.kernel.cache.api.tenant;
|
||||||
|
|
||||||
|
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 租户缓存动态代理
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/11/8 0:45
|
||||||
|
*/
|
||||||
|
public class TenantCacheProxyFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存放所有代理的缓存
|
||||||
|
*/
|
||||||
|
private static final Map<String, CacheOperatorApi<?>> proxyCacheMap = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建带租户控制的缓存代理
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2022/11/8 1:18
|
||||||
|
*/
|
||||||
|
public static <T> CacheOperatorApi<T> createTenantCacheProxy(String tenantCode, CacheOperatorApi<?> cacheOperatorApi) {
|
||||||
|
|
||||||
|
// 计算缓存的key
|
||||||
|
String key = tenantCode + cacheOperatorApi.getCommonKeyPrefix();
|
||||||
|
|
||||||
|
// 直接获取缓存中的代理
|
||||||
|
if (proxyCacheMap.containsKey(key)) {
|
||||||
|
return (CacheOperatorApi<T>) proxyCacheMap.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
TenantCacheProxy tenantCacheProxy = new TenantCacheProxy();
|
||||||
|
CacheOperatorApi<?> resultCacheOperator = tenantCacheProxy.bindCacheObject(tenantCode, cacheOperatorApi);
|
||||||
|
proxyCacheMap.put(key, resultCacheOperator);
|
||||||
|
return (CacheOperatorApi<T>) resultCacheOperator;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue