머리말
이 기사에서는 주로 여러 암호와 호환되는 Spring Security 5.x의 암호화 방법을 소개합니다. 참조와 학습을 위해 공유됩니다. 아래에서 많이 말하지 않겠습니다. 자세한 소개를 함께 살펴 보겠습니다.
1. 스프링 보안 비밀번호 코더
Spring Security 5는 암호의 암호화 방법이 필요하지 않지만 사용자 비밀번호의 접두사는 암호화 방법을 나타냅니다.
Spring Security는 공식적으로보다 안전한 BCrypt 암호화 방법을 사용하는 것이 좋습니다.
이를 통해 동일한 시스템에서 여러 암호화 방법을 지원할 수 있으므로 사용자를 더 쉽게 마이그레이션 할 수 있습니다. Spring Security 5에서 지원하는 암호화 방법은 PasswordEncoderFactories에서 정의됩니다.
public class passwordencoderfactories {public static passwordencoder createLegatingPasswordEncoder () {String encodingId = "bcrypt"; Map <String, passwordencoder> encoders = new Hashmap (); encoders.put (encodingid, 새로운 bcryptpasswordencoder ()); encoders.put ( "ldap", new ldapshapasswordencoder ()); Encoders.put ( "MD4", New Md4passwordencoder ()); Encoders.put ( "MD5", New MessageSageStageStpassWordenCoder ( "MD5")); encoders.put ( "noop", nooppasswordencoder.getInstance ()); Encoders.put ( "PBKDF2", 새로운 PBKDF2PASSWORDENCODER ()); Encoders.put ( "scrypt", new scryptpasswordencoder ()); Encoders.put ( "SHA-1", New MessageSgeigestPasswordEncoder ( "SHA-1")); Encoders.put ( "SHA-256", New MessageSgeigestPasswordenCoder ( "SHA-256")); Encoders.put ( "SHA256", New StandardPasswordenCoder ()); 새로운 위임장 PasswordEncoder (EncodingId, Encoders)를 반환합니다. } private passepordencoderFactories () {}}2 테스트
2.1 pom.xml
<? xml version = "1.0"encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance" xsi : schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modeversion> 4.0.0 </modelversion> <groupid> com.hfcsbc </groupsid> </artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>security</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M7</version> <relativePath/> <!-- 저장소에서 부모 조회-> </parent> <properties> <project.build.build.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.versition> 1.8 </java. <pectionency> <groupId> org.springframework.boot </groupid> <artifactid> 스프링-부트 스타터-보안 </artifactid> </fectionency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> </artifactid> </artifactid> <pectionency> <groupid> org.springframework.security </groupid> <artifactid> spring-security-test </artifactid> <cope> test </scope> </fectionency> <prectionency> <groupId> org.projectlombok </groupid> <artifactid> laLOMITS> </fection> <Plugin> <groupId> org.springframework.boot </groupid> <artifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugin> </build> <repositories> <repository> <id> spring-snapshots </id> spring snaphots </name> <url> https://repo.spring.io/snapshot </url> <snapshots> <enabled> true </enabled> </snapshots> </repository> <repository> <id> Spring-milestones </id> <name> spring milestones </name> <url> httpso.spro. <snapshots> <enabled> false </enabled> </snapshots> </repository> </repository> </repository> </repository> <fluginRepository> <id> spring-snapShots </id> <name> Spring Snapshots </formal> <Url> https://repo.spring.io </snapshot </shots </shots </shots >/snapshots. <enabled> true </enabled> </snapshots> </pluginRepository> <fluginRepository> <id> spring-milestones </id> <name> Spring Milestones </name> <url> https://repo.spring.io/milestone </url> <snapshots> </enabled> </pluginRepository> </pluginRepositories> </project>
2.2 테스트
Spring Security 5.x는 기본적으로 Bcrypt 암호화를 사용합니다
@slf4jpublic class domainuserDetailsService {public static void main (String [] args) {passwordEncoder passwordencoder = passwordEncoDerFactories.CreateDegatingPasswordEncoder (); String encode = passwordencoder.encode ( "password"); log.info ( "암호화 된 암호 :" + encode); log.info ( "bcrypt password 비교 :" + passwordencoder.matches ( "password", encode)); 문자열 md5password = "{md5} 88e2d8e2d8e2d1e92fd554c8621508cd706b"; // md5 암호화 전 암호는 : password log.info ( "md5 암호 비교 :" + passwordencoder.matches ( "password", encode)); }}요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.