目的:BCryptPasswordEncorderを使用してパスワードをハッシュ化したい
ログイン画面の動作確認のためDBに保存しているアカウントでログインしたいのですがDBにはハッシュ化されたパスワードをいれなければならないので困っています。
本の通りに進めているのですが本ではINSERT文でハッシュ化されたパスワードを直接いれててどうやってハッシュ化したものを確認したのかわからないといった状況です。
追記 ハッシュ化したパスワードを確認しようとアプリケーションのmainメソッドを以下のように変更したところアプリケーションを起動するたび表示されるハッシュ値がかわってしまいました。何が原因なんでしょうか
追記2 どうやらbcryptを使うと同じパスワードでもハッシュ値が毎回変わるようです。どうしたらパスワードをハッシュ化したものを表示しINSERTでDBにいれることができるのでしょうか?
追記3 アプリケーション起動→ハッシュ値確認→アプリケーション終了→DBにかくにんしたハッシュ値を登録→mainメソッドからハッシュ値生成に関係するメソッドを除去→アプリケーション起動でもなぜかエラーが発生してしまいました。
Spring Security
PostgreSQL
WebSecurityConfig.java
package mrs; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import mrs.domain.service.user.ReservationUserDetailsService; @Configuration //Spring Securityのweb連携機能を有効にする @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired ReservationUserDetailsService userDetailsService; @Bean PasswordEncoder passwordEncoder() { //パスワードのエンコードアルゴリズムとしてBCryptを使用したBCryptPasswordEncoderを有効にする return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http)throws Exception{ http.authorizeRequests() //js以下と/css以下へのアクセスは常に許可 .antMatchers("/js/**","/css/**").permitAll() //それ以外のアクセスは認証を要求(authenticated)する .antMatchers("/**").authenticated() .and() .formLogin() //ログインフォームのパス .loginPage("/loginForm") //認証処理のパス .loginProcessingUrl("/login") .usernameParameter("username") .passwordParameter("password") //認証成功時の飛び先 .defaultSuccessUrl("/rooms",true) //失敗時の遷移先へのアクセス許可 .failureUrl("/loginForm?error=true").permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception{ auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } }
data.sql
一部抜粋 INSERT INTO usr (user_id,first_name,last_name,password,role_name) VALUES ('aaaa','Aaa','Aaa','/*ここにハッシュ化したパスワードをいれたい*/','USER');
MrsApplication.java
package mrs; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @SpringBootApplication public class MrsApplication{ public static void main(String[] args) { SpringApplication.run(MrsApplication.class, args); BCryptPasswordEncoder a =new BCryptPasswordEncoder(); String p = "demo"; String d = a.encode(p); System.out.println(d); } }
###loginForm.html(ログイン画面)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title></title> </head> <body> <h3>ログインフォーム</h3> <p th:if="${param.error}"> Error! </p> <form th:action="@{/login}" method="POST"> <table> <tr> <td><label for="username">User:</label></td> <td><input type="text" id="username" name="username" value="aaaa"/></td> </tr> <tr> <td><label for="password">Password:</label></td> <td><input type="password" id="password" name ="password" value="demo"/></td> <tr> <td> </td> <td><button type="submit">ログイン</button></td> </tr> </table> </form> </body> </html>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/02 04:12
2019/06/02 05:29
2019/06/02 06:02 編集
2019/06/02 10:20 編集
退会済みユーザー
2019/06/02 10:46
2019/06/02 11:03
2019/06/02 11:06
2019/06/02 11:22
退会済みユーザー
2019/06/02 11:35
2019/06/02 11:45
2019/06/02 12:28
退会済みユーザー
2019/06/02 13:09
2019/06/02 14:45
2019/06/03 00:45 編集
2019/06/03 01:17
2019/06/03 03:20
2019/06/03 06:14
2019/06/03 06:28
2019/06/03 06:51 編集