大量代码调整和优化

This commit is contained in:
shimingxy
2024-07-22 16:24:58 +08:00
parent 32b0f69c50
commit 363703f21e
288 changed files with 5872 additions and 1155 deletions
@@ -21,7 +21,7 @@ import java.util.Collection;
import java.util.List;
import org.dromara.maxkey.authn.session.Session;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
@@ -23,7 +23,7 @@ import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.dromara.maxkey.authn.SignPrincipal;
import org.dromara.maxkey.crypto.jwt.HMAC512Service;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.util.StrUtils;
import org.dromara.maxkey.web.WebContext;
import org.joda.time.DateTime;
@@ -21,7 +21,7 @@ import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.dromara.maxkey.entity.HistoryLogin;
import org.dromara.maxkey.entity.history.HistoryLogin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -20,7 +20,7 @@ package org.dromara.maxkey.authn.session;
import java.time.LocalDateTime;
import java.util.List;
import org.dromara.maxkey.entity.HistoryLogin;
import org.dromara.maxkey.entity.history.HistoryLogin;
import org.dromara.maxkey.persistence.redis.RedisConnection;
import org.dromara.maxkey.persistence.redis.RedisConnectionFactory;
import org.slf4j.Logger;
@@ -20,7 +20,7 @@ package org.dromara.maxkey.authn.session;
import java.time.LocalDateTime;
import java.util.List;
import org.dromara.maxkey.entity.HistoryLogin;
import org.dromara.maxkey.entity.history.HistoryLogin;
public interface SessionManager {
@@ -25,8 +25,8 @@ import java.util.Date;
import java.util.List;
import org.dromara.maxkey.constants.ConstsPersistence;
import org.dromara.maxkey.entity.HistoryLogin;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.history.HistoryLogin;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.persistence.redis.RedisConnectionFactory;
import org.dromara.maxkey.util.DateUtils;
import org.slf4j.Logger;
@@ -24,7 +24,7 @@ import org.dromara.maxkey.authn.SignPrincipal;
import org.dromara.maxkey.authn.jwt.AuthTokenService;
import org.dromara.maxkey.authn.session.Session;
import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.util.AuthorizationHeaderUtils;
import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext;
@@ -18,7 +18,7 @@
package org.dromara.maxkey.authn.web;
import org.dromara.maxkey.authn.annotation.CurrentUser;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.web.WebConstants;
import org.springframework.core.MethodParameter;
import org.springframework.security.core.Authentication;
@@ -22,7 +22,7 @@ import java.io.IOException;
import org.dromara.maxkey.authn.annotation.CurrentUser;
import org.dromara.maxkey.entity.FileUpload;
import org.dromara.maxkey.entity.Message;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.persistence.service.FileUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -30,7 +30,7 @@ import org.dromara.maxkey.authn.web.AuthorizationUtils;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.constants.ConstsLoginType;
import org.dromara.maxkey.constants.ConstsStatus;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.dromara.maxkey.web.WebConstants;
@@ -25,7 +25,7 @@ import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.constants.ConstsLoginType;
import org.dromara.maxkey.entity.Institutions;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext;
import org.slf4j.Logger;
@@ -72,10 +72,8 @@ public class MfaAuthenticationProvider extends AbstractAuthenticationProvider {
loginCredential.getPrincipal(), getProviderName());
try {
_logger.debug("authentication " + loginCredential);
Institutions inst = (Institutions)WebContext.getAttribute(WebConstants.CURRENT_INST);
_logger.debug("authentication {}" , loginCredential);
emptyPasswordValid(loginCredential.getPassword());
UserInfo userInfo = null;
@@ -23,7 +23,7 @@ import org.dromara.maxkey.authn.realm.AbstractAuthenticationRealm;
import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.constants.ConstsLoginType;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.sms.SmsOtpAuthnService;
import org.dromara.maxkey.web.WebConstants;
@@ -26,8 +26,7 @@ import org.dromara.maxkey.authn.realm.AbstractAuthenticationRealm;
import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.constants.ConstsLoginType;
import org.dromara.maxkey.entity.Institutions;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext;
import org.slf4j.Logger;
@@ -73,16 +72,10 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
loginCredential.getPrincipal(), getProviderName());
try {
_logger.debug("authentication " + loginCredential);
Institutions inst = (Institutions)WebContext.getAttribute(WebConstants.CURRENT_INST);
_logger.debug("authentication {}" , loginCredential);
if(this.applicationConfig.getLoginConfig().isCaptcha()) {
captchaValid(loginCredential.getState(),loginCredential.getCaptcha());
}else if(!inst.getCaptcha().equalsIgnoreCase("NONE")) {
captchaValid(loginCredential.getState(),loginCredential.getCaptcha());
}
emptyPasswordValid(loginCredential.getPassword());
@@ -116,9 +109,9 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
WebConstants.LOGIN_RESULT.SUCCESS);
} catch (AuthenticationException e) {
_logger.error("Failed to authenticate user {} via {}: {}",
new Object[] { loginCredential.getPrincipal(),
loginCredential.getPrincipal(),
getProviderName(),
e.getMessage() });
e.getMessage() );
WebContext.setAttribute(
WebConstants.LOGIN_ERROR_SESSION_MESSAGE, e.getMessage());
} catch (Exception e) {
@@ -136,7 +129,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
* @param captcha String
* @throws ParseException
*/
protected void captchaValid(String state ,String captcha) throws ParseException {
protected void captchaValid(String state ,String captcha) {
// for basic
if(!authTokenService.validateCaptcha(state,captcha)) {
throw new BadCredentialsException(WebContext.getI18nValue("login.error.captcha"));
@@ -22,7 +22,7 @@ import org.dromara.maxkey.authn.provider.AbstractAuthenticationProvider;
import org.dromara.maxkey.authn.realm.AbstractAuthenticationRealm;
import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -22,9 +22,9 @@ import java.util.List;
import org.dromara.maxkey.authn.SignPrincipal;
import org.dromara.maxkey.authn.realm.ldap.LdapAuthenticationRealmService;
import org.dromara.maxkey.entity.Groups;
import org.dromara.maxkey.entity.HistoryLogin;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.history.HistoryLogin;
import org.dromara.maxkey.entity.idm.Groups;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.ip2location.Region;
import org.dromara.maxkey.persistence.repository.LoginHistoryRepository;
@@ -23,8 +23,8 @@ import org.dromara.maxkey.authn.realm.ldap.LdapAuthenticationRealmService;
import org.dromara.maxkey.constants.ConstsLoginType;
import org.dromara.maxkey.constants.ConstsStatus;
import org.dromara.maxkey.entity.ChangePassword;
import org.dromara.maxkey.entity.PasswordPolicy;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.cnf.CnfPasswordPolicy;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.persistence.repository.LoginHistoryRepository;
import org.dromara.maxkey.persistence.repository.LoginRepository;
@@ -128,7 +128,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
if (!passwordMatches) {
passwordPolicyValidator.plusBadPasswordCount(userInfo);
insertLoginHistory(userInfo, ConstsLoginType.LOCAL, "", "xe00000004", WebConstants.LOGIN_RESULT.PASSWORD_ERROE);
PasswordPolicy passwordPolicy = passwordPolicyValidator.getPasswordPolicyRepository().getPasswordPolicy();
CnfPasswordPolicy passwordPolicy = passwordPolicyValidator.getPasswordPolicyRepository().getPasswordPolicy();
if(userInfo.getBadPasswordCount()>=(passwordPolicy.getAttempts()/2)) {
throw new BadCredentialsException(
WebContext.getI18nValue("login.error.password.attempts",
@@ -21,7 +21,7 @@ import java.util.List;
import org.dromara.maxkey.authn.realm.AbstractAuthenticationRealm;
import org.dromara.maxkey.authn.realm.IAuthenticationServer;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -22,10 +22,10 @@ import java.util.concurrent.TimeUnit;
import org.dromara.maxkey.authn.realm.IAuthenticationServer;
import org.dromara.maxkey.crypto.password.PasswordReciprocal;
import org.dromara.maxkey.entity.LdapContext;
import org.dromara.maxkey.entity.cnf.CnfLdapContext;
import org.dromara.maxkey.ldap.ActiveDirectoryUtils;
import org.dromara.maxkey.ldap.LdapUtils;
import org.dromara.maxkey.persistence.service.LdapContextService;
import org.dromara.maxkey.persistence.service.CnfLdapContextService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
@@ -36,23 +36,23 @@ public class LdapAuthenticationRealmService {
.expireAfterWrite(60, TimeUnit.MINUTES)
.build();
LdapContextService ldapContextService;
CnfLdapContextService ldapContextService;
public LdapAuthenticationRealmService(LdapContextService ldapContextService) {
public LdapAuthenticationRealmService(CnfLdapContextService ldapContextService) {
this.ldapContextService = ldapContextService;
}
public LdapAuthenticationRealm getByInstId(String instId) {
LdapAuthenticationRealm authenticationRealm = ldapRealmStore.getIfPresent(instId);
if(authenticationRealm == null) {
List<LdapContext> ldapContexts =
List<CnfLdapContext> ldapContexts =
ldapContextService.find("where instid = ? and status = 1 ", new Object[]{instId}, new int[]{Types.VARCHAR});
authenticationRealm = new LdapAuthenticationRealm(false);
if(ldapContexts != null && ldapContexts.size()>0) {
authenticationRealm.setLdapSupport(true);
List<IAuthenticationServer> ldapAuthenticationServers = new ArrayList<IAuthenticationServer>();
for(LdapContext ldapContext : ldapContexts) {
for(CnfLdapContext ldapContext : ldapContexts) {
if(ldapContext.getProduct().equalsIgnoreCase("ActiveDirectory")) {
ActiveDirectoryServer ldapServer = new ActiveDirectoryServer();
ActiveDirectoryUtils ldapUtils = new ActiveDirectoryUtils(
@@ -24,7 +24,7 @@ import org.dromara.maxkey.authn.SignPrincipal;
import org.dromara.maxkey.authn.jwt.AuthTokenService;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.crypto.jwt.HMAC512Service;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.util.DateUtils;
import org.dromara.maxkey.web.WebContext;
import org.joda.time.DateTime;
@@ -29,7 +29,7 @@ import org.dromara.maxkey.constants.ConstsLoginType;
import org.dromara.maxkey.entity.Message;
import org.dromara.maxkey.entity.SocialsAssociate;
import org.dromara.maxkey.entity.SocialsProvider;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.uuid.UUID;
import org.dromara.maxkey.web.WebContext;
import org.slf4j.Logger;