OneTimePasswordAutoConfiguration

This commit is contained in:
shimingxy 2025-11-24 17:12:43 +08:00
parent cfb09a2f1a
commit 66a8569f44
6 changed files with 97 additions and 129 deletions

View File

@ -17,8 +17,18 @@
package org.dromara.maxkey.autoconfigure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import org.dromara.maxkey.configuration.EmailConfig;
import org.dromara.maxkey.constants.ConstsPersistence;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.dromara.maxkey.password.onetimepwd.algorithm.OtpKeyUriFormat;
import org.dromara.maxkey.password.onetimepwd.impl.MailOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.dromara.maxkey.persistence.redis.RedisConnectionFactory;
import org.dromara.maxkey.persistence.service.CnfEmailSendersService;
@ -27,17 +37,35 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@AutoConfiguration
public class OneTimePasswordAutoConfiguration {
private static final Logger _logger =
LoggerFactory.getLogger(OneTimePasswordAutoConfiguration.class);
private static final Logger _logger = LoggerFactory.getLogger(OneTimePasswordAutoConfiguration.class);
@Bean
OtpKeyUriFormat otpKeyUriFormat(
@Value("${maxkey.otp.policy.type:totp}")
String type,
@Value("${maxkey.otp.policy.domain:MaxKey.top}")
String domain,
@Value("${maxkey.otp.policy.issuer:MaxKey}")
String issuer,
@Value("${maxkey.otp.policy.digits:6}")
int digits,
@Value("${maxkey.otp.policy.period:30}")
int period) {
OtpKeyUriFormat otpKeyUriFormat=new OtpKeyUriFormat(type,issuer,domain,digits,period);
_logger.debug("OTP KeyUri Format {}" , otpKeyUriFormat);
return otpKeyUriFormat;
}
@Bean(name = "mailOtpAuthnService")
MailOtpAuthnService mailOtpAuthnService(
@Value("${maxkey.server.persistence}") int persistence,
@Value("${maxkey.server.persistence:0}") int persistence,
CnfEmailSendersService emailSendersService,
RedisConnectionFactory redisConnFactory) {
MailOtpAuthnService otpAuthnService =
@ -53,4 +81,68 @@ public class OneTimePasswordAutoConfiguration {
return otpAuthnService;
}
@Bean
TimeBasedOtpAuthn timeBasedOtpAuthn(
@Value("${maxkey.otp.policy.digits:6}")
int digits,
@Value("${maxkey.otp.policy.period:30}")
int period) {
TimeBasedOtpAuthn timeBasedOtpAuthn = new TimeBasedOtpAuthn(digits , period);
_logger.debug("TimeBasedOtpAuthn inited.");
return timeBasedOtpAuthn;
}
@Bean
AbstractOtpAuthn tfaOtpAuthn(
@Value("${maxkey.login.mfa.type:TimeBasedOtpAuthn}") String mfaType,
@Value("${maxkey.otp.policy.digits:6}")
int digits,
@Value("${maxkey.otp.policy.period:30}")
int period,
@Value("${maxkey.server.persistence:0}") int persistence,
RedisConnectionFactory redisConnFactory) {
AbstractOtpAuthn tfaOtpAuthn = new TimeBasedOtpAuthn(digits , period);
_logger.debug("TFAOtpAuthn inited.");
if (persistence == ConstsPersistence.REDIS) {
RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
tfaOtpAuthn.setOptTokenStore(redisOptTokenStore);
}
tfaOtpAuthn.initPropertys();
return tfaOtpAuthn;
}
@Bean
MailOtpAuthn mailOtpAuthn(
EmailConfig emailConfig,
@Value("${spring.mail.properties.mailotp.message.subject:One Time PassWord}")
String messageSubject,
@Value("${spring.mail.properties.mailotp.message.template:You Token is %s }")
String messageTemplate,
@Value("${spring.mail.properties.mailotp.message.validity:300}")
int messageValidity,
@Value("${spring.mail.properties.mailotp.message.type:text}")
String messageType
) {
if(messageType!= null && messageType.equalsIgnoreCase("html")) {
Resource resource = new ClassPathResource("messages/email/forgotpassword.html");
try {
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(resource.getInputStream()));
messageTemplate = bufferedReader.lines().collect(Collectors.joining("\n"));
bufferedReader.close();
} catch (IOException e) {
_logger.error("mailOtpAuthn IOException ",e);
}
}
_logger.trace("messageTemplate \n {}" ,messageTemplate);
MailOtpAuthn mailOtpAuthn = new MailOtpAuthn();
mailOtpAuthn.setSubject(messageSubject);
mailOtpAuthn.setMessageTemplate(messageTemplate);
mailOtpAuthn.setEmailConfig(emailConfig);
mailOtpAuthn.setInterval(messageValidity);
_logger.debug("MailOtpAuthn inited.");
return mailOtpAuthn;
}
}

View File

@ -17,13 +17,8 @@
package org.dromara.maxkey.autoconfigure;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.dromara.maxkey.authn.listener.SessionListenerAdapter;
import org.dromara.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
import org.dromara.maxkey.authn.realm.ldap.LdapAuthenticationRealmService;
@ -32,16 +27,8 @@ import org.dromara.maxkey.authn.session.SessionManager;
import org.dromara.maxkey.authn.support.kerberos.KerberosProxy;
import org.dromara.maxkey.authn.support.kerberos.RemoteKerberosService;
import org.dromara.maxkey.configuration.ApplicationConfig;
import org.dromara.maxkey.configuration.EmailConfig;
import org.dromara.maxkey.constants.ConstsPersistence;
import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.dromara.maxkey.password.onetimepwd.algorithm.OtpKeyUriFormat;
import org.dromara.maxkey.password.onetimepwd.impl.MailOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.dromara.maxkey.persistence.redis.RedisConnectionFactory;
import org.dromara.maxkey.persistence.service.CnfLdapContextService;
import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
@ -56,8 +43,6 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
@ -65,25 +50,6 @@ import org.springframework.security.crypto.password.PasswordEncoder;
public class MaxKeyConfig {
private static final Logger logger = LoggerFactory.getLogger(MaxKeyConfig.class);
@Bean
OtpKeyUriFormat otpKeyUriFormat(
@Value("${maxkey.otp.policy.type:totp}")
String type,
@Value("${maxkey.otp.policy.domain:MaxKey.top}")
String domain,
@Value("${maxkey.otp.policy.issuer:MaxKey}")
String issuer,
@Value("${maxkey.otp.policy.digits:6}")
int digits,
@Value("${maxkey.otp.policy.period:30}")
int period) {
OtpKeyUriFormat otpKeyUriFormat=new OtpKeyUriFormat(type,issuer,domain,digits,period);
logger.debug("OTP KeyUri Format {}" , otpKeyUriFormat);
return otpKeyUriFormat;
}
//可以在此实现其他的登陆认证方式请实现AbstractAuthenticationRealm
@Bean
JdbcAuthenticationRealm authenticationRealm(
@ -109,71 +75,6 @@ public class MaxKeyConfig {
);
}
@Bean
TimeBasedOtpAuthn timeBasedOtpAuthn(
@Value("${maxkey.otp.policy.digits:6}")
int digits,
@Value("${maxkey.otp.policy.period:30}")
int period) {
TimeBasedOtpAuthn tfaOtpAuthn = new TimeBasedOtpAuthn(digits , period);
logger.debug("TimeBasedOtpAuthn inited.");
return tfaOtpAuthn;
}
@Bean
AbstractOtpAuthn tfaOtpAuthn(
@Value("${maxkey.login.mfa.type}") String mfaType,
@Value("${maxkey.otp.policy.digits:6}")
int digits,
@Value("${maxkey.otp.policy.period:30}")
int period,
@Value("${maxkey.server.persistence}") int persistence,
RedisConnectionFactory redisConnFactory) {
AbstractOtpAuthn tfaOtpAuthn = new TimeBasedOtpAuthn(digits , period);
logger.debug("TimeBasedOtpAuthn inited.");
if (persistence == ConstsPersistence.REDIS) {
RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
tfaOtpAuthn.setOptTokenStore(redisOptTokenStore);
}
tfaOtpAuthn.initPropertys();
return tfaOtpAuthn;
}
@Bean
MailOtpAuthn mailOtpAuthn(
EmailConfig emailConfig,
@Value("${spring.mail.properties.mailotp.message.subject}")
String messageSubject,
@Value("${spring.mail.properties.mailotp.message.template}")
String messageTemplate,
@Value("${spring.mail.properties.mailotp.message.validity}")
int messageValidity,
@Value("${spring.mail.properties.mailotp.message.type}")
String messageType
) {
if(messageType!= null && messageType.equalsIgnoreCase("html")) {
Resource resource = new ClassPathResource("messages/email/forgotpassword.html");
try {
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(resource.getInputStream()));
messageTemplate = bufferedReader.lines().collect(Collectors.joining("\n"));
bufferedReader.close();
} catch (IOException e) {
logger.error("mailOtpAuthn IOException ",e);
}
}
logger.trace("messageTemplate \n {}" ,messageTemplate);
MailOtpAuthn mailOtpAuthn = new MailOtpAuthn();
mailOtpAuthn.setSubject(messageSubject);
mailOtpAuthn.setMessageTemplate(messageTemplate);
mailOtpAuthn.setEmailConfig(emailConfig);
mailOtpAuthn.setInterval(messageValidity);
logger.debug("MailOtpAuthn inited.");
return mailOtpAuthn;
}
@Bean
RemoteKerberosService kerberosService(
@Value("${maxkey.login.kerberos.default.userdomain}")

View File

@ -19,8 +19,6 @@ package org.dromara.maxkey.autoconfigure;
import org.dromara.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
@ -62,19 +60,4 @@ public class MaxKeyMgtConfig {
return authenticationRealm;
}
@Bean
AbstractOtpAuthn timeBasedOtpAuthn() {
AbstractOtpAuthn tfaOtpAuthn = new TimeBasedOtpAuthn();
logger.debug("TimeBasedOtpAuthn inited.");
return tfaOtpAuthn;
}
/*@Bean
public ISynchronizerService ldapSynchronizerService() {
LdapSynchronizerService ldapSynchronizerService = new LdapSynchronizerService();
ldapSynchronizerService.setId("LDAP_11122");
ldapSynchronizerService.syncOrg();
return ldapSynchronizerService;
}*/
}

View File

@ -133,7 +133,7 @@ spring.mail.properties.ssl =true
spring.mail.properties.sender =${MAIL_SENDER:maxkey@163.com}
spring.mail.properties.mailotp.message.subject =MaxKey One Time PassWord
spring.mail.properties.mailotp.message.template ={0} You Token is {1} , it validity in {2} minutes.
spring.mail.properties.mailotp.message.type =html
spring.mail.properties.mailotp.message.type =text
spring.mail.properties.mailotp.message.validity =300
############################################################################

View File

@ -19,8 +19,6 @@ package org.dromara.maxkey.autoconfigure;
import org.dromara.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.dromara.maxkey.persistence.service.HistoryLoginService;
import org.dromara.maxkey.persistence.service.LoginService;
import org.dromara.maxkey.persistence.service.PasswordPolicyValidatorService;
@ -62,10 +60,4 @@ public class MaxKeyOpenApiConfig{
return authenticationRealm;
}
@Bean
AbstractOtpAuthn timeBasedOtpAuthn() {
AbstractOtpAuthn tfaOtpAuthn = new TimeBasedOtpAuthn();
logger.debug("TimeBasedOtpAuthn inited.");
return tfaOtpAuthn;
}
}

View File

@ -131,7 +131,7 @@ spring.mail.properties.ssl =true
spring.mail.properties.sender =${MAIL_SENDER:maxkey@163.com}
spring.mail.properties.mailotp.message.subject =MaxKey One Time PassWord
spring.mail.properties.mailotp.message.template ={0} You Token is {1} , it validity in {2} minutes.
spring.mail.properties.mailotp.message.type =html
spring.mail.properties.mailotp.message.type =text
spring.mail.properties.mailotp.message.validity =300
############################################################################