【7.1.3】【bugfix】获取当前登录用户操作数据库的操作进行租户切换考虑

pull/22/head
fengshuonan 2021-09-22 11:27:19 +08:00
parent e98a77d165
commit 3868f282bf
2 changed files with 24 additions and 2 deletions

View File

@ -17,6 +17,14 @@
<dependencies>
<!-- 多数据源操作的api -->
<!-- 获取当前登录用户之前需要切数据源 -->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>ds-container-api</artifactId>
<version>${roses.version}</version>
</dependency>
<!--demo api-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>

View File

@ -24,6 +24,7 @@
*/
package cn.stylefeng.roses.kernel.auth.auth;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi;
@ -32,6 +33,8 @@ import cn.stylefeng.roses.kernel.auth.api.exception.AuthException;
import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum;
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.dsctn.api.context.CurrentDataSourceContext;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import cn.stylefeng.roses.kernel.system.api.UserServiceApi;
import org.springframework.stereotype.Service;
@ -113,8 +116,19 @@ public class LoginUserImpl implements LoginUserApi {
throw new AuthException(AuthExceptionEnum.AUTH_EXPIRED_ERROR);
}
// 从新组装一次loginUser保证loginUser中数据的时效性
return userServiceApi.getEffectiveLoginUser(session);
// 获取当前上下文的数据源名称
String dataSourceName = CurrentDataSourceContext.getDataSourceName();
// 如果当前用户有租户编码,则需要切下数据源
if (ObjectUtil.isNotEmpty(session.getTenantCode())) {
CurrentDataSourceContext.setDataSourceName(RuleConstants.TENANT_DB_PREFIX + session.getTenantCode());
}
try {
// 从新组装一次loginUser保证loginUser中数据的时效性
return userServiceApi.getEffectiveLoginUser(session);
} finally {
CurrentDataSourceContext.setDataSourceName(dataSourceName);
}
}
@Override