mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-16 05:20:42 +08:00
PasswordPolicyMessageResolver
PasswordPolicyMessageResolver
This commit is contained in:
@@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
@@ -130,8 +131,8 @@ public class ApplicationAutoConfiguration implements InitializingBean {
|
||||
}
|
||||
|
||||
@Bean(name = "passwordPolicyValidator")
|
||||
public PasswordPolicyValidator passwordPolicyValidator(JdbcTemplate jdbcTemplate) {
|
||||
return new PasswordPolicyValidator(jdbcTemplate);
|
||||
public PasswordPolicyValidator passwordPolicyValidator(JdbcTemplate jdbcTemplate,MessageSource messageSource) {
|
||||
return new PasswordPolicyValidator(jdbcTemplate,messageSource);
|
||||
}
|
||||
|
||||
@Bean(name = "loginService")
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.maxkey.persistence.db;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.passay.MessageResolver;
|
||||
import org.passay.PropertiesMessageResolver;
|
||||
import org.passay.RuleResultDetail;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
import org.springframework.context.support.MessageSourceAccessor;
|
||||
|
||||
|
||||
public class PasswordPolicyMessageResolver implements MessageResolver{
|
||||
|
||||
/** A accessor for Spring's {@link MessageSource} */
|
||||
private final MessageSourceAccessor messageSourceAccessor;
|
||||
|
||||
/** The {@link MessageResolver} for fallback */
|
||||
private final MessageResolver fallbackMessageResolver = new PropertiesMessageResolver();
|
||||
|
||||
/**
|
||||
* Create a new instance with the locale associated with the current thread.
|
||||
* @param messageSource a message source managed by spring
|
||||
*/
|
||||
public PasswordPolicyMessageResolver(final MessageSource messageSource)
|
||||
{
|
||||
this.messageSourceAccessor = new MessageSourceAccessor(messageSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance with the specified locale.
|
||||
* @param messageSource a message source managed by spring
|
||||
* @param locale the locale to use for message access
|
||||
*/
|
||||
public PasswordPolicyMessageResolver(final MessageSource messageSource, final Locale locale)
|
||||
{
|
||||
this.messageSourceAccessor = new MessageSourceAccessor(messageSource, locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the message for the supplied rule result detail using Spring's {@link MessageSource}.
|
||||
* (If the message can't retrieve from a {@link MessageSource}, return default message provided by passay)
|
||||
* @param detail rule result detail
|
||||
* @return message for the detail error code
|
||||
*/
|
||||
@Override
|
||||
public String resolve(final RuleResultDetail detail)
|
||||
{
|
||||
try {
|
||||
return this.messageSourceAccessor.getMessage(detail.getErrorCode().toLowerCase(), detail.getValues());
|
||||
} catch (NoSuchMessageException e) {
|
||||
return this.fallbackMessageResolver.resolve(detail);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import org.passay.dictionary.Dictionary;
|
||||
import org.passay.dictionary.DictionaryBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
@@ -58,6 +59,8 @@ public class PasswordPolicyValidator {
|
||||
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
|
||||
MessageSource messageSource;
|
||||
|
||||
private static final String PASSWORD_POLICY_KEY = "PASSWORD_POLICY_KEY";
|
||||
private static final String LOCK_USER_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET ISLOCKED = ? , UNLOCKTIME = ? WHERE ID = ?";
|
||||
|
||||
@@ -72,7 +75,8 @@ public class PasswordPolicyValidator {
|
||||
public PasswordPolicyValidator() {
|
||||
}
|
||||
|
||||
public PasswordPolicyValidator(JdbcTemplate jdbcTemplate) {
|
||||
public PasswordPolicyValidator(JdbcTemplate jdbcTemplate,MessageSource messageSource) {
|
||||
this.messageSource=messageSource;
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@@ -138,8 +142,9 @@ public class PasswordPolicyValidator {
|
||||
}
|
||||
|
||||
getPasswordPolicy();
|
||||
|
||||
PasswordValidator validator = new PasswordValidator(passwordPolicyRuleList);
|
||||
|
||||
PasswordValidator validator = new PasswordValidator(
|
||||
new PasswordPolicyMessageResolver(messageSource),passwordPolicyRuleList);
|
||||
|
||||
RuleResult result = validator.validate(new PasswordData(username,password));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user