|
|
@@ -2,11 +2,18 @@ package com.fs.framework.web.service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fs.common.enums.DataSourceType;
|
|
|
import com.fs.common.service.WechatLoginService;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.framework.datasource.DynamicDataSource;
|
|
|
+import com.fs.framework.datasource.DynamicDataSourceContextHolder;
|
|
|
+import com.fs.framework.datasource.TenantDataSourceManager;
|
|
|
+import com.fs.tenant.domain.TenantInfo;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
@@ -77,7 +84,7 @@ public class SysLoginService
|
|
|
* @param uuid 唯一标识
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public String login(String username, String password, String code, String uuid)
|
|
|
+ public String login(String username, String password, String code, String uuid, String tenantCode)
|
|
|
{
|
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
|
|
// 验证码开关
|
|
|
@@ -85,34 +92,66 @@ public class SysLoginService
|
|
|
{
|
|
|
validateCaptcha(username, code, uuid);
|
|
|
}
|
|
|
- // 用户验证
|
|
|
- Authentication authentication = null;
|
|
|
- try
|
|
|
+
|
|
|
+ TenantInfo tenantInfo = null;
|
|
|
+
|
|
|
+ // 默认使用主库
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
+
|
|
|
+ // ===== 只有传了 tenantCode 才查询租户并切库 =====
|
|
|
+ if (StringUtils.isNotBlank(tenantCode))
|
|
|
{
|
|
|
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
- authentication = authenticationManager
|
|
|
- .authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
+ // 查询租户(主库)
|
|
|
+ tenantInfo = userService.getTenantInfo(tenantCode);
|
|
|
+ if (BeanUtil.isEmpty(tenantInfo)) {
|
|
|
+ throw new ServiceException("企业不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!tenantInfo.getStatus().equals(1)) {
|
|
|
+ throw new ServiceException("企业已禁用");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 切租户库
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
}
|
|
|
- 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();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 用户验证
|
|
|
+ Authentication authentication = null;
|
|
|
+ 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 {
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
- 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();
|
|
|
+
|
|
|
+ // 只有多租户登录才设置 tenantId
|
|
|
+ if (tenantInfo != null) {
|
|
|
+ loginUser.setTenantId(tenantInfo.getId());
|
|
|
}
|
|
|
+
|
|
|
+ recordLoginInfo(loginUser.getUser());
|
|
|
+
|
|
|
+ // 生成 token
|
|
|
+ return tokenService.createToken(loginUser);
|
|
|
+ } finally {
|
|
|
+ // 防止线程串库(必须)
|
|
|
+ tenantDataSourceManager.clear();
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
}
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
- LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
- recordLoginInfo(loginUser.getUser());
|
|
|
- // 生成token
|
|
|
- return tokenService.createToken(loginUser);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 校验验证码
|
|
|
*
|
|
|
@@ -159,62 +198,99 @@ public class SysLoginService
|
|
|
userService.updateUserProfile(user);
|
|
|
}
|
|
|
|
|
|
+ @Resource
|
|
|
+ private TenantDataSourceManager tenantDataSourceManager;
|
|
|
|
|
|
- public boolean checkIsNeedCheck(String username, String password, String code, String uuid)
|
|
|
+ public boolean checkIsNeedCheck(String username, String password, String code, String uuid, String tenantCode)
|
|
|
{
|
|
|
- String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
|
- String captcha = redisCache.getCacheObject(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))
|
|
|
+ TenantInfo tenantInfo = null;
|
|
|
+
|
|
|
+ // 默认使用主库
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
+
|
|
|
+ // ===== 只有传了 tenantCode 才走租户逻辑 =====
|
|
|
+ if (StringUtils.isNotBlank(tenantCode))
|
|
|
{
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
|
- throw new CaptchaException();
|
|
|
+ // 查询租户(主库)
|
|
|
+ tenantInfo = userService.getTenantInfo(tenantCode);
|
|
|
+ if (BeanUtil.isEmpty(tenantInfo))
|
|
|
+ {
|
|
|
+ throw new ServiceException("企业不存在");
|
|
|
+ }
|
|
|
+ if (!tenantInfo.getStatus().equals(1))
|
|
|
+ {
|
|
|
+ throw new ServiceException("企业已禁用");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 切到租户库
|
|
|
+ tenantDataSourceManager.switchTenant(tenantInfo);
|
|
|
}
|
|
|
- // 用户验证
|
|
|
- Authentication authentication = null;
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
- authentication = authenticationManager
|
|
|
- .authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
+ String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
|
+ String captcha = redisCache.getCacheObject(verifyKey);
|
|
|
+ // redisCache.deleteObject(verifyKey);
|
|
|
+
|
|
|
+ if (captcha == null)
|
|
|
{
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
- throw new UserPasswordNotMatchException();
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
|
|
+ throw new CaptchaExpireException();
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ if (!code.equalsIgnoreCase(captcha))
|
|
|
{
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
- throw new ServiceException(e.getMessage());
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
|
+ throw new CaptchaException();
|
|
|
}
|
|
|
- }
|
|
|
- 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())){
|
|
|
+
|
|
|
+ // 用户验证
|
|
|
+ Authentication authentication = null;
|
|
|
+ try {
|
|
|
+ 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 {
|
|
|
+ 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()))
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断是否开启了扫码配置
|
|
|
+ if (ObjectUtil.isEmpty(isNeedScan) || !isNeedScan)
|
|
|
+ {
|
|
|
return false;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 判断是否开启了扫码配置
|
|
|
- if (ObjectUtil.isEmpty(isNeedScan) || !isNeedScan){
|
|
|
- return false;
|
|
|
+ // true → 需要短信验证码
|
|
|
+ // false → 直接登录
|
|
|
+ return needCheck(sysUser);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ // 防止线程串库
|
|
|
+ tenantDataSourceManager.clear();
|
|
|
+ DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
|
|
|
}
|
|
|
-
|
|
|
- //true → 要发短信验证码再登录
|
|
|
- //false → 直接登录
|
|
|
- return needCheck(sysUser);
|
|
|
}
|
|
|
+
|
|
|
public boolean needCheck(SysUser sysUser) {
|
|
|
|
|
|
|