spring security 5.x实现兼容多种密码的加密方式

网友投稿 428 2023-02-21


spring security 5.x实现兼容多种密码的加密方式

前言

本文主要给大家介绍了关于spring security 5.x实现兼容多种密码的加密方式,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

1、spring security PasswordEncoder

spring security 5不需要配置密码的加密方式,而是用户密码加前缀的方式表明加密方式,如:

{MD5}88e2d8cd1e92fd5544c8621508cd706b代表使用的是MD5加密方式;

{bcrypt}$2a$10$eZeGvVV2ZXr/vgiVFzqzS.JLV878ApBgRT9maPK1Wrg0ovsf4YuI6代表使用的是bcrypt加密方式。

spring security官方推荐使用更加安全的bcrypt加密方式。

这样可以在同一系统中支持多种加密方式,迁移用户比较省事。spring security 5支持的加密方式在PasswordEncoderFactories中定义:

public class PasswordEncoderFactories {

public static PasswordEncoder createDelegatingPasswordEncoder() {

String encodingId = "bcrypt";

Map encoders = new HashMap();

encoders.put(encodingId, new BCryptPasswordEncoder());

encoders.put("ldap", new LdapShaPasswordEncoder());

encoders.put("MD4", new Md4PasswordEncoder());

encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));

encoders.put("noop", NoOpPasswordEncoder.getInstance());

encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());

encoders.put("scrypt", new SCryptPasswordEncoder());

encoders.put("SHA-1", new MessageDigestPcdwZTasswordEncoder("SHA-1"));

encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));

encoders.put("sha256", new StandardPasswordEncoder());

return new DelegatingPasswordEncoder(encodingId, encoders);

}

private PasswordEncoderFactories() {

}

}

2 测试

2.1 pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.hfcsbc

securithttp://y

0.0.1-SNAPSHOT

jar

security

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.0.M7

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-security

org.springframework.boot

spring-boot-starter-test

test

org.springframework.security

spring-security-test

test

org.projectlombok

lombok

org.springframework.boot

spring-boot-maven-plugin

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.hfcsbc

securithttp://y

0.0.1-SNAPSHOT

jar

security

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.0.M7

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-security

org.springframework.boot

spring-boot-starter-test

test

org.springframework.security

spring-security-test

test

org.projectlombok

lombok

org.springframework.boot

spring-boot-maven-plugin

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

spring-snapshots

Spring Snapshots

https://repo.spring.io/snapshot

true

spring-milestones

Spring Milestones

https://repo.spring.io/milestone

false

2.2 测试

spring security 5.x默认使用bcrypt加密

@Slf4j

public class DomainUserDetailsService {

public static void main(String[] args){

PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

String encode = passwordEncoder.encode("password");

log.info("加密后的密码:" + encode);

log.info("bcrypt密码对比:" + passwordEncoder.matches("password", encode));

String md5Password = "{MD5}88e2d8cd1e92fd5544c8621508cd706b";//MD5加密前的密码为:password

log.info("MD5密码对比:" + passwordEncoder.matches("password", encode));

}

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:API接口管理 分类(API接口管理)
下一篇:Vue引用第三方datepicker插件无法监听datepicker输入框的值的解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~