Spring MVC 5.3 BCryptPasswordEncoderで文字列をハッシュ化するサンプル
環境
Spring 5.3.23
JDK8
Eclipse 4.14.0
構文
public class BCryptPasswordEncoder
BCrypt の強力なハッシュ関数を使用する PasswordEncoder の実装。 クライアントは、オプションで「バージョン」($2a、$2b、$2y)と「強度」(別名 BCrypt のログラウンド)と SecureRandom インスタンスを提供できます。
public boolean matches(CharSequence rawPassword, String encodedPassword)
ストレージから取得したエンコードされたパスワードが、エンコードされた後に送信された生のパスワードと一致することを確認します。
操作方法
1.pom.xmlの設定
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.7.5</version>
</dependency>
</dependencies>
spring-security-core 5.7.5 を指定しています
2.起動ファイル
package com.arkgame.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ArkgameApp {
public static void main(String[] args) {
// applicationContext.xmlを読み込みます
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 「クラス名.class」を取得
UserService userService = context.getBean(UserService.class);
//getPasswordメソッドを呼び出します
userService.getPassword();
context.close();
}
}
説明
applicationContext.xmlを読み込みます
new ClassPathXmlApplicationContext("applicationContext.xml");
3.BCryptPasswordEncoderでハッシュ化するファイル
package com.arkgame.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
//アノテーション@Component
@Component
public class UserService {
@Autowired
BCryptPasswordEncoder passEncoder;
public void getPassword() {
String target = "東京太郎";
// BCryptPasswordEncoderで文字列をハッシュ化する
String result = passEncoder.encode(target);
System.out.println("BCryptPasswordEncoderでハッシュ化する結果: " + result);
// 元の文字列とハッシュ化した文字列を比較する
if (passEncoder.matches(target, result)) {
System.out.println("ハッシュ化した値と元の値が同じです");
}
}
}
インスタンスを生成します
@Autowired BCryptPasswordEncoder passEncoder; BCryptPasswordEncoderで文字列をハッシュ化しています。 passEncoder.encode(文字列)
4.設定ファイル(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- BCryptPasswordEncoderをDIコンテナに登録 -->
<bean id="passwdEncoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
<!-- context:component-scanで対象パッケージを指定 -->
<context:component-scan base-package="com.arkgame.test" />
</beans>
5.動作確認
「ArkgameApp.java」を右クリックして「実行」->「Javaアプリケーション」をクリックします コンソールに下記メッセージが表示されます。
実行結果
BCryptPasswordEncoderでハッシュ化する結果: $2a$10$nRkyv1weNUpANzSJ4ZB3WObOBUn0AifWmJipBUJshUKSehiZJXMXi ハッシュ化した値と元の値が同じです