ludc
2023-11-28 4a2c9c11f6e660483b2a8ece1d76fc7ee138181d
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,7 @@
import com.vci.ubcs.common.cache.CacheNames;
import org.springblade.core.jwt.JwtUtil;
import org.springblade.core.jwt.props.JwtProperties;
import org.springblade.core.log.annotation.LoginOrLogoutLog;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
@@ -40,11 +42,13 @@
import com.vci.ubcs.system.entity.Tenant;
import com.vci.ubcs.system.feign.ISysClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
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;
@@ -97,6 +101,7 @@
   @Override
   @SneakyThrows
   @LoginOrLogoutLog("login")
   public BladeUserDetails loadUserByUsername(String username) {
      HttpServletRequest request = WebUtil.getRequest();
      // 获取用户绑定ID
@@ -134,9 +139,9 @@
      //超级管理员配置文件配置账号密码,实现登录, 默认租户id和超管为配置出来的
      if(tenantId.equals(this.tenantId) && userName.equals(username)){
         if (!this.userName.equals(username) && !password.equalsIgnoreCase(this.password)) {
         if (!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+"还有【"+(failCount-count)+"】次尝试机会!");
         }
         //如果ip比对后get抛出异常No value present就直接抛异常结束登录
         if(ipEnable){
@@ -192,13 +197,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+"还有【"+(failCount-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+"还有【"+(failCount-count)+"】次尝试机会!");
            }
            // 用户角色不存在
            if (Func.isEmpty(userInfo.getRoles())) {