登录整合优化

This commit is contained in:
shimingxy 2024-12-19 09:27:31 +08:00
parent 7844fc25bf
commit 0d18f5d7df
21 changed files with 160 additions and 84 deletions

View File

@ -97,7 +97,7 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
mobileCaptchaValid(loginCredential.getPassword(),userInfo); mobileCaptchaValid(loginCredential.getPassword(),userInfo);
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo); authenticationRealm.getLoginService().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo); authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated // user authenticated

View File

@ -87,7 +87,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
isUserExist(loginCredential , userInfo); isUserExist(loginCredential , userInfo);
//Validate PasswordPolicy //Validate PasswordPolicy
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo); authenticationRealm.getLoginService().passwordPolicyValid(userInfo);
statusValid(loginCredential , userInfo); statusValid(loginCredential , userInfo);
@ -95,7 +95,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword()); authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo); authenticationRealm.getLoginService().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo); authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated // user authenticated

View File

@ -61,9 +61,9 @@ public class TrustedAuthenticationProvider extends AbstractAuthenticationProvide
statusValid(loginCredential , loadeduserInfo); statusValid(loginCredential , loadeduserInfo);
if (loadeduserInfo != null) { if (loadeduserInfo != null) {
//Validate PasswordPolicy //Validate PasswordPolicy
authenticationRealm.getLoginRepository().passwordPolicyValid(loadeduserInfo); authenticationRealm.getLoginService().passwordPolicyValid(loadeduserInfo);
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(loadeduserInfo); authenticationRealm.getLoginService().applyPasswordPolicy(loadeduserInfo);
Authentication authentication = createOnlineTicket(loginCredential,loadeduserInfo); Authentication authentication = createOnlineTicket(loginCredential,loadeduserInfo);
authenticationRealm.insertLoginHistory( loadeduserInfo, authenticationRealm.insertLoginHistory( loadeduserInfo,

View File

@ -28,8 +28,8 @@ import org.dromara.maxkey.entity.idm.Groups;
import org.dromara.maxkey.entity.idm.UserInfo; import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser; import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.ip2location.Region; import org.dromara.maxkey.ip2location.Region;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.HistoryLoginService; import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants; import org.dromara.maxkey.web.WebConstants;
@ -52,7 +52,7 @@ public abstract class AbstractAuthenticationRealm {
protected PasswordPolicyValidatorService passwordPolicyValidatorService; protected PasswordPolicyValidatorService passwordPolicyValidatorService;
protected LoginRepository loginRepository; protected LoginService loginService;
protected HistoryLoginService historyLoginService; protected HistoryLoginService historyLoginService;
@ -78,18 +78,18 @@ public abstract class AbstractAuthenticationRealm {
return passwordPolicyValidatorService; return passwordPolicyValidatorService;
} }
public LoginRepository getLoginRepository() { public LoginService getLoginService() {
return loginRepository; return loginService;
} }
public UserInfo loadUserInfo(String username, String password) { public UserInfo loadUserInfo(String username, String password) {
return loginRepository.find(username, password); return loginService.find(username, password);
} }
public abstract boolean passwordMatches(UserInfo userInfo, String password); public abstract boolean passwordMatches(UserInfo userInfo, String password);
public List<Groups> queryGroups(UserInfo userInfo) { public List<Groups> queryGroups(UserInfo userInfo) {
return loginRepository.queryGroups(userInfo); return loginService.queryGroups(userInfo);
} }
/** /**
@ -99,7 +99,7 @@ public abstract class AbstractAuthenticationRealm {
* @return ArrayList<GrantedAuthority> * @return ArrayList<GrantedAuthority>
*/ */
public List<GrantedAuthority> grantAuthority(UserInfo userInfo) { public List<GrantedAuthority> grantAuthority(UserInfo userInfo) {
return loginRepository.grantAuthority(userInfo); return loginService.grantAuthority(userInfo);
} }
/** /**
@ -109,7 +109,7 @@ public abstract class AbstractAuthenticationRealm {
* @return ArrayList<GrantedAuthority Apps> * @return ArrayList<GrantedAuthority Apps>
*/ */
public List<GrantedAuthority> queryAuthorizedApps(List<GrantedAuthority> grantedAuthoritys) { public List<GrantedAuthority> queryAuthorizedApps(List<GrantedAuthority> grantedAuthoritys) {
return loginRepository.queryAuthorizedApps(grantedAuthoritys); return loginService.queryAuthorizedApps(grantedAuthoritys);
} }
/** /**
@ -160,7 +160,7 @@ public abstract class AbstractAuthenticationRealm {
} }
historyLoginService.login(historyLogin); historyLoginService.login(historyLogin);
loginRepository.updateLastLogin(userInfo); loginService.updateLastLogin(userInfo);
return true; return true;
} }

View File

@ -26,8 +26,8 @@ import org.dromara.maxkey.entity.ChangePassword;
import org.dromara.maxkey.entity.cnf.CnfPasswordPolicy; import org.dromara.maxkey.entity.cnf.CnfPasswordPolicy;
import org.dromara.maxkey.entity.idm.UserInfo; import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser; import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.HistoryLoginService; import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants; import org.dromara.maxkey.web.WebConstants;
@ -59,7 +59,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm( public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder, PasswordEncoder passwordEncoder,
PasswordPolicyValidatorService passwordPolicyValidatorService, PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository, LoginService loginService,
HistoryLoginService historyLoginService, HistoryLoginService historyLoginService,
UserInfoService userInfoService, UserInfoService userInfoService,
IpLocationParser ipLocationParser, IpLocationParser ipLocationParser,
@ -67,7 +67,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
this.passwordEncoder =passwordEncoder; this.passwordEncoder =passwordEncoder;
this.passwordPolicyValidatorService=passwordPolicyValidatorService; this.passwordPolicyValidatorService=passwordPolicyValidatorService;
this.loginRepository = loginRepository; this.loginService = loginService;
this.historyLoginService = historyLoginService; this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService; this.userInfoService = userInfoService;
this.ipLocationParser = ipLocationParser; this.ipLocationParser = ipLocationParser;
@ -77,7 +77,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm( public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder, PasswordEncoder passwordEncoder,
PasswordPolicyValidatorService passwordPolicyValidatorService, PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository, LoginService loginService,
HistoryLoginService historyLoginService, HistoryLoginService historyLoginService,
UserInfoService userInfoService, UserInfoService userInfoService,
IpLocationParser ipLocationParser, IpLocationParser ipLocationParser,
@ -85,7 +85,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
LdapAuthenticationRealmService ldapAuthenticationRealmService) { LdapAuthenticationRealmService ldapAuthenticationRealmService) {
this.passwordEncoder = passwordEncoder; this.passwordEncoder = passwordEncoder;
this.passwordPolicyValidatorService = passwordPolicyValidatorService; this.passwordPolicyValidatorService = passwordPolicyValidatorService;
this.loginRepository = loginRepository; this.loginService = loginService;
this.historyLoginService = historyLoginService; this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService; this.userInfoService = userInfoService;
this.ipLocationParser = ipLocationParser; this.ipLocationParser = ipLocationParser;
@ -126,7 +126,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
} }
_logger.debug("passwordvalid : {}" , passwordMatches); _logger.debug("passwordvalid : {}" , passwordMatches);
if (!passwordMatches) { if (!passwordMatches) {
loginRepository.plusBadPasswordCount(userInfo); loginService.plusBadPasswordCount(userInfo);
insertLoginHistory(userInfo, ConstsLoginType.LOCAL, "", "xe00000004", WebConstants.LOGIN_RESULT.PASSWORD_ERROE); insertLoginHistory(userInfo, ConstsLoginType.LOCAL, "", "xe00000004", WebConstants.LOGIN_RESULT.PASSWORD_ERROE);
CnfPasswordPolicy passwordPolicy = passwordPolicyValidatorService.getPasswordPolicy(); CnfPasswordPolicy passwordPolicy = passwordPolicyValidatorService.getPasswordPolicy();
if(userInfo.getBadPasswordCount()>=(passwordPolicy.getAttempts()/2)) { if(userInfo.getBadPasswordCount()>=(passwordPolicy.getAttempts()/2)) {

View File

@ -25,17 +25,14 @@ import org.dromara.maxkey.authn.realm.AbstractAuthenticationRealm;
import org.dromara.maxkey.authn.session.SessionManager; import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.configuration.ApplicationConfig; import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.password.sms.SmsOtpAuthnService; import org.dromara.maxkey.password.sms.SmsOtpAuthnService;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService; import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.persistence.service.impl.PasswordPolicyValidatorServiceImpl; import org.dromara.maxkey.persistence.service.impl.PasswordPolicyValidatorServiceImpl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
@AutoConfiguration @AutoConfiguration
@ -108,9 +105,4 @@ public class AuthnProviderAutoConfiguration {
return new PasswordPolicyValidatorServiceImpl(cnfPasswordPolicyService,messageSource); return new PasswordPolicyValidatorServiceImpl(cnfPasswordPolicyService,messageSource);
} }
@Bean
LoginRepository loginRepository(UserInfoService userInfoService,CnfPasswordPolicyService cnfPasswordPolicyService,JdbcTemplate jdbcTemplate) {
return new LoginRepository(userInfoService,cnfPasswordPolicyService,jdbcTemplate);
}
} }

View File

@ -84,7 +84,7 @@ public class AppAuthenticationProvider extends AbstractAuthenticationProvider {
UserInfo userInfo = loadUserInfo(loginCredential.getUsername(), loginCredential.getPassword()); UserInfo userInfo = loadUserInfo(loginCredential.getUsername(), loginCredential.getPassword());
//Validate PasswordPolicy //Validate PasswordPolicy
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo); authenticationRealm.getLoginService().passwordPolicyValid(userInfo);
statusValid(loginCredential, userInfo); statusValid(loginCredential, userInfo);
@ -92,7 +92,7 @@ public class AppAuthenticationProvider extends AbstractAuthenticationProvider {
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword()); authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo); authenticationRealm.getLoginService().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential, userInfo); authenticationToken = createOnlineTicket(loginCredential, userInfo);
// user authenticated // user authenticated

View File

@ -89,13 +89,13 @@ public class MfaAuthenticationProvider extends AbstractAuthenticationProvider {
mfacaptchaValid(loginCredential.getOtpCaptcha(),userInfo); mfacaptchaValid(loginCredential.getOtpCaptcha(),userInfo);
//Validate PasswordPolicy //Validate PasswordPolicy
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo); authenticationRealm.getLoginService().passwordPolicyValid(userInfo);
//Match password //Match password
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword()); authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo); authenticationRealm.getLoginService().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo); authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated // user authenticated

View File

@ -97,7 +97,7 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
mobileCaptchaValid(loginCredential.getPassword(),userInfo); mobileCaptchaValid(loginCredential.getPassword(),userInfo);
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo); authenticationRealm.getLoginService().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo); authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated // user authenticated

View File

@ -87,7 +87,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
isUserExist(loginCredential , userInfo); isUserExist(loginCredential , userInfo);
//Validate PasswordPolicy //Validate PasswordPolicy
authenticationRealm.getLoginRepository().passwordPolicyValid(userInfo); authenticationRealm.getLoginService().passwordPolicyValid(userInfo);
statusValid(loginCredential , userInfo); statusValid(loginCredential , userInfo);
@ -95,7 +95,7 @@ public class NormalAuthenticationProvider extends AbstractAuthenticationProvider
authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword()); authenticationRealm.passwordMatches(userInfo, loginCredential.getPassword());
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(userInfo); authenticationRealm.getLoginService().applyPasswordPolicy(userInfo);
authenticationToken = createOnlineTicket(loginCredential,userInfo); authenticationToken = createOnlineTicket(loginCredential,userInfo);
// user authenticated // user authenticated

View File

@ -61,9 +61,9 @@ public class TrustedAuthenticationProvider extends AbstractAuthenticationProvide
statusValid(loginCredential , loadeduserInfo); statusValid(loginCredential , loadeduserInfo);
if (loadeduserInfo != null) { if (loadeduserInfo != null) {
//Validate PasswordPolicy //Validate PasswordPolicy
authenticationRealm.getLoginRepository().passwordPolicyValid(loadeduserInfo); authenticationRealm.getLoginService().passwordPolicyValid(loadeduserInfo);
//apply PasswordSetType and resetBadPasswordCount //apply PasswordSetType and resetBadPasswordCount
authenticationRealm.getLoginRepository().applyPasswordPolicy(loadeduserInfo); authenticationRealm.getLoginService().applyPasswordPolicy(loadeduserInfo);
Authentication authentication = createOnlineTicket(loginCredential,loadeduserInfo); Authentication authentication = createOnlineTicket(loginCredential,loadeduserInfo);
authenticationRealm.insertLoginHistory( loadeduserInfo, authenticationRealm.insertLoginHistory( loadeduserInfo,

View File

@ -28,8 +28,8 @@ import org.dromara.maxkey.entity.idm.Groups;
import org.dromara.maxkey.entity.idm.UserInfo; import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser; import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.ip2location.Region; import org.dromara.maxkey.ip2location.Region;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.HistoryLoginService; import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants; import org.dromara.maxkey.web.WebConstants;
@ -52,7 +52,7 @@ public abstract class AbstractAuthenticationRealm {
protected PasswordPolicyValidatorService passwordPolicyValidatorService; protected PasswordPolicyValidatorService passwordPolicyValidatorService;
protected LoginRepository loginRepository; protected LoginService loginService;
protected HistoryLoginService historyLoginService; protected HistoryLoginService historyLoginService;
@ -78,18 +78,18 @@ public abstract class AbstractAuthenticationRealm {
return passwordPolicyValidatorService; return passwordPolicyValidatorService;
} }
public LoginRepository getLoginRepository() { public LoginService getLoginService() {
return loginRepository; return loginService;
} }
public UserInfo loadUserInfo(String username, String password) { public UserInfo loadUserInfo(String username, String password) {
return loginRepository.find(username, password); return loginService.find(username, password);
} }
public abstract boolean passwordMatches(UserInfo userInfo, String password); public abstract boolean passwordMatches(UserInfo userInfo, String password);
public List<Groups> queryGroups(UserInfo userInfo) { public List<Groups> queryGroups(UserInfo userInfo) {
return loginRepository.queryGroups(userInfo); return loginService.queryGroups(userInfo);
} }
/** /**
@ -99,7 +99,7 @@ public abstract class AbstractAuthenticationRealm {
* @return ArrayList<GrantedAuthority> * @return ArrayList<GrantedAuthority>
*/ */
public List<GrantedAuthority> grantAuthority(UserInfo userInfo) { public List<GrantedAuthority> grantAuthority(UserInfo userInfo) {
return loginRepository.grantAuthority(userInfo); return loginService.grantAuthority(userInfo);
} }
/** /**
@ -109,7 +109,7 @@ public abstract class AbstractAuthenticationRealm {
* @return ArrayList<GrantedAuthority Apps> * @return ArrayList<GrantedAuthority Apps>
*/ */
public List<GrantedAuthority> queryAuthorizedApps(List<GrantedAuthority> grantedAuthoritys) { public List<GrantedAuthority> queryAuthorizedApps(List<GrantedAuthority> grantedAuthoritys) {
return loginRepository.queryAuthorizedApps(grantedAuthoritys); return loginService.queryAuthorizedApps(grantedAuthoritys);
} }
/** /**
@ -160,7 +160,7 @@ public abstract class AbstractAuthenticationRealm {
} }
historyLoginService.login(historyLogin); historyLoginService.login(historyLogin);
loginRepository.updateLastLogin(userInfo); loginService.updateLastLogin(userInfo);
return true; return true;
} }

View File

@ -26,8 +26,8 @@ import org.dromara.maxkey.entity.ChangePassword;
import org.dromara.maxkey.entity.cnf.CnfPasswordPolicy; import org.dromara.maxkey.entity.cnf.CnfPasswordPolicy;
import org.dromara.maxkey.entity.idm.UserInfo; import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser; import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.HistoryLoginService; import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants; import org.dromara.maxkey.web.WebConstants;
@ -59,7 +59,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm( public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder, PasswordEncoder passwordEncoder,
PasswordPolicyValidatorService passwordPolicyValidatorService, PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository, LoginService loginService,
HistoryLoginService historyLoginService, HistoryLoginService historyLoginService,
UserInfoService userInfoService, UserInfoService userInfoService,
IpLocationParser ipLocationParser, IpLocationParser ipLocationParser,
@ -67,7 +67,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
this.passwordEncoder =passwordEncoder; this.passwordEncoder =passwordEncoder;
this.passwordPolicyValidatorService=passwordPolicyValidatorService; this.passwordPolicyValidatorService=passwordPolicyValidatorService;
this.loginRepository = loginRepository; this.loginService = loginService;
this.historyLoginService = historyLoginService; this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService; this.userInfoService = userInfoService;
this.ipLocationParser = ipLocationParser; this.ipLocationParser = ipLocationParser;
@ -77,7 +77,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
public JdbcAuthenticationRealm( public JdbcAuthenticationRealm(
PasswordEncoder passwordEncoder, PasswordEncoder passwordEncoder,
PasswordPolicyValidatorService passwordPolicyValidatorService, PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository, LoginService loginService,
HistoryLoginService historyLoginService, HistoryLoginService historyLoginService,
UserInfoService userInfoService, UserInfoService userInfoService,
IpLocationParser ipLocationParser, IpLocationParser ipLocationParser,
@ -85,7 +85,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
LdapAuthenticationRealmService ldapAuthenticationRealmService) { LdapAuthenticationRealmService ldapAuthenticationRealmService) {
this.passwordEncoder = passwordEncoder; this.passwordEncoder = passwordEncoder;
this.passwordPolicyValidatorService = passwordPolicyValidatorService; this.passwordPolicyValidatorService = passwordPolicyValidatorService;
this.loginRepository = loginRepository; this.loginService = loginService;
this.historyLoginService = historyLoginService; this.historyLoginService = historyLoginService;
this.userInfoService = userInfoService; this.userInfoService = userInfoService;
this.ipLocationParser = ipLocationParser; this.ipLocationParser = ipLocationParser;
@ -126,7 +126,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
} }
_logger.debug("passwordvalid : {}" , passwordMatches); _logger.debug("passwordvalid : {}" , passwordMatches);
if (!passwordMatches) { if (!passwordMatches) {
loginRepository.plusBadPasswordCount(userInfo); loginService.plusBadPasswordCount(userInfo);
insertLoginHistory(userInfo, ConstsLoginType.LOCAL, "", "xe00000004", WebConstants.LOGIN_RESULT.PASSWORD_ERROE); insertLoginHistory(userInfo, ConstsLoginType.LOCAL, "", "xe00000004", WebConstants.LOGIN_RESULT.PASSWORD_ERROE);
CnfPasswordPolicy passwordPolicy = passwordPolicyValidatorService.getPasswordPolicy(); CnfPasswordPolicy passwordPolicy = passwordPolicyValidatorService.getPasswordPolicy();
if(userInfo.getBadPasswordCount()>=(passwordPolicy.getAttempts()/2)) { if(userInfo.getBadPasswordCount()>=(passwordPolicy.getAttempts()/2)) {

View File

@ -27,8 +27,8 @@ import org.dromara.maxkey.authn.support.rememberme.AbstractRemeberMeManager;
import org.dromara.maxkey.authn.support.rememberme.JdbcRemeberMeManager; import org.dromara.maxkey.authn.support.rememberme.JdbcRemeberMeManager;
import org.dromara.maxkey.configuration.ApplicationConfig; import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.password.sms.SmsOtpAuthnService; import org.dromara.maxkey.password.sms.SmsOtpAuthnService;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService; import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.persistence.service.impl.PasswordPolicyValidatorServiceImpl; import org.dromara.maxkey.persistence.service.impl.PasswordPolicyValidatorServiceImpl;
@ -142,11 +142,6 @@ public class AuthnProviderAutoConfiguration {
return new PasswordPolicyValidatorServiceImpl(cnfPasswordPolicyService,messageSource); return new PasswordPolicyValidatorServiceImpl(cnfPasswordPolicyService,messageSource);
} }
@Bean
LoginRepository loginRepository(UserInfoService userInfoService,CnfPasswordPolicyService cnfPasswordPolicyService,JdbcTemplate jdbcTemplate) {
return new LoginRepository(userInfoService,cnfPasswordPolicyService,jdbcTemplate);
}
/** /**
* remeberMeService . * remeberMeService .
* @return * @return

View File

@ -0,0 +1,89 @@
/*
* Copyright [2024] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.maxkey.persistence.service;
import java.util.List;
import org.dromara.maxkey.entity.idm.Groups;
import org.dromara.maxkey.entity.idm.UserInfo;
import org.springframework.security.core.GrantedAuthority;
public interface LoginService {
public UserInfo find(String username, String password);
public List<UserInfo> findByUsername(String username, String password);
public List<UserInfo> findByUsernameOrMobile(String username, String password);
public List<UserInfo> findByUsernameOrMobileOrEmail(String username, String password);
/**
* dynamic passwordPolicy Valid for user login.
* @param userInfo
* @return boolean
*/
public boolean passwordPolicyValid(UserInfo userInfo) ;
public void applyPasswordPolicy(UserInfo userInfo) ;
/**
* lockUser
*
* @param userInfo
*/
public void lockUser(UserInfo userInfo) ;
/**
* unlockUser
*
* @param userInfo
*/
public void unlockUser(UserInfo userInfo);
/**
* reset BadPasswordCount And Lockout
*
* @param userInfo
*/
public void resetAttempts(UserInfo userInfo);
public void plusBadPasswordCount(UserInfo userInfo) ;
public void resetBadPasswordCount(UserInfo userInfo);
public List<GrantedAuthority> queryAuthorizedApps(List<GrantedAuthority> grantedAuthoritys);
public List<Groups> queryGroups(UserInfo userInfo) ;
/**
* grant Authority by userinfo
*
* @param userInfo
* @return ArrayList<GrantedAuthority>
*/
public List<GrantedAuthority> grantAuthority(UserInfo userInfo) ;
public void updateLastLogin(UserInfo userInfo) ;
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top] * Copyright [2024] [MaxKey of copyright http://www.maxkey.top]
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,13 +15,12 @@
*/ */
package org.dromara.maxkey.persistence.repository; package org.dromara.maxkey.persistence.service.impl;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -33,6 +32,7 @@ import org.dromara.maxkey.entity.cnf.CnfPasswordPolicy;
import org.dromara.maxkey.entity.idm.Groups; import org.dromara.maxkey.entity.idm.Groups;
import org.dromara.maxkey.entity.idm.UserInfo; import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService; import org.dromara.maxkey.persistence.service.CnfPasswordPolicyService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.web.WebConstants; import org.dromara.maxkey.web.WebConstants;
import org.dromara.maxkey.web.WebContext; import org.dromara.maxkey.web.WebContext;
@ -40,14 +40,17 @@ import org.joda.time.DateTime;
import org.joda.time.Duration; import org.joda.time.Duration;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Repository;
public class LoginRepository { @Repository
private static final Logger _logger = LoggerFactory.getLogger(LoginRepository.class); public class LoginServiceImpl implements LoginService{
private static final Logger _logger = LoggerFactory.getLogger(LoginServiceImpl.class);
private static final String LOGIN_USERINFO_UPDATE_STATEMENT = "update mxk_userinfo set lastlogintime = ? , lastloginip = ? , logincount = ?, online = " private static final String LOGIN_USERINFO_UPDATE_STATEMENT = "update mxk_userinfo set lastlogintime = ? , lastloginip = ? , logincount = ?, online = "
+ UserInfo.ONLINE.ONLINE + " where id = ?"; + UserInfo.ONLINE.ONLINE + " where id = ?";
@ -62,10 +65,13 @@ public class LoginRepository {
private static final String DEFAULT_MYAPPS_SELECT_STATEMENT = "select distinct app.id,app.appname from mxk_apps app,mxk_access gp,mxk_groups g where app.id=gp.appid and app.status = 1 and gp.groupid=g.id and g.id in(%s)"; private static final String DEFAULT_MYAPPS_SELECT_STATEMENT = "select distinct app.id,app.appname from mxk_apps app,mxk_access gp,mxk_groups g where app.id=gp.appid and app.status = 1 and gp.groupid=g.id and g.id in(%s)";
protected JdbcTemplate jdbcTemplate; @Autowired
JdbcTemplate jdbcTemplate;
@Autowired
UserInfoService userInfoService; UserInfoService userInfoService;
@Autowired
CnfPasswordPolicyService cnfPasswordPolicyService; CnfPasswordPolicyService cnfPasswordPolicyService;
/** /**
@ -73,16 +79,10 @@ public class LoginRepository {
*/ */
public static int LOGIN_ATTRIBUTE_TYPE = 2; public static int LOGIN_ATTRIBUTE_TYPE = 2;
public LoginRepository(){ public LoginServiceImpl(){
} }
public LoginRepository(UserInfoService userInfoService,CnfPasswordPolicyService cnfPasswordPolicyService,JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
this.userInfoService = userInfoService;
this.cnfPasswordPolicyService = cnfPasswordPolicyService;
}
public UserInfo find(String username, String password) { public UserInfo find(String username, String password) {
List<UserInfo> listUserInfo = null ; List<UserInfo> listUserInfo = null ;
if( LOGIN_ATTRIBUTE_TYPE == 1) { if( LOGIN_ATTRIBUTE_TYPE == 1) {

View File

@ -18,7 +18,7 @@ import org.dromara.maxkey.authn.SignPrincipal;
import org.dromara.maxkey.authn.provider.AbstractAuthenticationProvider; import org.dromara.maxkey.authn.provider.AbstractAuthenticationProvider;
import org.dromara.maxkey.authn.session.Session; import org.dromara.maxkey.authn.session.Session;
import org.dromara.maxkey.entity.idm.UserInfo; import org.dromara.maxkey.entity.idm.UserInfo;
import org.dromara.maxkey.persistence.repository.LoginRepository; import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.web.WebConstants; import org.dromara.maxkey.web.WebConstants;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -35,7 +35,7 @@ public class OAuth2UserDetailsService implements UserDetailsService {
private static final Logger _logger = private static final Logger _logger =
LoggerFactory.getLogger(OAuth2UserDetailsService.class); LoggerFactory.getLogger(OAuth2UserDetailsService.class);
LoginRepository loginRepository; LoginService loginRepository;
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo; UserInfo userInfo;
@ -68,7 +68,7 @@ public class OAuth2UserDetailsService implements UserDetailsService {
return principal; return principal;
} }
public void setLoginRepository(LoginRepository loginRepository) { public void setLoginRepository(LoginService loginRepository) {
this.loginRepository = loginRepository; this.loginRepository = loginRepository;
} }

View File

@ -48,8 +48,8 @@ import org.dromara.maxkey.crypto.jose.keystore.JWKSetKeyStore;
import org.dromara.maxkey.crypto.jwt.encryption.service.impl.DefaultJwtEncryptionAndDecryptionService; import org.dromara.maxkey.crypto.jwt.encryption.service.impl.DefaultJwtEncryptionAndDecryptionService;
import org.dromara.maxkey.crypto.jwt.signer.service.impl.DefaultJwtSigningAndValidationService; import org.dromara.maxkey.crypto.jwt.signer.service.impl.DefaultJwtSigningAndValidationService;
import org.dromara.maxkey.persistence.redis.RedisConnectionFactory; import org.dromara.maxkey.persistence.redis.RedisConnectionFactory;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.AppsService; import org.dromara.maxkey.persistence.service.AppsService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@ -329,7 +329,7 @@ public class Oauth20AutoConfiguration implements InitializingBean {
ProviderManager oauth20UserAuthenticationManager( ProviderManager oauth20UserAuthenticationManager(
@Qualifier("passwordEncoder") @Qualifier("passwordEncoder")
PasswordEncoder passwordEncoder, PasswordEncoder passwordEncoder,
LoginRepository loginRepository LoginService loginRepository
) { ) {
OAuth2UserDetailsService userDetailsService =new OAuth2UserDetailsService(); OAuth2UserDetailsService userDetailsService =new OAuth2UserDetailsService();

View File

@ -42,9 +42,9 @@ import org.dromara.maxkey.password.onetimepwd.impl.MailOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn; import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.token.RedisOtpTokenStore; import org.dromara.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.dromara.maxkey.persistence.redis.RedisConnectionFactory; import org.dromara.maxkey.persistence.redis.RedisConnectionFactory;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.CnfLdapContextService; import org.dromara.maxkey.persistence.service.CnfLdapContextService;
import org.dromara.maxkey.persistence.service.HistoryLoginService; import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.dromara.maxkey.schedule.ScheduleAdapterBuilder; import org.dromara.maxkey.schedule.ScheduleAdapterBuilder;
@ -89,7 +89,7 @@ public class MaxKeyConfig {
JdbcAuthenticationRealm authenticationRealm( JdbcAuthenticationRealm authenticationRealm(
@Qualifier("passwordEncoder") PasswordEncoder passwordEncoder, @Qualifier("passwordEncoder") PasswordEncoder passwordEncoder,
PasswordPolicyValidatorService passwordPolicyValidatorService, PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginService, LoginService loginService,
HistoryLoginService historyLoginService, HistoryLoginService historyLoginService,
UserInfoService userInfoService, UserInfoService userInfoService,
IpLocationParser ipLocationParser, IpLocationParser ipLocationParser,

View File

@ -21,8 +21,8 @@ import org.dromara.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
import org.dromara.maxkey.ip2location.IpLocationParser; import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn; import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.HistoryLoginService; import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -43,7 +43,7 @@ public class MaxKeyMgtConfig {
@Qualifier("passwordEncoder") @Qualifier("passwordEncoder")
PasswordEncoder passwordEncoder, PasswordEncoder passwordEncoder,
PasswordPolicyValidatorService passwordPolicyValidatorService, PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository, LoginService loginService,
HistoryLoginService historyLoginService, HistoryLoginService historyLoginService,
UserInfoService userInfoService, UserInfoService userInfoService,
IpLocationParser ipLocationParser, IpLocationParser ipLocationParser,
@ -52,7 +52,7 @@ public class MaxKeyMgtConfig {
JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm( JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(
passwordEncoder, passwordEncoder,
passwordPolicyValidatorService, passwordPolicyValidatorService,
loginRepository, loginService,
historyLoginService, historyLoginService,
userInfoService, userInfoService,
ipLocationParser, ipLocationParser,

View File

@ -21,8 +21,8 @@ import org.dromara.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
import org.dromara.maxkey.ip2location.IpLocationParser; import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn; import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.service.HistoryLoginService; import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService; import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
import org.dromara.maxkey.persistence.service.UserInfoService; import org.dromara.maxkey.persistence.service.UserInfoService;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -43,7 +43,7 @@ public class MaxKeyOpenApiConfig{
@Qualifier("passwordEncoder") @Qualifier("passwordEncoder")
PasswordEncoder passwordEncoder, PasswordEncoder passwordEncoder,
PasswordPolicyValidatorService passwordPolicyValidatorService, PasswordPolicyValidatorService passwordPolicyValidatorService,
LoginRepository loginRepository, LoginService loginService,
HistoryLoginService historyLoginService, HistoryLoginService historyLoginService,
UserInfoService userInfoService, UserInfoService userInfoService,
IpLocationParser ipLocationParser, IpLocationParser ipLocationParser,
@ -52,7 +52,7 @@ public class MaxKeyOpenApiConfig{
JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm( JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(
passwordEncoder, passwordEncoder,
passwordPolicyValidatorService, passwordPolicyValidatorService,
loginRepository, loginService,
historyLoginService, historyLoginService,
userInfoService, userInfoService,
ipLocationParser, ipLocationParser,