1 spring security PasswordEncoder

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

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

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

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


  1. public class PasswordEncoderFactories {
  2. public static PasswordEncoder createDelegatingPasswordEncoder() {
  3. String encodingId = "bcrypt";
  4. Map<String, PasswordEncoder> encoders = new HashMap();
  5. encoders.put(encodingId, new BCryptPasswordEncoder());
  6. encoders.put("ldap", new LdapShaPasswordEncoder());
  7. encoders.put("MD4", new Md4PasswordEncoder());
  8. encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
  9. encoders.put("noop", NoOpPasswordEncoder.getInstance());
  10. encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
  11. encoders.put("scrypt", new SCryptPasswordEncoder());
  12. encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));
  13. encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));
  14. encoders.put("sha256", new StandardPasswordEncoder());
  15. return new DelegatingPasswordEncoder(encodingId, encoders);
  16. }
  17. private PasswordEncoderFactories() {
  18. }
  19. }

2 测试

2.1 pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.hfcsbc</groupId>
  6. <artifactId>security</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>security</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.0.M7</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-security</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.security</groupId>
  34. <artifactId>spring-security-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.projectlombok</groupId>
  39. <artifactId>lombok</artifactId>
  40. </dependency>
  41. </dependencies>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. <repositories>
  51. <repository>
  52. <id>spring-snapshots</id>
  53. <name>Spring Snapshots</name>
  54. <url>https://repo.spring.io/snapshot</url>
  55. <snapshots>
  56. <enabled>true</enabled>
  57. </snapshots>
  58. </repository>
  59. <repository>
  60. <id>spring-milestones</id>
  61. <name>Spring Milestones</name>
  62. <url>https://repo.spring.io/milestone</url>
  63. <snapshots>
  64. <enabled>false</enabled>
  65. </snapshots>
  66. </repository>
  67. </repositories>
  68. <pluginRepositories>
  69. <pluginRepository>
  70. <id>spring-snapshots</id>
  71. <name>Spring Snapshots</name>
  72. <url>https://repo.spring.io/snapshot</url>
  73. <snapshots>
  74. <enabled>true</enabled>
  75. </snapshots>
  76. </pluginRepository>
  77. <pluginRepository>
  78. <id>spring-milestones</id>
  79. <name>Spring Milestones</name>
  80. <url>https://repo.spring.io/milestone</url>
  81. <snapshots>
  82. <enabled>false</enabled>
  83. </snapshots>
  84. </pluginRepository>
  85. </pluginRepositories>
  86. </project>
2.2 测试

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


  1. @Slf4j
  2. public class DomainUserDetailsService {
  3. public static void main(String[] args){
  4. PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
  5. String encode = passwordEncoder.encode("password");
  6. log.info("加密后的密码:" + encode);
  7. log.info("bcrypt密码对比:" + passwordEncoder.matches("password", encode));
  8. String md5Password = "{MD5}88e2d8cd1e92fd5544c8621508cd706b";//MD5加密前的密码为:password
  9. log.info("MD5密码对比:" + passwordEncoder.matches("password", encode));
  10. }
  11. }

原文地址:https://blog.csdn.net/wiselyman/article/details/84915939

Spring Security 5.x兼容多种密码加密方式的更多相关文章

  1. laravel更改默认的登录密码加密方式

    laravel更改默认的登录密码加密方式   laravel 默认用的登录密码加密方式是: $password = Hash::make('password'); 而我平时用的密码加密方式是: $pa ...

  2. 关于mysql8.0及以上版本连接navicat时候报错(密码加密方式需要修改)

    首先这个原因是因为MySQL版本的密码加密方式变了,要把它修改成以前的方式(因为,navicat不支持这种方式) 1:先进入mysql: mysql -uroot -p123456; 2:查询密码加密 ...

  3. Spring Security笔记:使用BCrypt算法加密存储登录密码

    在前一节使用数据库进行用户认证(form login using database)里,我们学习了如何把“登录帐号.密码”存储在db中,但是密码都是明文存储的,显然不太讲究.这一节将学习如何使用spr ...

  4. Spring Security中的MD5盐值加密

    在 spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字 ...

  5. Spring Security 5中的默认密码编码器

    1.概述 在Spring Security 4中,可以使用内存中身份验证以纯文本格式存储密码. 对版本5中的密码管理过程进行了重大改进,为密码编码和解码引入了更安全的默认机制.这意味着如果您的Spri ...

  6. Ecstore会员密码加密方式破解

    <?php //以下是加密方式,亲测有效 $string_md5 = md5(md5("密码")."用户名"."注册时间");//三个 ...

  7. Spring Boot 2.0 利用 Spring Security 实现简单的OAuth2.0认证方式1

    0. 前言 之前帐号认证用过自己写的进行匹配,现在要学会使用标准了.准备了解和使用这个OAuth2.0协议. 1. 配置 1.1 配置pom.xml 有些可能会用不到,我把我项目中用到的所有包都贴出来 ...

  8. Django 自带密码加密,自定密码加密方式 及自定义验证方式

    在django1.6中,默认的加密方式是pbkdf_sha256,具体算法不表,一直以来用django的自带用户验证都十分顺手,今天有需求,需要修改默认加密方式为md5,具体方法为: 在setting ...

  9. spring security结合数据库验证用户-XML配置方式

    之前的用户信息我们都是使用的内存用户,测试例子可以,实际中使用肯定不行,需要结合数据库进行验证用户.这就是本节的重点: 项目目录如下:  在之前的项目中的依赖中添加两个依赖: <dependen ...

随机推荐

  1. 非接触型手掌静脉识别 PalmSecure™

    静脉识别,使用近红外线读取静脉模式,与存储的静脉模式进行比较,进行本人识别的识别技术.富士通的PalmSecure™,利用该技术,由离开识别装置的位置,使用近红外线拍摄,与预先存储的静脉模式进行比较从 ...

  2. 【Django入坑之路】Django后台上传图片,以及前端的显示

    #setting配置: MEDIA_URL = "/media/" MEDIA_ROOT = os.path.join(BASE_DIR, "media") # ...

  3. oracle审计的格式

    audit table; audit table by xxx(username); audit table by xxx(username) whenever not successful; 系统表 ...

  4. JavaScript的六种数据类型与隐式转换

    一.六种数据类型 javascript的数据类型包括: (1)基本数据类型:number.string.boolean.null.undefined (2)对象:object object又包括Fun ...

  5. 我的iOS高效编程秘诀—坚持编程习惯

    http://www.cocoachina.com/programmer/20150819/13103.html 作者:sunljz 授权本站转载. 习惯会影响一个人做事的方式,也会直接影响效率.我经 ...

  6. 2018-9-30-C#-从零开始写-SharpDx-应用-画三角

    title author date CreateTime categories C# 从零开始写 SharpDx 应用 画三角 lindexi 2018-09-30 18:30:14 +0800 20 ...

  7. 10Redis键空间通知(keyspace notifications)

    Redis的键空间通知(keyspace notifications)功能是自2.8.0版本开始加入的,客户端可以通过订阅/发布(Pub/Sub)机制,接收那些以某种方式改变了Redis数据空间的事件 ...

  8. 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google

    摘要: 作为此次Gartner报告中唯一上榜的中国科技公司,阿里云获得六个评判维度的最高分,排名第二 近日,知名调研机构Gartner发布了全球领先公共云厂商区块链服务能力报告,作为唯一上榜的中国科技 ...

  9. CSS文本超过两行用省略号代替

    1.只显示一行,超出部分用省略号 white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 2.只显示两行(或多行),超出部分用省 ...

  10. 自定义View系列教程08--滑动冲突的产生及其处理

    深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...