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. qt获取屏幕

    m_pDesktopWidget = QApplication::desktop(); // 屏体数量,包含扩展屏 int screenCount = m_pDesktopWidget->scr ...

  2. JavaScript--函数中this的几种指向

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 读JS高性能总结——DOM编程(一)

    DOM是一个与语言无关的API,它在浏览器中的借口却是用JS来实现的. 浏览器通常会把DOM和JS独立实现. 在IE中,JS的实现名是JScript,位于jscript.dll文件中,DOM实现则是m ...

  4. Java版阿里云通信短信发送API接口实例(新)

    阿里云通信(原名阿里大于)的短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码.短信通知等. 完美支撑双11期间2亿用户,发送6亿短信 ...

  5. D3D10/11中的遮挡查询的使用

    原文:D3D10/11中的遮挡查询的使用       在D3D10/11中,有D3D10_QUERY/D3D11_QUERY接口,通过QUERY接口,我们可以查询GPU的一些状态,比如GPU的时间戳信 ...

  6. 重温Observer模式--热水器·改

    引言 在 C#中的委托和事件 一文的后半部分,讲述了Observer(观察者)模式,并使用委托和事件实现了这个模式.实际上,不使用委托和事件,一样可以实现Observer模式.在本文中,我将使用GOF ...

  7. iOS 9 学习系列:UIStack View

    http://www.cocoachina.com/ios/20150921/13492.html 在 iOS9 中,Apple 引入了 UIStackView,他让你的应用可以通过简单的方式,纵向或 ...

  8. 四.使用JDBC进行批处理操作

    1 create table testbatch 2 ( 3 id int primary key, 4 name varchar(20) 5 ); 在实际的项目开发中,有时候需要向数据库发送一批SQ ...

  9. 【JZOJ4848】【GDOI2017模拟11.3】永恒的契约

    题目描述 宅邸迅速的燃烧着,必须带贝蒂走出禁书库!凭着感觉,又一次直接找到禁书库的门. "你,是那个人嘛?"400年了,当初圣域建立结界时没有进入圣域,被伤了心的人工精灵贝蒂,与强 ...

  10. python开发资源链接

    1.docker docker Windows版下载:https://oomake.com/download/docker-windows docker 英文官网:https://www.docker ...