From 940bdec33a4c2a6b52d1497e6eeffddb1b6b4585 Mon Sep 17 00:00:00 2001 From: DYL0109 <dn18191638832@163.com> Date: 星期五, 18 四月 2025 19:17:25 +0800 Subject: [PATCH] Merge pull request #73 from zhitan-cloud/develop1.0 --- zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java | 72 ++++++++++++++++++++++++++---------- 1 files changed, 52 insertions(+), 20 deletions(-) diff --git a/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java b/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java index d881545..89ddfce 100644 --- a/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java +++ b/zhitan-framework/src/main/java/com/zhitan/framework/web/service/SysLoginService.java @@ -1,15 +1,5 @@ package com.zhitan.framework.web.service; -import javax.annotation.Resource; - -import com.zhitan.framework.manager.AsyncManager; -import com.zhitan.framework.manager.factory.AsyncFactory; -import com.zhitan.framework.security.context.AuthenticationContextHolder; -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 com.zhitan.common.constant.CacheConstants; import com.zhitan.common.constant.Constants; import com.zhitan.common.constant.UserConstants; @@ -17,21 +7,28 @@ import com.zhitan.common.core.domain.model.LoginUser; import com.zhitan.common.core.redis.RedisCache; import com.zhitan.common.exception.ServiceException; -import com.zhitan.common.exception.user.BlackListException; -import com.zhitan.common.exception.user.CaptchaException; -import com.zhitan.common.exception.user.CaptchaExpireException; -import com.zhitan.common.exception.user.UserNotExistsException; -import com.zhitan.common.exception.user.UserPasswordNotMatchException; +import com.zhitan.common.exception.user.*; import com.zhitan.common.utils.DateUtils; import com.zhitan.common.utils.MessageUtils; import com.zhitan.common.utils.StringUtils; import com.zhitan.common.utils.ip.IpUtils; +import com.zhitan.framework.manager.AsyncManager; +import com.zhitan.framework.manager.factory.AsyncFactory; +import com.zhitan.framework.security.context.AuthenticationContextHolder; +import com.zhitan.framework.security.single.SingleAuthenticationToken; import com.zhitan.system.service.ISysConfigService; import com.zhitan.system.service.ISysUserService; +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 zhitan */ @Component @@ -45,7 +42,7 @@ @Resource private RedisCache redisCache; - + @Resource private ISysUserService userService; @@ -54,7 +51,7 @@ /** * 鐧诲綍楠岃瘉 - * + * * @param username 鐢ㄦ埛鍚� * @param password 瀵嗙爜 * @param code 楠岃瘉鐮� @@ -64,7 +61,7 @@ public String login(String username, String password, String code, String uuid) { // 楠岃瘉鐮佹牎楠� - validateCaptcha(username, code, uuid); +// validateCaptcha(username, code, uuid); // 鐧诲綍鍓嶇疆鏍¢獙 loginPreCheck(username, password); // 鐢ㄦ埛楠岃瘉 @@ -102,7 +99,7 @@ /** * 鏍¢獙楠岃瘉鐮� - * + * * @param username 鐢ㄦ埛鍚� * @param code 楠岃瘉鐮� * @param uuid 鍞竴鏍囪瘑 @@ -178,4 +175,39 @@ sysUser.setLoginDate(DateUtils.getNowDate()); userService.updateUserProfile(sysUser); } + + + /** + * 鐧诲綍楠岃瘉 + * @param username + * @return 缁撴灉 + */ + public String loginNoCode(String username) + { + // 鐢ㄦ埛楠岃瘉 + Authentication authentication = null; + try + { + authentication = authenticationManager + .authenticate(new SingleAuthenticationToken(username)); + } + catch (Exception e) + { + if (e instanceof BadCredentialsException) + { + AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); + throw new UserPasswordNotMatchException(); + } + else + { + AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constants.LOGIN_FAIL, e.getMessage())); + throw new ServiceException(e.getMessage()); + } + } + AsyncManager.me().execute(AsyncFactory.recordLoginInfo(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); + LoginUser loginUser = (LoginUser) authentication.getPrincipal(); + recordLoginInfo(loginUser.getUserId()); + // 鐢熸垚token + return tokenService.createToken(loginUser); + } } -- Gitblit v1.9.3