代码整合优化

This commit is contained in:
shimingxy
2024-12-18 10:47:35 +08:00
parent f19e92e2dc
commit 7844fc25bf
30 changed files with 592 additions and 707 deletions
@@ -97,7 +97,7 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
mobileCaptchaValid(loginCredential.getPassword(),userInfo);
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated
@@ -87,7 +87,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
isUserExist(loginCredential , userInfo);
//Validate PasswordPolicy
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(userInfo);
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo);
statusValid(loginCredential , userInfo);
@@ -95,7 +95,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated
@@ -61,9 +61,9 @@ public class TrustedAuthenticationProvider extends AbstractAuthenticationProvide
statusValid(loginCredential , loadeduserInfo);
if (loadeduserInfo != null) {
//Validate PasswordPolicy
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(loadeduserInfo);
authenticationRealm.getLoginRepository().passwordPolicyValid(loadeduserInfo);
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(loadeduserInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(loadeduserInfo);
Authentication authentication = createOnlineTicket(loginCredential,loadeduserInfo);
authenticationRealm.insertLoginHistory( loadeduserInfo,
@@ -29,8 +29,8 @@ 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.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext;
@@ -50,7 +50,7 @@ public abstract class AbstractAuthenticationRealm {
protected JdbcTemplate jdbcTemplate;
protected PasswordPolicyValidator passwordPolicyValidator;
protected PasswordPolicyValidatorService passwordPolicyValidatorService;
protected LoginRepository loginRepository;
@@ -74,8 +74,8 @@ public abstract class AbstractAuthenticationRealm {
this.jdbcTemplate = jdbcTemplate;
}
public PasswordPolicyValidator getPasswordPolicyValidator() {
return passwordPolicyValidator;
public PasswordPolicyValidatorService getPasswordPolicyValidatorService() {
return passwordPolicyValidatorService;
}
public LoginRepository getLoginRepository() {
@@ -27,8 +27,8 @@ 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.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext;
@@ -58,7 +58,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder,
PasswordPolicyValidator passwordPolicyValidator,
PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository,
HistoryLoginService historyLoginService,
UserInfoService userInfoService,
@@ -66,7 +66,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
JdbcTemplate jdbcTemplate) {
this.passwordEncoder =passwordEncoder;
this.passwordPolicyValidator=passwordPolicyValidator;
this.passwordPolicyValidatorService=passwordPolicyValidatorService;
this.loginRepository = loginRepository;
this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService;
@@ -76,7 +76,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder,
PasswordPolicyValidator passwordPolicyValidator,
PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository,
HistoryLoginService historyLoginService,
UserInfoService userInfoService,
@@ -84,7 +84,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
JdbcTemplate jdbcTemplate,
LdapAuthenticationRealmService ldapAuthenticationRealmService) {
this.passwordEncoder = passwordEncoder;
this.passwordPolicyValidator = passwordPolicyValidator;
this.passwordPolicyValidatorService = passwordPolicyValidatorService;
this.loginRepository = loginRepository;
this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService;
@@ -126,9 +126,9 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
}
_logger.debug("passwordvalid : {}" , passwordMatches);
if (!passwordMatches) {
passwordPolicyValidator.plusBadPasswordCount(userInfo);
loginRepository.plusBadPasswordCount(userInfo);
insertLoginHistory(userInfo, ConstsLoginType.LOCAL, "", "xe00000004", WebConstants.LOGIN_RESULT.PASSWORD_ERROE);
CnfPasswordPolicy passwordPolicy = passwordPolicyValidator.getPasswordPolicyRepository().getPasswordPolicy();
CnfPasswordPolicy passwordPolicy = passwordPolicyValidatorService.getPasswordPolicy();
if(userInfo.getBadPasswordCount()>=(passwordPolicy.getAttempts()/2)) {
throw new BadCredentialsException(
WebContext.getI18nValue("login.error.password.attempts",
@@ -26,7 +26,10 @@ import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.password.sms.SmsOtpAuthnService;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.persistence.service.impl.PasswordPolicyValidatorServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -99,13 +102,15 @@ public class AuthnProviderAutoConfiguration {
}
@Bean
PasswordPolicyValidator passwordPolicyValidator(JdbcTemplate jdbcTemplate,MessageSource messageSource) {
return new PasswordPolicyValidator(jdbcTemplate,messageSource);
PasswordPolicyValidatorService passwordPolicyValidatorService(
CnfPasswordPolicyService cnfPasswordPolicyService,
MessageSource messageSource) {
return new PasswordPolicyValidatorServiceImpl(cnfPasswordPolicyService,messageSource);
}
@Bean
LoginRepository loginRepository(JdbcTemplate jdbcTemplate) {
return new LoginRepository(jdbcTemplate);
LoginRepository loginRepository(UserInfoService userInfoService,CnfPasswordPolicyService cnfPasswordPolicyService,JdbcTemplate jdbcTemplate) {
return new LoginRepository(userInfoService,cnfPasswordPolicyService,jdbcTemplate);
}
}
@@ -84,7 +84,7 @@ public class AppAuthenticationProvider extends AbstractAuthenticationProvider {
UserInfo userInfo = loadUserInfo(loginCredential.getUsername(), loginCredential.getPassword());
//Validate PasswordPolicy
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(userInfo);
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo);
statusValid(loginCredential, userInfo);
@@ -92,7 +92,7 @@ public class AppAuthenticationProvider extends AbstractAuthenticationProvider {
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential, userInfo);
// user authenticated
@@ -89,13 +89,13 @@ public class MfaAuthenticationProvider extends AbstractAuthenticationProvider {
mfacaptchaValid(loginCredential.getOtpCaptcha(),userInfo);
//Validate PasswordPolicy
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(userInfo);
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo);
//Match password
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated
@@ -97,7 +97,7 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
mobileCaptchaValid(loginCredential.getPassword(),userInfo);
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated
@@ -87,7 +87,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
isUserExist(loginCredential , userInfo);
//Validate PasswordPolicy
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(userInfo);
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo);
statusValid(loginCredential , userInfo);
@@ -95,7 +95,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(userInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated
@@ -61,9 +61,9 @@ public class TrustedAuthenticationProvider extends AbstractAuthenticationProvide
statusValid(loginCredential , loadeduserInfo);
if (loadeduserInfo != null) {
//Validate PasswordPolicy
authenticationRealm.getPasswordPolicyValidator().passwordPolicyValid(loadeduserInfo);
authenticationRealm.getLoginRepository().passwordPolicyValid(loadeduserInfo);
//apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getPasswordPolicyValidator().applyPasswordPolicy(loadeduserInfo);
authenticationRealm.getLoginRepository().applyPasswordPolicy(loadeduserInfo);
Authentication authentication = createOnlineTicket(loginCredential,loadeduserInfo);
authenticationRealm.insertLoginHistory( loadeduserInfo,
@@ -29,8 +29,8 @@ 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.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext;
@@ -50,7 +50,7 @@ public abstract class AbstractAuthenticationRealm {
protected JdbcTemplate jdbcTemplate;
protected PasswordPolicyValidator passwordPolicyValidator;
protected PasswordPolicyValidatorService passwordPolicyValidatorService;
protected LoginRepository loginRepository;
@@ -74,8 +74,8 @@ public abstract class AbstractAuthenticationRealm {
this.jdbcTemplate = jdbcTemplate;
}
public PasswordPolicyValidator getPasswordPolicyValidator() {
return passwordPolicyValidator;
public PasswordPolicyValidatorService getPasswordPolicyValidatorService() {
return passwordPolicyValidatorService;
}
public LoginRepository getLoginRepository() {
@@ -27,8 +27,8 @@ 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.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext;
@@ -58,7 +58,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder,
PasswordPolicyValidator passwordPolicyValidator,
PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository,
HistoryLoginService historyLoginService,
UserInfoService userInfoService,
@@ -66,7 +66,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
JdbcTemplate jdbcTemplate) {
this.passwordEncoder =passwordEncoder;
this.passwordPolicyValidator=passwordPolicyValidator;
this.passwordPolicyValidatorService=passwordPolicyValidatorService;
this.loginRepository = loginRepository;
this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService;
@@ -76,7 +76,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder,
PasswordPolicyValidator passwordPolicyValidator,
PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository,
HistoryLoginService historyLoginService,
UserInfoService userInfoService,
@@ -84,7 +84,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
JdbcTemplate jdbcTemplate,
LdapAuthenticationRealmService ldapAuthenticationRealmService) {
this.passwordEncoder = passwordEncoder;
this.passwordPolicyValidator = passwordPolicyValidator;
this.passwordPolicyValidatorService = passwordPolicyValidatorService;
this.loginRepository = loginRepository;
this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService;
@@ -126,9 +126,9 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
}
_logger.debug("passwordvalid : {}" , passwordMatches);
if (!passwordMatches) {
passwordPolicyValidator.plusBadPasswordCount(userInfo);
loginRepository.plusBadPasswordCount(userInfo);
insertLoginHistory(userInfo, ConstsLoginType.LOCAL, "", "xe00000004", WebConstants.LOGIN_RESULT.PASSWORD_ERROE);
CnfPasswordPolicy passwordPolicy = passwordPolicyValidator.getPasswordPolicyRepository().getPasswordPolicy();
CnfPasswordPolicy passwordPolicy = passwordPolicyValidatorService.getPasswordPolicy();
if(userInfo.getBadPasswordCount()>=(passwordPolicy.getAttempts()/2)) {
throw new BadCredentialsException(
WebContext.getI18nValue("login.error.password.attempts",
@@ -28,7 +28,10 @@ import org.dromara.maxkey.authn.support.rememberme.JdbcRemeberMeManager;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.password.sms.SmsOtpAuthnService;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.persistence.service.impl.PasswordPolicyValidatorServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -133,13 +136,15 @@ public class AuthnProviderAutoConfiguration {
}
@Bean
PasswordPolicyValidator passwordPolicyValidator(JdbcTemplate jdbcTemplate,MessageSource messageSource) {
return new PasswordPolicyValidator(jdbcTemplate,messageSource);
PasswordPolicyValidatorService passwordPolicyValidatorService(
CnfPasswordPolicyService cnfPasswordPolicyService,
MessageSource messageSource) {
return new PasswordPolicyValidatorServiceImpl(cnfPasswordPolicyService,messageSource);
}
@Bean
LoginRepository loginRepository(JdbcTemplate jdbcTemplate) {
return new LoginRepository(jdbcTemplate);
LoginRepository loginRepository(UserInfoService userInfoService,CnfPasswordPolicyService cnfPasswordPolicyService,JdbcTemplate jdbcTemplate) {
return new LoginRepository(userInfoService,cnfPasswordPolicyService,jdbcTemplate);
}
/**