Spring Security5多加密方式处理

从spring security 5.0起支持多种加密方式处理,并通过前缀来区分加密方式
测试代码:

 PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        String encode = passwordEncoder.encode("123456");
        System.out.println("bcrypt密文:" + encode);
        System.out.println("bcrypt对比:" + passwordEncoder.matches("123456", encode));
        //MD5加密前的密码:123456
        String md5encode = "{MD5}e10adc3949ba59abbe56e057f20f883e";
        System.out.println("MD5对比:" + passwordEncoder.matches("123456", md5encode));

测试结果

bcrypt密文:{bcrypt}$2a$10$r5vO.SHOl2nsv71k8lZgLe5na1fBdtqDvDYhYPweu/CnzFRSYhZSq
bcrypt对比:true
MD5对比:true

PasswordEncoderFactories代码

	public static PasswordEncoder createDelegatingPasswordEncoder() {
		String encodingId = "bcrypt";
		Map<String, PasswordEncoder> encoders = new HashMap<>();
		encoders.put(encodingId, new BCryptPasswordEncoder());
		encoders.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder());
		encoders.put("MD4", new org.springframework.security.crypto.password.Md4PasswordEncoder());
		encoders.put("MD5", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("MD5"));
		encoders.put("noop", org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance());
		encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
		encoders.put("scrypt", new SCryptPasswordEncoder());
		encoders.put("SHA-1", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1"));
		encoders.put("SHA-256", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256"));
		encoders.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder());

		return new DelegatingPasswordEncoder(encodingId, encoders);
	}

你可能感兴趣的:(spring)