spring security 5 xは複数のパスワードに対応した暗号化方式を実現します。


前言
この記事では、spring security 5 xの複数の暗号化を可能にする方法について紹介します。参考に学習するために、以下の話は多くなくなりました。詳しく紹介してみましょう。
1、スプリングsecurity Password Engcoder
spring security 5は、パスワードを設定する暗号化方式を必要とせず、ユーザパスワードにプレフィックスを付ける方式は、暗号化方式を示す。
  • {MD 5}88 e 2 d 8 cd 1 924 fd 5544 c 8621508 cd 706 bは、MD 5暗号化方式を表す。
  • {bcrypt}$2 a-10 eZeGvVV 2 ZXr/vgiVFzq S.JLV 878 AppBgRT 9 maPK 1 Wrg 0 osf 4 YuI 6は、bcrypt暗号化方式を表すものです。
  • spring securityはより安全なbcrypt暗号化方式を推奨しています。
    このようにして、同じシステムで複数の暗号化方式をサポートしてもいいです。spring security 5がサポートする暗号化方式は、Password Entocder Factoresで定義されています。
    
    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テスト
    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試験
    spring security 5 xデフォルトではbcryptを使って暗号化します。
    
    @Slf4j
    public class DomainUserDetailsService {
     public static void main(String[] args){
      PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
      String encode = passwordEncoder.encode("password");
      log.info("      :" + encode);
      log.info("bcrypt    :" + passwordEncoder.matches("password", encode));
      String md5Password = "{MD5}88e2d8cd1e92fd5544c8621508cd706b";//MD5       :password
      log.info("MD5    :" + passwordEncoder.matches("password", encode));
     }
    }

    締め括りをつける
    以上はこの文章の全部の内容です。本文の内容は皆さんの学習や仕事に対して一定の参考となる学習価値を持っています。質問があれば、メッセージを書いて交流してください。ありがとうございます。