Spring security 5.x implements multiple password-compatible encryption methods.

Source: Internet
Author: User

Spring security 5.x implements multiple password-compatible encryption methods.

Preface

This article mainly introduces spring security 5. x is compatible with various encryption methods for your reference. I will not talk much about it here. Let's take a look at the detailed introduction.

1. spring security PasswordEncoder

Spring security 5 does not need to configure the password encryption method. Instead, the user password is prefixed to indicate the encryption method, such:

  • {MD5} 88e2d8cd1e92fd5544c8621508cd706b indicates that MD5 encryption is used;
  • {Bcrypt} $ 2a $10 $ eZeGvVV2ZXr/vgiVFzqzS. jlv878apbgrt9mapk1wr1_ovsf4yui6 indicates the bcrypt encryption method.

Spring security officially recommends a safer bcrypt encryption method.

In this way, multiple encryption methods can be supported in the same system, saving trouble for migrating users. The encryption methods supported by spring security 5 are defined in PasswordEncoderFactories:

public class PasswordEncoderFactories { public static PasswordEncoder createDelegatingPasswordEncoder() {  String encodingId = "bcrypt";  Map<String, PasswordEncoder> encoders = new HashMap();  encoders.put(encodingId, new BCryptPasswordEncoder());  encoders.put("ldap", new LdapShaPasswordEncoder());  encoders.put("MD4", new Md4PasswordEncoder());  encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));  encoders.put("noop", NoOpPasswordEncoder.getInstance());  encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());  encoders.put("scrypt", new SCryptPasswordEncoder());  encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));  encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));  encoders.put("sha256", new StandardPasswordEncoder());  return new DelegatingPasswordEncoder(encodingId, encoders); } private PasswordEncoderFactories() { }}

2 Test

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"> <modelVersion>4.0.0</modelVersion> <groupId>com.hfcsbc</groupId> <artifactId>security</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/> <!-- lookup parent from repository --> </parent> <properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  <java.version>1.8</java.version> </properties> <dependencies>  <dependency><groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-security</artifactId>  </dependency>  <dependency><groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-test</artifactId>   <scope>test</scope>  </dependency>  <dependency>   <groupId>org.springframework.security</groupId>   <artifactId>spring-security-test</artifactId>   <scope>test</scope>  </dependency>  <dependency>   <groupId>org.projectlombok</groupId>   <artifactId>lombok</artifactId>  </dependency> </dependencies> <build>  <plugins>   <plugin><groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId>   </plugin>  </plugins> </build> <repositories>  <repository>   <id>spring-snapshots</id>   <name>Spring Snapshots</name>   <url>https://repo.spring.io/snapshot</url>   <snapshots>    <enabled>true</enabled>   </snapshots>  </repository>  <repository>   <id>spring-milestones</id>   <name>Spring Milestones</name>   <url>https://repo.spring.io/milestone</url>   <snapshots>    <enabled>false</enabled>   </snapshots>  </repository> </repositories> <pluginRepositories>  <pluginRepository>   <id>spring-snapshots</id>   <name>Spring Snapshots</name>   <url>https://repo.spring.io/snapshot</url>   <snapshots>    <enabled>true</enabled>   </snapshots>  </pluginRepository>  <pluginRepository>   <id>spring-milestones</id>   <name>Spring Milestones</name>   <url>https://repo.spring.io/milestone</url>   <snapshots>    <enabled>false</enabled>   </snapshots>  </pluginRepository> </pluginRepositories></project>

2.2 Test

Spring security 5.x uses bcrypt Encryption by default

@ Slf4jpublic class DomainUserDetailsService {public static void main (String [] args) {PasswordEncoder passwordEncoder = PasswordEncoderFactories. createDelegatingPasswordEncoder (); String encode = passwordEncoder. encode ("password"); log.info ("encrypted password:" + encode); log.info ("bcrypt password comparison:" + passwordEncoder. matches ("password", encode); String md5Password = "{MD5} 88e2d8cd1e92fd5544c8621508cd706b"; // The password before MD5 encryption is: password log.info ("MD5 password comparison: "+ passwordEncoder. matches ("password", encode ));}}

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.