序文
この記事では、主に、複数のパスワードと互換性のあるSpring Security 5.xの暗号化方法を紹介します。参照と学習のために共有されます。以下ではあまり言いません。詳細な紹介を一緒に見てみましょう。
1。スプリングセキュリティPasswordEncoder
Spring Security 5にはパスワードの暗号化方法は必要ありませんが、ユーザーパスワードのプレフィックスは次のような暗号化方法を示します。
Spring Securityは、より安全なBcrypt暗号化方法の使用を公式に推奨しています。
これにより、複数の暗号化方法を同じシステムでサポートできるようになり、ユーザーの移行が容易になります。 Spring Security 5によってサポートされる暗号化方法は、PasswordEncoderFactoriesで定義されています。
public class passwordEncoderFactories {public static passwordEncoder createdElegatingPassWordENCODER(){string encodingId = "bcrypt"; map <string、passdapEncoder> encoders = new Hashmap(); encoders.put(encodingid、new bcryptpasswordencoder()); encoders.put( "ldap"、new ldapshapasswordencoder()); encoders.put( "md4"、new MD4PassWordENCODER()); encoders.put( "md5"、new MessagedGestPassWordENCODER( "MD5")); encoders.put( "noop"、nooppasswordencoder.getInstance()); encoders.put( "pbkdf2"、new pbkdf2passwordencoder()); encoders.put( "scrypt"、new scryptpasswordencoder()); encoders.put( "sha-1"、new MessagedGestPassWordENCoder( "SHA-1")); encoders.put( "sha-256"、new MessagedGestPassWordENCODER( "SHA-256")); encoders.put( "sha256"、new StandardPassWordenCoder()); New DelegatingPassWordENCODER(encodingID、encoders)を返します。 } private passhaptencoderfactories(){}}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 <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modeleversion> 4.0.0 </modelversion> <artifidid> </groupidid> </groupidid> b <バージョン> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> security </name> <description> spring boot </descripted> <parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-parent <リポジトリからのlookup parent-> </parent> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <依存関係> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-security </artifactid> </dependency> <dependency> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-stertertest </scope <Dependency> groupId> org.springframework.security </groupid> <artifactid> spring-security-test </artifactid> <scope> test </scope> </dependency> <sependency> <groupid> org.projectlombok </groupid> <artifactid> edecifactid> </<burd> <Plugin> <groupId> org.springframework.boot </groupid> <artifactid> spring-boot-maven-plugin </artifactid> </glugin> </blagin> </build> <リポジトリ<url> https://repo.spring.io/snapshot </url> <SnapShots> <Enabled> true </enabled> </snapshots> </repository> <リポジトリ> <id> spring-milestones </id> <name> spring mirestones </name> <url> https:urepo/ <SnapShots> <Enabled> false </enabled> </snapshots> </repository> </repository> </repository> </repository> <pluginrepository> <id> spring-snapshots </id> <name> spring snapshots </name> <url> https://Repo.spring <Enabled> true </enabled> </snapshots> </plaginRepository> <pluginrepository> <id> spring-milestones </name> <url> https://repo.spring.io/milestone </url> <snapshots> </enabed> </enabled> </plaginRepository> </plaginRepository> </plaginRepositories> </project>
2.2テスト
Spring Security 5.xは、デフォルトでBCRYPT暗号化を使用します
@slf4jpublic class domainuserdetailsservice {public static void main(string [] args){passwordencoder passwordEncoder = passwordencoderfactories.createdelegatingpasswordencoder();文字列encode = passwordencoder.encode( "password"); log.info( "暗号化されたパスワード:" +エンコード); log.info( "bcryptパスワードの比較:" + passwordencoder.matches( "password"、encode)); String MD5PassWord = "{MD5} 88E2D8CD1E92FD554C8621508CD706B"; // MD5暗号化前のパスワードは次のとおりです。 }}要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。