【7.3.2】【auth】更新登录会话管理

pull/41/head
fengshuonan 2022-11-09 19:06:30 +08:00
parent 385f5fd783
commit ac6bab76a2
2 changed files with 26 additions and 10 deletions

View File

@ -33,10 +33,8 @@ import cn.stylefeng.roses.kernel.auth.api.cookie.SessionCookieCreator;
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.api.tenant.TenantCacheProxyFactory;
import cn.stylefeng.roses.kernel.message.api.expander.WebSocketConfigExpander;
import cn.stylefeng.roses.kernel.rule.callback.ConfigUpdateCallback;
import cn.stylefeng.roses.kernel.rule.constants.TenantConstants;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import javax.servlet.http.Cookie;
@ -94,19 +92,16 @@ public class DefaultSessionManager implements SessionManagerApi, ConfigUpdateCal
@Override
public void createSession(String token, LoginUser loginUser, Boolean createCookie) {
CacheOperatorApi<LoginUser> tenantCacheProxy = TenantCacheProxyFactory.createTenantCacheProxy(loginUser.getTenantCode(), loginUserCache);
CacheOperatorApi<Set<String>> allPlaceLoginTokenCacheProxy = TenantCacheProxyFactory.createTenantCacheProxy(loginUser.getTenantCode(), allPlaceLoginTokenCache);
// 装配用户信息的缓存
tenantCacheProxy.put(token, loginUser, sessionExpiredSeconds);
loginUserCache.put(token, loginUser, sessionExpiredSeconds);
// 装配用户token的缓存
Set<String> theUserTokens = allPlaceLoginTokenCacheProxy.get(loginUser.getUserId().toString());
Set<String> theUserTokens = allPlaceLoginTokenCache.get(loginUser.getUserId().toString());
if (theUserTokens == null) {
theUserTokens = new HashSet<>();
}
theUserTokens.add(token);
allPlaceLoginTokenCacheProxy.put(loginUser.getUserId().toString(), theUserTokens);
allPlaceLoginTokenCache.put(loginUser.getUserId().toString(), theUserTokens);
// 如果开启了cookie存储会话信息则需要给HttpServletResponse添加一个cookie
if (createCookie) {
@ -128,8 +123,7 @@ public class DefaultSessionManager implements SessionManagerApi, ConfigUpdateCal
@Override
public LoginUser getSession(String token) {
CacheOperatorApi<LoginUser> tenantCacheProxy = TenantCacheProxyFactory.createTenantCacheProxy(TenantConstants.MASTER_DATASOURCE_NAME, loginUserCache);
return tenantCacheProxy.get(token);
return loginUserCache.get(token);
}
@Override

View File

@ -142,6 +142,20 @@ public interface CacheOperatorApi<T> {
*/
String getCommonKeyPrefix();
/**
*
* <p>
* key::key
* <p>
* master:
*
* @author fengshuonan
* @date 2022/11/9 19:02
*/
default Boolean divideByTenant() {
return false;
}
/**
*
* <p>
@ -201,6 +215,14 @@ public interface CacheOperatorApi<T> {
*/
default String getTenantPrefix() {
// 缓存是否按租户维度切分
Boolean divideByTenantFlag = divideByTenant();
// 如果不按租户维度切分则默认都返回为master
if (!divideByTenantFlag) {
return TenantConstants.MASTER_DATASOURCE_NAME;
}
// 用户的租户前缀
String tenantPrefix = "";
try {