|
@@ -1,13 +1,11 @@
|
|
package com.ruoyi.framework.web.service;
|
|
package com.ruoyi.framework.web.service;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
-import org.springframework.security.authentication.BadCredentialsException;
|
|
|
|
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
-import org.springframework.security.core.Authentication;
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
import com.ruoyi.common.constant.Constants;
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
+import com.ruoyi.common.constant.UserConstants;
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
@@ -17,21 +15,32 @@ import com.ruoyi.common.exception.user.CaptchaExpireException;
|
|
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
|
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
import com.ruoyi.common.utils.ServletUtils;
|
|
import com.ruoyi.common.utils.ServletUtils;
|
|
import com.ruoyi.common.utils.ip.IpUtils;
|
|
import com.ruoyi.common.utils.ip.IpUtils;
|
|
import com.ruoyi.framework.manager.AsyncManager;
|
|
import com.ruoyi.framework.manager.AsyncManager;
|
|
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
|
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
import com.ruoyi.system.service.ISysConfigService;
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
+import org.springframework.security.authentication.BadCredentialsException;
|
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 登录校验方法
|
|
* 登录校验方法
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @author ruoyi
|
|
* @author ruoyi
|
|
*/
|
|
*/
|
|
@Component
|
|
@Component
|
|
-public class SysLoginService
|
|
|
|
-{
|
|
|
|
|
|
+@Slf4j
|
|
|
|
+public class SysLoginService {
|
|
@Autowired
|
|
@Autowired
|
|
private TokenService tokenService;
|
|
private TokenService tokenService;
|
|
|
|
|
|
@@ -40,47 +49,90 @@ public class SysLoginService
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private RedisCache redisCache;
|
|
private RedisCache redisCache;
|
|
-
|
|
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private ISysUserService userService;
|
|
private ISysUserService userService;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private ISysConfigService configService;
|
|
private ISysConfigService configService;
|
|
|
|
|
|
|
|
+ @Value("${system.security}")
|
|
|
|
+ private String thirdSecurity;
|
|
|
|
+
|
|
|
|
+ @Value("${third.userLogin}")
|
|
|
|
+ private String userLogin;
|
|
|
|
+
|
|
|
|
+ @Value("${token.filterUser}")
|
|
|
|
+ private String filterUser;
|
|
|
|
+
|
|
|
|
+ @Value("${third.userTokenInfo}")
|
|
|
|
+ private String dajiaoguan_token;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 登录验证
|
|
* 登录验证
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param username 用户名
|
|
* @param username 用户名
|
|
* @param password 密码
|
|
* @param password 密码
|
|
- * @param code 验证码
|
|
|
|
- * @param uuid 唯一标识
|
|
|
|
|
|
+ * @param code 验证码
|
|
|
|
+ * @param uuid 唯一标识
|
|
* @return 结果
|
|
* @return 结果
|
|
*/
|
|
*/
|
|
- public String login(String username, String password, String code, String uuid)
|
|
|
|
- {
|
|
|
|
|
|
+ public String login(String username, String password, String code, String uuid) {
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
|
// 验证码开关
|
|
// 验证码开关
|
|
- if (captchaOnOff)
|
|
|
|
- {
|
|
|
|
|
|
+ if (captchaOnOff) {
|
|
validateCapcha(username, code, uuid);
|
|
validateCapcha(username, code, uuid);
|
|
}
|
|
}
|
|
// 用户验证
|
|
// 用户验证
|
|
Authentication authentication = null;
|
|
Authentication authentication = null;
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ try {
|
|
|
|
+ if ("third".equals(thirdSecurity) && !StrUtil.contains(filterUser, username)) {
|
|
|
|
+ // 走第三方的校验规则,同时将第三方的password设置为系统自动生成的
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
+ jsonObject.set("account", username);
|
|
|
|
+ jsonObject.set("password", password);
|
|
|
|
+ String post = HttpUtil.post(userLogin, jsonObject.toString());
|
|
|
|
+ JSONObject postJson = JSONUtil.parseObj(post);
|
|
|
|
+ if (postJson.getInt("code") == 200) {
|
|
|
|
+ // 成功
|
|
|
|
+ password = Constants.DEFAULT_PASSWORD;
|
|
|
|
+ // 保存用户
|
|
|
|
+ if (UserConstants.UNIQUE.equals(userService.checkUserNameUnique(username))) {
|
|
|
|
+ // 查询第三方用户信息
|
|
|
|
+ String getResult = HttpUtil.createGet(dajiaoguan_token).header("Authorization", Constants.TOKEN_PREFIX + postJson.getStr("data")).execute().body();
|
|
|
|
+ JSONObject getJson = JSONUtil.parseObj(getResult);
|
|
|
|
+ int infoCode = getJson.getInt("code");
|
|
|
|
+ if (infoCode == 1) {
|
|
|
|
+ SysUser sysUser = new SysUser();
|
|
|
|
+ // 将此用户新增入系统
|
|
|
|
+ JSONObject data = getJson.getJSONObject("data");
|
|
|
|
+ sysUser.setUserId(data.getLong("userId"));
|
|
|
|
+ sysUser.setUserName(username);
|
|
|
|
+ sysUser.setDeptId(data.getLong("depId"));
|
|
|
|
+ sysUser.setPhonenumber(data.getStr("phone"));
|
|
|
|
+ sysUser.setDelFlag("0");
|
|
|
|
+ sysUser.setStatus("0");
|
|
|
|
+ sysUser.setNickName(data.getStr("name"));
|
|
|
|
+ // 新加入的用户属于普通用户角色
|
|
|
|
+ sysUser.setRoleIds(new Long[]{2L});
|
|
|
|
+ sysUser.setPassword(SecurityUtils.encryptPassword(password));
|
|
|
|
+ userService.insertUser(sysUser);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ log.info("第三方校验登录失败:{}", postJson);
|
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
|
+ throw new UserPasswordNotMatchException();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
authentication = authenticationManager
|
|
authentication = authenticationManager
|
|
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ if (e instanceof BadCredentialsException) {
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
throw new UserPasswordNotMatchException();
|
|
throw new UserPasswordNotMatchException();
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ } else {
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
throw new CustomException(e.getMessage());
|
|
throw new CustomException(e.getMessage());
|
|
}
|
|
}
|
|
@@ -94,24 +146,21 @@ public class SysLoginService
|
|
|
|
|
|
/**
|
|
/**
|
|
* 校验验证码
|
|
* 校验验证码
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param username 用户名
|
|
* @param username 用户名
|
|
- * @param code 验证码
|
|
|
|
- * @param uuid 唯一标识
|
|
|
|
|
|
+ * @param code 验证码
|
|
|
|
+ * @param uuid 唯一标识
|
|
* @return 结果
|
|
* @return 结果
|
|
*/
|
|
*/
|
|
- public void validateCapcha(String username, String code, String uuid)
|
|
|
|
- {
|
|
|
|
|
|
+ public void validateCapcha(String username, String code, String uuid) {
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
String captcha = redisCache.getCacheObject(verifyKey);
|
|
String captcha = redisCache.getCacheObject(verifyKey);
|
|
redisCache.deleteObject(verifyKey);
|
|
redisCache.deleteObject(verifyKey);
|
|
- if (captcha == null)
|
|
|
|
- {
|
|
|
|
|
|
+ if (captcha == null) {
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
|
throw new CaptchaExpireException();
|
|
throw new CaptchaExpireException();
|
|
}
|
|
}
|
|
- if (!code.equalsIgnoreCase(captcha))
|
|
|
|
- {
|
|
|
|
|
|
+ if (!code.equalsIgnoreCase(captcha)) {
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
throw new CaptchaException();
|
|
throw new CaptchaException();
|
|
}
|
|
}
|
|
@@ -120,10 +169,10 @@ public class SysLoginService
|
|
/**
|
|
/**
|
|
* 记录登录信息
|
|
* 记录登录信息
|
|
*/
|
|
*/
|
|
- public void recordLoginInfo(SysUser user)
|
|
|
|
- {
|
|
|
|
|
|
+ public void recordLoginInfo(SysUser user) {
|
|
user.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
user.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
user.setLoginDate(DateUtils.getNowDate());
|
|
user.setLoginDate(DateUtils.getNowDate());
|
|
userService.updateUserProfile(user);
|
|
userService.updateUserProfile(user);
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|