1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| import com.maxzhao.security.crypto.BootPasswordProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; import org.springframework.security.crypto.argon2.Argon2PasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.DelegatingPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils;
import javax.annotation.Resource; import java.util.HashMap; import java.util.Map;
@Slf4j @Component @AutoConfigureAfter(BootPasswordProperties.class) public class PasswordEncoderFactory { @Resource private BootPasswordProperties bootPasswordProperties;
private final Map<String, PasswordEncoder> idToPasswordEncoder = new HashMap<>(16);
@Bean public PasswordEncoder passwordEncoder() { return getDelegatingPasswordEncoder(); }
public String encode(String idForEncode, CharSequence rawPassword) { if (idForEncode == null) { throw new IllegalArgumentException("idForEncode cannot be null"); } PasswordEncoder passwordEncoder = idToPasswordEncoder.get(idForEncode); if (passwordEncoder == null) { throw new IllegalArgumentException(idForEncode + " PasswordEncoder cannot be null"); } return passwordEncoder.encode(rawPassword); }
public PasswordEncoder getDelegatingPasswordEncoder() { String defaultEncoderId = bootPasswordProperties.getDefaultEncoderId(); if (!StringUtils.hasText(defaultEncoderId)) { log.warn("默认密码加密方式不存在,已使用默认配置 {}", BootPasswordProperties.DEFAULT_ENCODER_ID); defaultEncoderId = BootPasswordProperties.DEFAULT_ENCODER_ID; } if (idToPasswordEncoder.isEmpty()) { Map<String, String> encoder = bootPasswordProperties.getEncoder(); if (encoder == null) { encoder = new HashMap<>(0); } encoder.forEach((key, value) -> { try { Class<?> com = Class.forName(value); Object o = com.newInstance(); if (o instanceof PasswordEncoder) { idToPasswordEncoder.put(key, (PasswordEncoder) o); } else { log.error("加载自定义 {} 失败:{} 需要实现 PasswordEncoder 接口 ", key, value); } } catch (ClassNotFoundException e) { log.error("加载自定义 {} 失败:PasswordEncoder[{}] ", key, value); throw new RuntimeException(e); } catch (InstantiationException | IllegalAccessException e) { log.error("实例化 {} 失败:PasswordEncoder[{}] ", key, value); throw new RuntimeException(e); } }); idToPasswordEncoder.put("bcrypt", new BCryptPasswordEncoder()); idToPasswordEncoder.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder()); idToPasswordEncoder.put("MD4", new org.springframework.security.crypto.password.Md4PasswordEncoder()); idToPasswordEncoder.put("MD5", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("MD5")); idToPasswordEncoder.put("noop", org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance()); idToPasswordEncoder.put("pbkdf2", new Pbkdf2PasswordEncoder()); idToPasswordEncoder.put("scrypt", new SCryptPasswordEncoder()); idToPasswordEncoder.put("SHA-1", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1")); idToPasswordEncoder.put("SHA-256", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256")); idToPasswordEncoder.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder()); idToPasswordEncoder.put("argon2", new Argon2PasswordEncoder()); } return new DelegatingPasswordEncoder(defaultEncoderId, idToPasswordEncoder); } }
|