คำนำ
บทความนี้ส่วนใหญ่แนะนำวิธีการเข้ารหัสของ Spring Security 5.x ที่เข้ากันได้กับรหัสผ่านหลายรหัส มันถูกแบ่งปันสำหรับการอ้างอิงและการเรียนรู้ของคุณ ฉันจะไม่พูดด้านล่างมากนักลองมาดูการแนะนำรายละเอียดด้วยกัน
1. Spring Security PasswordEncoder
Spring Security 5 ไม่จำเป็นต้องใช้วิธีการเข้ารหัสของรหัสผ่าน แต่คำนำหน้าของรหัสผ่านผู้ใช้ระบุวิธีการเข้ารหัสเช่น:
Spring Security แนะนำอย่างเป็นทางการโดยใช้วิธีการเข้ารหัส bcrypt ที่ปลอดภัยยิ่งขึ้น
สิ่งนี้ช่วยให้สามารถรองรับวิธีการเข้ารหัสหลายวิธีในระบบเดียวกันทำให้ง่ายต่อการย้ายผู้ใช้ วิธีการเข้ารหัสที่รองรับโดย Spring Security 5 ถูกกำหนดใน PasswordEncoderFactories:
Public PasswordEncoderFactories {Public Static Passwateencoder CreateLeGatingPasswordEncoder () {string encodingId = "bcrypt"; แผนที่ <สตริงรหัสผ่าน> encoders = new hashmap (); encoders.put (encodingId, bcryptpasswordencoder ใหม่ ()); encoders.put ("LDAP", LDAPSHAPASSWORDENCODER ใหม่ ()); encoders.put ("MD4", ใหม่ MD4PasswordEncoder ()); encoders.put ("MD5", New MessageGeStPasswordEncoder ("MD5")); encoders.put ("noop", nooppasswordencoder.getInstance ()); encoders.put ("PBKDF2", ใหม่ PBKDF2PasswordEncoder ()); encoders.put ("scrypt", ใหม่ scryptpasswordencoder ()); encoders.put ("sha-1", ใหม่ messageGeSpasswardencoder ("sha-1")); encoders.put ("sha-256", ใหม่ messageGeSpasswardencoder ("sha-256")); encoders.put ("sha256", ใหม่ StandardPasswordEncoder ()); ส่งคืนผู้แทนใหม่ PasswordEncoder (encodingId, encoders); } Private PasswordEncoderFactories () {}}2 การทดสอบ
2.1 pom.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <cersion> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> ความปลอดภัย </name> <scription> โครงการสาธิตสำหรับการบูตฤดูใบไม้ผลิ </คำอธิบาย> <parent> <mentar> การค้นหาผู้ปกครองจากที่เก็บ-> </parent> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.raporting.outputencoding> UTF-8 </project.reporting.UutPutuCoding> <การพึ่งพา> <roupId> org.springframework.boot </groupid> <ratifactid> Spring-Boot-Starter-Security </ArtifactId> </การพึ่งพา> <การพึ่งพา> <roupId> org.springframework.boot </groupid> <การพึ่งพา> <roupId> org.springframework.security </groupid> <ratifactid> การทดสอบความปลอดภัยในฤดูใบไม้ผลิ </artifactid> <scope> ทดสอบ </cope> </การพึ่งพาอาศัย> <การพึ่งพา> <roupid> org.projectlombok </groupid> <plugin> <splousid> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </build> <positories> <url> https://repo.spring.io/snapshot </url> <naplots> <enabled> true </enabled> </napshots> </repository> <repository> <id> spring-milestones </id> <name> ฤดูใบไม้ผลิ <snapshots> <enabled> false </enabled> </napshots> </repository> </repository> </repository> </repository> <pluginRepository> <id> Spring-Snapshots </id> <name> snapshots </name> <url> https://repo.spring.spring <enabled> True </enabled> </snapshots> </pluginRepository> <pluginRepository> <id> Spring-Milestones </id> <name> Stars Milestones </name> <url> https://repo.spring.io/milestone </url> </pluginRepository> </pluginRepositories> </project>
2.2 การทดสอบ
Spring Security 5.x ใช้การเข้ารหัส bcrypt โดยค่าเริ่มต้น
@slf4jpublic คลาส domainuserDetailsService {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {passwordencoder passwordencoder = passwordencoderfactories string encode = passwordencoder.encode ("รหัสผ่าน"); log.info ("รหัสผ่านที่เข้ารหัส:" + เข้ารหัส); log.info ("การเปรียบเทียบรหัสผ่าน bcrypt:" + passwordencoder.matches ("รหัสผ่าน", เข้ารหัส)); สตริง md5password = "{md5} 88e2d8cd1e92fd5544c8621508cd706b"; // รหัสผ่านก่อนการเข้ารหัส MD5 คือ: รหัสผ่าน log.info ("การเปรียบเทียบรหัสผ่าน MD5:" + รหัสผ่าน -สรุป
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่าเนื้อหาของบทความนี้จะมีค่าอ้างอิงบางอย่างสำหรับการศึกษาหรือที่ทำงานของทุกคน หากคุณมีคำถามใด ๆ คุณสามารถฝากข้อความไว้เพื่อสื่อสาร ขอบคุณสำหรับการสนับสนุน Wulin.com