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中定義:


   
   
  
  
          
  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM