| | |
| | | package cn.gistack.auth.config; |
| | | |
| | | import cn.gistack.auth.granter.PhoneTokenGranter; |
| | | import cn.gistack.auth.utils.TokenUtil; |
| | | import cn.gistack.common.cache.CacheNames; |
| | | import com.alibaba.nacos.common.utils.StringUtils; |
| | |
| | | public static final String CUSTOM_LOGIN_SMS = "CUSTOM_LOGIN_SMS"; |
| | | public static final String CUSTOM_LOGIN_WX = "CUSTOM_LOGIN_WX"; |
| | | |
| | | public static final String CUSTOM_LOGIN_OAUTH = "CUSTOM_LOGIN_OAUTH"; |
| | | |
| | | public static final String OAUTH = "oauth"; |
| | | public static final String PHONE = "phone"; |
| | | public static final String WX = "wx"; |
| | | |
| | | @Autowired |
| | | private BladeRedis bladeRedis; |
| | | |
| | |
| | | throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); |
| | | } else { |
| | | String password = authentication.getCredentials().toString(); |
| | | // 手机号登录时不验证密码 |
| | | if(!CUSTOM_LOGIN_SMS.equals(password)&&!CUSTOM_LOGIN_WX.equals(password)){ |
| | | HttpServletRequest request = WebUtil.getRequest(); |
| | | String grantType = request.getParameter(TokenUtil.GRANT_TYPE_KEY); |
| | | // 获取租户ID |
| | | String headerTenant = request.getHeader(TokenUtil.TENANT_HEADER_KEY); |
| | | String paramTenant = request.getParameter(TokenUtil.TENANT_PARAM_KEY); |
| | | // 指定租户ID |
| | | String tenantId = StringUtils.isBlank(headerTenant) ? paramTenant : headerTenant; |
| | | String detailsPassword = userDetails.getPassword().substring(7); |
| | | if (!detailsPassword.equals(DigestUtil.hex(password))) { |
| | | int count = getFailCount(tenantId, userDetails.getUsername()); |
| | | // 用户存在但密码错误,超过次数则锁定账号 |
| | | if (grantType != null && !grantType.equals(TokenUtil.REFRESH_TOKEN_KEY)) { |
| | | setFailCount(tenantId, userDetails.getUsername(), count); |
| | | throw new UserDeniedAuthorizationException(TokenUtil.USER_NOT_FOUND); |
| | | } |
| | | this.logger.debug("Failed to authenticate since password does not match stored value"); |
| | | throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); |
| | | } |
| | | |
| | | HttpServletRequest request = WebUtil.getRequest(); |
| | | String grantType = request.getParameter(TokenUtil.GRANT_TYPE_KEY); |
| | | // 获取租户ID |
| | | String headerTenant = request.getHeader(TokenUtil.TENANT_HEADER_KEY); |
| | | String paramTenant = request.getParameter(TokenUtil.TENANT_PARAM_KEY); |
| | | // 指定租户ID |
| | | String tenantId = StringUtils.isBlank(headerTenant) ? paramTenant : headerTenant; |
| | | |
| | | // 手机号短信登录时不验证密码、微信登录时不验证密码、单点登录时不验证密码 |
| | | if (grantType.equals(PHONE) || grantType.equals(WX) || grantType.equals(OAUTH)) { |
| | | // 成功则清除登录错误次数 |
| | | delFailCount(tenantId, userDetails.getUsername()); |
| | | return; |
| | | } |
| | | |
| | | String detailsPassword = userDetails.getPassword().substring(7); |
| | | if (!detailsPassword.equals(DigestUtil.hex(password))) { |
| | | int count = getFailCount(tenantId, userDetails.getUsername()); |
| | | // 用户存在但密码错误,超过次数则锁定账号 |
| | | if (grantType != null && !grantType.equals(TokenUtil.REFRESH_TOKEN_KEY)) { |
| | | setFailCount(tenantId, userDetails.getUsername(), count); |
| | | throw new UserDeniedAuthorizationException(TokenUtil.USER_NOT_FOUND); |
| | | } |
| | | this.logger.debug("Failed to authenticate since password does not match stored value"); |
| | | throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); |
| | | } |
| | | // 成功则清除登录错误次数 |
| | | delFailCount(tenantId, userDetails.getUsername()); |
| | | } |
| | | } |
| | | |