From 46de46ed851dc909f0ff355e9c6e3422efb5b4ad Mon Sep 17 00:00:00 2001
From: ludc
Date: 星期二, 30 四月 2024 14:27:10 +0800
Subject: [PATCH] 集成代码调整

---
 Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/service/BladeUserDetailsServiceImpl.java |   46 +++++++++++++++++++++++++++++-----------------
 1 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/service/BladeUserDetailsServiceImpl.java b/Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/service/BladeUserDetailsServiceImpl.java
index 4bd2fd2..ab06f3a 100644
--- a/Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/service/BladeUserDetailsServiceImpl.java
+++ b/Source/UBCS/ubcs-auth/src/main/java/com/vci/ubcs/auth/service/BladeUserDetailsServiceImpl.java
@@ -18,6 +18,7 @@
 
 import com.alibaba.nacos.common.utils.StringUtils;
 import com.vci.ubcs.auth.constant.AuthConstant;
+import com.vci.ubcs.auth.support.BladePasswordEncoderFactories;
 import com.vci.ubcs.auth.utils.TokenUtil;
 import com.vci.ubcs.system.user.entity.User;
 import com.vci.ubcs.system.user.entity.UserInfo;
@@ -30,6 +31,8 @@
 import com.vci.ubcs.common.cache.CacheNames;
 import org.springblade.core.jwt.JwtUtil;
 import org.springblade.core.jwt.props.JwtProperties;
+import org.springblade.core.launch.constant.TokenConstant;
+import org.springblade.core.log.annotation.LoginOrLogoutLog;
 import org.springblade.core.redis.cache.BladeRedis;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.support.Kv;
@@ -44,6 +47,7 @@
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
 import org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException;
 import org.springframework.stereotype.Service;
 
@@ -62,9 +66,9 @@
 public class BladeUserDetailsServiceImpl implements UserDetailsService {
 
 	/**
-	 * 鍏佽閿欒娆℃暟
+	 * 榛樿鍏佽閿欒娆℃暟
 	 */
-	//public static final Integer FAIL_COUNT = 5;
+	public static final Integer FAIL_COUNT = 5;
 	public static final String FAIL_COUNT_VALUE = "account.failCount";
 
 	/**
@@ -77,7 +81,6 @@
 	private final BladeRedis bladeRedis;
 
 	private final JwtProperties jwtProperties;
-
 
 	/**
 	 * 瓒呯骇绠$悊鍛樹俊鎭�
@@ -97,6 +100,7 @@
 
 	@Override
 	@SneakyThrows
+	@LoginOrLogoutLog("login")
 	public BladeUserDetails loadUserByUsername(String username) {
 		HttpServletRequest request = WebUtil.getRequest();
 		// 鑾峰彇鐢ㄦ埛缁戝畾ID
@@ -105,6 +109,7 @@
 		// 鑾峰彇绉熸埛ID
 		String headerTenant = request.getHeader(TokenUtil.TENANT_HEADER_KEY);
 		String paramTenant = request.getParameter(TokenUtil.TENANT_PARAM_KEY);
+		String refreshToken = request.getParameter(TokenConstant.REFRESH_TOKEN);
 		String password = request.getParameter(TokenUtil.PASSWORD_KEY);
 		String grantType = request.getParameter(TokenUtil.GRANT_TYPE_KEY);
 		// 鍒ゆ柇绉熸埛璇锋眰澶�
@@ -125,24 +130,27 @@
 		// 鍒ゆ柇鐧诲綍鏄惁閿佸畾
 		int count = getFailCount(tenantId, username);
 		//涓洪槻姝㈠彇鍊间负绌烘姤閿欑殑鎯呭喌锛屽綋涓虹┖鐨勬椂鍊欑粰榛樿鍙负5娆′究閿佸畾鐢ㄦ埛鐧诲綍锛屼絾鏄竴鑸緢闅惧嚭鐜拌繖绉嶆儏鍐碉紝鍥犱负鎴慺eign閲岄潰鏄粰浜嗛粯璁ゅ瘑鐮佺瓥鐣ユ煡璇㈢殑
-		int failCountValue = Func.isEmpty(strategy) ? 5:Func.toInt(strategy.getLockingNum());
+		int failCountValue = Func.isEmpty(strategy) ? FAIL_COUNT:Func.toInt(strategy.getLockingNum());
+		//int failCount = Func.toInt(ParamCache.getValue(FAIL_COUNT_VALUE), failCountValue);
 
-		int failCount = Func.toInt(ParamCache.getValue(FAIL_COUNT_VALUE), failCountValue);
-
-		if (count >= failCount) {
+		if (count >= failCountValue) {
 			throw new UserDeniedAuthorizationException(TokenUtil.USER_HAS_TOO_MANY_FAILS);
 		}
-		//瓒呯骇绠$悊鍛橀厤缃枃浠堕厤缃处鍙峰瘑鐮侊紝瀹炵幇鐧诲綍, 榛樿绉熸埛id涓�000000
-		if(tenantId.equals(this.tenantId)){
-			if (!this.userName.equals(username) && !password.equalsIgnoreCase(this.password)) {
+
+		//瓒呯骇绠$悊鍛橀厤缃枃浠堕厤缃处鍙峰瘑鐮侊紝瀹炵幇鐧诲綍, 榛樿绉熸埛id鍜岃秴绠′负閰嶇疆鍑烘潵鐨�
+		if(tenantId.equals(this.tenantId) && userName.equals(username)){
+			if (Func.isBlank(refreshToken)/*鍒锋柊token涓嶇敤鏍¢獙瀵嗙爜*/
+				&& (!this.userName.equals(username) || !BladePasswordEncoderFactories.createDelegatingPasswordEncoder().encode(password).equalsIgnoreCase(AuthConstant.ENCRYPT+this.password))
+			) {
 				setFailCount(tenantId, username, count,strategy.getLockingTime());
-				throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND);
+				throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND+"杩樻湁銆�"+(failCountValue-count)+"銆戞灏濊瘯鏈轰細!");
 			}
 			//濡傛灉ip姣斿鍚巊et鎶涘嚭寮傚父No value present灏辩洿鎺ユ姏寮傚父缁撴潫鐧诲綍
 			if(ipEnable){
-				Log.debug("褰撳墠璁块棶IP锛�"+getIpAddress(request));
+				// Log.debug("褰撳墠璁块棶IP锛�"+getIpAddress(request));
+				Log.debug("褰撳墠璁块棶IP锛�"+WebUtil.getIP(request));
 				try {
-					ips.stream().filter(s -> s.equals(getIpAddress(request))).findFirst().get();
+					ips.stream().filter(s -> s.equals(WebUtil.getIP(request))).findFirst().get();
 				} catch (Exception e){
 					throw new UserDeniedAuthorizationException(TokenUtil.IP_NOT_FOND);
 				}
@@ -191,13 +199,17 @@
 				// 鐢ㄦ埛涓嶅瓨鍦�,浣嗘彁绀虹敤鎴峰悕涓庡瘑鐮侀敊璇苟閿佸畾璐﹀彿
 				if (user == null || user.getId() == null) {
 					setFailCount(tenantId, username, count,strategy.getLockingTime());
-					throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND);
+					throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND+"杩樻湁銆�"+(failCountValue-count)+"銆戞灏濊瘯鏈轰細!");
+				}
+				// 鐢ㄦ埛鐘舵�佷负1鏃惰鏄庤鐢ㄦ埛琚攣瀹�
+				if(user.getUserStatus() == 1){
+					throw new InvalidGrantException(TokenUtil.USER_LOCK);
 				}
 				String hex = DigestUtil.hex(password);
 				// 鐢ㄦ埛瀛樺湪浣嗗瘑鐮侀敊璇�,瓒呰繃娆℃暟鍒欓攣瀹氳处鍙�
 				if (grantType != null && !grantType.equals(TokenUtil.REFRESH_TOKEN_KEY) && !user.getPassword().equals(hex)) {
 					setFailCount(tenantId, username, count,strategy.getLockingTime());
-					throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND);
+					throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND+"杩樻湁銆�"+(failCountValue-count)+"銆戞灏濊瘯鏈轰細!");
 				}
 				// 鐢ㄦ埛瑙掕壊涓嶅瓨鍦�
 				if (Func.isEmpty(userInfo.getRoles())) {
@@ -220,8 +232,8 @@
 				//濉厖鐢ㄦ埛淇℃伅鍒扮敤鎴蜂俊鎭墿灞曠被
 				BladeUserDetails bladeUserDetails = new BladeUserDetails(user.getId(),
 					user.getTenantId(), StringPool.EMPTY, user.getName(), user.getRealName(), user.getDeptId(), user.getPostId(), user.getRoleId(), Func.join(userInfo.getRoles()), Func.toStr(user.getAvatar(), TokenUtil.DEFAULT_AVATAR),
-					username, AuthConstant.ENCRYPT + user.getPassword(), userInfo.getDetail(), true, true, true, true,
-					AuthorityUtils.commaSeparatedStringToAuthorityList(Func.join(result.getData().getRoles())),user.getStrategyUpdateStatus());
+					username, AuthConstant.ENCRYPT + user.getPassword(), userInfo.getDetail(),user.getSecretGrade(), true, true, true, true,
+					AuthorityUtils.commaSeparatedStringToAuthorityList(Func.join(result.getData().getRoles())),user.getStrategyUpdateStatus(),tenant.getData().getTenantName(),user.getDeptName(),user.getEmail());
 				return bladeUserDetails;
 			} else {
 				throw new UsernameNotFoundException(result.getMsg());

--
Gitblit v1.9.3