diff --git a/maxkey-starter/maxkey-starter-otp/src/main/java/org/dromara/maxkey/autoconfigure/OneTimePasswordAutoConfiguration.java b/maxkey-starter/maxkey-starter-otp/src/main/java/org/dromara/maxkey/autoconfigure/OneTimePasswordAutoConfiguration.java index d6a3978be..9aaf0eca2 100644 --- a/maxkey-starter/maxkey-starter-otp/src/main/java/org/dromara/maxkey/autoconfigure/OneTimePasswordAutoConfiguration.java +++ b/maxkey-starter/maxkey-starter-otp/src/main/java/org/dromara/maxkey/autoconfigure/OneTimePasswordAutoConfiguration.java @@ -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 = @@ -52,5 +80,69 @@ public class OneTimePasswordAutoConfiguration { persistence == ConstsPersistence.REDIS ? "Redis" : "InMemory"); 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; + } } diff --git a/maxkey-webs/maxkey-web-maxkey/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyConfig.java b/maxkey-webs/maxkey-web-maxkey/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyConfig.java index 9e0547f3f..caac0d88d 100644 --- a/maxkey-webs/maxkey-web-maxkey/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyConfig.java +++ b/maxkey-webs/maxkey-web-maxkey/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyConfig.java @@ -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}") diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtConfig.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtConfig.java index 278d55f67..1553464d8 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtConfig.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyMgtConfig.java @@ -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; - }*/ - } diff --git a/maxkey-webs/maxkey-web-mgt/src/main/resources/application-maxkey-mgt.properties b/maxkey-webs/maxkey-web-mgt/src/main/resources/application-maxkey-mgt.properties index 383c38f4b..b6eeb2986 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/resources/application-maxkey-mgt.properties +++ b/maxkey-webs/maxkey-web-mgt/src/main/resources/application-maxkey-mgt.properties @@ -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 ############################################################################ diff --git a/maxkey-webs/maxkey-web-openapi/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyOpenApiConfig.java b/maxkey-webs/maxkey-web-openapi/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyOpenApiConfig.java index 76064d762..e9a469d77 100644 --- a/maxkey-webs/maxkey-web-openapi/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyOpenApiConfig.java +++ b/maxkey-webs/maxkey-web-openapi/src/main/java/org/dromara/maxkey/autoconfigure/MaxKeyOpenApiConfig.java @@ -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; - } } diff --git a/maxkey-webs/maxkey-web-openapi/src/main/resources/application-maxkey-openapi.properties b/maxkey-webs/maxkey-web-openapi/src/main/resources/application-maxkey-openapi.properties index 79411b7d0..a50ecaea5 100644 --- a/maxkey-webs/maxkey-web-openapi/src/main/resources/application-maxkey-openapi.properties +++ b/maxkey-webs/maxkey-web-openapi/src/main/resources/application-maxkey-openapi.properties @@ -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 ############################################################################