|
|
@@ -93,49 +93,65 @@ public class SysLoginService
|
|
|
validateCaptcha(username, code, uuid);
|
|
|
}
|
|
|
|
|
|
- // 查询租户
|
|
|
+ TenantInfo tenantInfo = null;
|
|
|
+
|
|
|
+ // 默认使用主库
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
- TenantInfo tenantInfo = userService.getTenantInfo(tenantCode);
|
|
|
- if (BeanUtil.isEmpty(tenantInfo)) throw new ServiceException("企业不存在");
|
|
|
- if (!tenantInfo.getStatus().equals(1)) throw new ServiceException("企业已禁用");
|
|
|
|
|
|
- // 切租户库
|
|
|
- tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ // ===== 只有传了 tenantCode 才查询租户并切库 =====
|
|
|
+ if (StringUtils.isNotBlank(tenantCode))
|
|
|
+ {
|
|
|
+ // 查询租户(主库)
|
|
|
+ tenantInfo = userService.getTenantInfo(tenantCode);
|
|
|
+ if (BeanUtil.isEmpty(tenantInfo)) {
|
|
|
+ throw new ServiceException("企业不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!tenantInfo.getStatus().equals(1)) {
|
|
|
+ throw new ServiceException("企业已禁用");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 切租户库
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ }
|
|
|
|
|
|
try {
|
|
|
// 用户验证
|
|
|
Authentication authentication = null;
|
|
|
- try
|
|
|
- {
|
|
|
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
- authentication = authenticationManager
|
|
|
- .authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
- {
|
|
|
+ try {
|
|
|
+ // 该方法会去调用 UserDetailsServiceImpl.loadUserByUsername
|
|
|
+ authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof BadCredentialsException) {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
throw new ServiceException(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
+
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
- loginUser.setTenantId(tenantInfo.getId());
|
|
|
+
|
|
|
+ // 只有多租户登录才设置 tenantId
|
|
|
+ if (tenantInfo != null) {
|
|
|
+ loginUser.setTenantId(tenantInfo.getId());
|
|
|
+ }
|
|
|
+
|
|
|
recordLoginInfo(loginUser.getUser());
|
|
|
- // 生成token
|
|
|
+
|
|
|
+ // 生成 token
|
|
|
return tokenService.createToken(loginUser);
|
|
|
} finally {
|
|
|
+ // 防止线程串库(必须)
|
|
|
tenantDataSourceManager.clear();
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 校验验证码
|
|
|
*
|
|
|
@@ -187,79 +203,94 @@ public class SysLoginService
|
|
|
|
|
|
public boolean checkIsNeedCheck(String username, String password, String code, String uuid, String tenantCode)
|
|
|
{
|
|
|
+ TenantInfo tenantInfo = null;
|
|
|
|
|
|
- // (查租户)
|
|
|
+ // 默认使用主库
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
|
|
|
- // 查询数据库配置
|
|
|
- TenantInfo tenantInfo = userService.getTenantInfo(tenantCode);
|
|
|
- if (BeanUtil.isEmpty(tenantInfo)) throw new ServiceException("企业不存在");
|
|
|
-
|
|
|
- if (!tenantInfo.getStatus().equals(1)) throw new ServiceException("企业已禁用");
|
|
|
+ // ===== 只有传了 tenantCode 才走租户逻辑 =====
|
|
|
+ if (StringUtils.isNotBlank(tenantCode))
|
|
|
+ {
|
|
|
+ // 查询租户(主库)
|
|
|
+ tenantInfo = userService.getTenantInfo(tenantCode);
|
|
|
+ if (BeanUtil.isEmpty(tenantInfo))
|
|
|
+ {
|
|
|
+ throw new ServiceException("企业不存在");
|
|
|
+ }
|
|
|
+ if (!tenantInfo.getStatus().equals(1))
|
|
|
+ {
|
|
|
+ throw new ServiceException("企业已禁用");
|
|
|
+ }
|
|
|
|
|
|
- // 切到租户库
|
|
|
- tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ // 切到租户库
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
+ }
|
|
|
|
|
|
- try {
|
|
|
+ try
|
|
|
+ {
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
|
String captcha = redisCache.getCacheObject(verifyKey);
|
|
|
- //redisCache.deleteObject(verifyKey);
|
|
|
+ // redisCache.deleteObject(verifyKey);
|
|
|
+
|
|
|
if (captcha == null)
|
|
|
{
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
|
|
throw new CaptchaExpireException();
|
|
|
}
|
|
|
+
|
|
|
if (!code.equalsIgnoreCase(captcha))
|
|
|
{
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
|
throw new CaptchaException();
|
|
|
}
|
|
|
+
|
|
|
// 用户验证
|
|
|
Authentication authentication = null;
|
|
|
- try
|
|
|
- {
|
|
|
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
- authentication = authenticationManager
|
|
|
- .authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
+ try {
|
|
|
+ authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
+ } catch (Exception e)
|
|
|
{
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
- {
|
|
|
+ if (e instanceof BadCredentialsException) {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
throw new ServiceException(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
- //查询当前登录用户信息
|
|
|
+
|
|
|
+ // 查询当前登录用户信息(在当前数据源下)
|
|
|
SysUser sysUser = userService.selectUserById(loginUser.getUserId());
|
|
|
- Long[] userIds = new Long[]{236L, 246L, 247L, 253L,119L};
|
|
|
- for (Long userId : userIds) {
|
|
|
- if (userId.equals(sysUser.getUserId())){
|
|
|
+
|
|
|
+ Long[] userIds = new Long[]{236L, 246L, 247L, 253L, 119L};
|
|
|
+ for (Long userId : userIds)
|
|
|
+ {
|
|
|
+ if (userId.equals(sysUser.getUserId()))
|
|
|
+ {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 判断是否开启了扫码配置
|
|
|
- if (ObjectUtil.isEmpty(isNeedScan) || !isNeedScan){
|
|
|
+ if (ObjectUtil.isEmpty(isNeedScan) || !isNeedScan)
|
|
|
+ {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- //true → 要发短信验证码再登录
|
|
|
- //false → 直接登录
|
|
|
+ // true → 需要短信验证码
|
|
|
+ // false → 直接登录
|
|
|
return needCheck(sysUser);
|
|
|
- } finally {
|
|
|
- // 防止线程串库(必须)
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ // 防止线程串库
|
|
|
tenantDataSourceManager.clear();
|
|
|
-
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
public boolean needCheck(SysUser sysUser) {
|
|
|
|
|
|
|