From c20e9a5dc79a642a1d59a3b4b98c9742fa58125b Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期五, 19 一月 2024 14:04:09 +0800
Subject: [PATCH] 代码提交
---
ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java | 104 +++++++++++++++++++++++-----------------------------
1 files changed, 46 insertions(+), 58 deletions(-)
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
index 3dcc042..6e79b6e 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
@@ -3,9 +3,11 @@
import javax.annotation.Resource;
import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.exception.user.WxOpenIDNotFindException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.wxopenidConfig.WxOpenIDAuthenticationToken;
+import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
@@ -31,12 +33,11 @@
/**
* 鐧诲綍鏍¢獙鏂规硶
- *
+ *
* @author ruoyi
*/
@Component
-public class SysLoginService
-{
+public class SysLoginService {
@Autowired
private TokenService tokenService;
@@ -45,47 +46,49 @@
@Autowired
private RedisCache redisCache;
-
+
@Autowired
private ISysUserService userService;
@Autowired
private ISysConfigService configService;
+
+ @Autowired
+ private SysUserMapper sysUserMapper;
+
/**
* 鐧诲綍楠岃瘉
- *
+ *
* @param username 鐢ㄦ埛鍚�
* @param password 瀵嗙爜
- * @param code 楠岃瘉鐮�
- * @param uuid 鍞竴鏍囪瘑
+ * @param code 楠岃瘉鐮�
+ * @param uuid 鍞竴鏍囪瘑
* @return 缁撴灉
*/
- public String login(String username, String password, String code, String uuid, String openId)
- {
+ public String login(String username, String password, String code, String uuid, String openId) {
+ SysUser sysUser = sysUserMapper.selectUserByUserName(username);
+ if (sysUser.getErrorNums() >= 3) {
+ //鏍¢獙澶辫触娆℃暟
+ throw new BaseException("浣犵殑瀵嗙爜杈撳叆閿欒娆℃暟杩囧锛岃鑱旂郴绠$悊鍛樺鐞�");
+ }
+
boolean captchaOnOff = configService.selectCaptchaOnOff();
// 楠岃瘉鐮佸紑鍏�
- if (captchaOnOff)
- {
+ if (captchaOnOff) {
validateCaptcha(username, code, uuid);
}
// 鐢ㄦ埛楠岃瘉
Authentication authentication = null;
- try
- {
+ try {
// 璇ユ柟娉曚細鍘昏皟鐢║serDetailsServiceImpl.loadUserByUsername
- authentication = authenticationManager
- .authenticate(new UsernamePasswordAuthenticationToken(username, password));
- }
- catch (Exception e)
- {
- if (e instanceof BadCredentialsException)
- {
+ 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")));
+ sysUserMapper.updateErrorNums(sysUser);
throw new UserPasswordNotMatchException();
- }
- else
- {
+ } else {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
throw new ServiceException(e.getMessage());
}
@@ -97,52 +100,44 @@
//鏇存柊openid
String userOpenId = user.getOpenId();
- if(StringUtils.isEmpty(openId))
- {
+ if (StringUtils.isEmpty(openId)) {
recordLoginInfo(loginUser.getUserId());
- }else
- {
- if(StringUtils.isNotEmpty(userOpenId))
- {
+ } else {
+ if (StringUtils.isNotEmpty(userOpenId)) {
//褰撴湰娆$櫥褰曠殑寰俊OPENID涓嶇瓑浜庣涓�娆$粦瀹氱殑寰俊OPENID灏变笉鍏佽鐧诲綍锛岄槻姝㈠悓涓�涓处鍙峰寰俊鐧诲綍
- if(!userOpenId.equals(openId))
- {
+ if (!userOpenId.equals(openId)) {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "璇ヨ处鎴峰凡缁忕粦瀹氬叾浠栬澶囷紝椤诲厛瑙i櫎缁戝畾鍐嶇櫥褰曪紝璇疯窡绯荤粺绠$悊鍛樿仈绯伙紒"));
throw new ServiceException("璇ヨ处鎴峰凡缁忕粦瀹氬叾浠栬澶囷紝椤诲厛瑙i櫎缁戝畾鍐嶇櫥褰曪紝璇疯窡绯荤粺绠$悊鍛樿仈绯伙紒");
- }else
- {
+ } else {
recordLoginInfo(loginUser.getUserId());
}
- }else
- {
+ } else {
recordLoginInfo(loginUser.getUserId(), openId);
}
}
-
+ //娓呴櫎澶辫触娆℃暟
+ sysUserMapper.cleanErrorNums(username);
// 鐢熸垚token
return tokenService.createToken(loginUser);
}
/**
* 鏍¢獙楠岃瘉鐮�
- *
+ *
* @param username 鐢ㄦ埛鍚�
- * @param code 楠岃瘉鐮�
- * @param uuid 鍞竴鏍囪瘑
+ * @param code 楠岃瘉鐮�
+ * @param uuid 鍞竴鏍囪瘑
* @return 缁撴灉
*/
- public void validateCaptcha(String username, String code, String uuid)
- {
+ public void validateCaptcha(String username, String code, String uuid) {
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
String captcha = redisCache.getCacheObject(verifyKey);
redisCache.deleteObject(verifyKey);
- if (captcha == null)
- {
+ if (captcha == null) {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
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")));
throw new CaptchaException();
}
@@ -155,20 +150,15 @@
* @return 缁撴灉
*/
- public AjaxResult wxopenidLogin(String openid)
- {
+ public AjaxResult wxopenidLogin(String openid) {
// 鐢ㄦ埛楠岃瘉
Authentication authentication = null;
- try
- {
+ try {
// 璇ユ柟娉曚細鍘昏皟鐢║serDetailsServiceImpl.loadUserByUsername
- authentication = authenticationManager
- .authenticate(new WxOpenIDAuthenticationToken(openid));
- }
- catch (Exception e)
- {
+ authentication = authenticationManager.authenticate(new WxOpenIDAuthenticationToken(openid));
+ } catch (Exception e) {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(openid, Constants.LOGIN_FAIL, e.getMessage()));
throw new WxOpenIDNotFindException();
@@ -189,8 +179,7 @@
*
* @param userId 鐢ㄦ埛ID
*/
- public void recordLoginInfo(Long userId)
- {
+ public void recordLoginInfo(Long userId) {
SysUser sysUser = new SysUser();
sysUser.setUserId(userId);
sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
@@ -199,8 +188,7 @@
}
- public void recordLoginInfo(Long userId, String openId)
- {
+ public void recordLoginInfo(Long userId, String openId) {
SysUser sysUser = new SysUser();
sysUser.setUserId(userId);
sysUser.setOpenId(openId);
--
Gitblit v1.9.3