初めての質問ですがよろしくお願いします。
前提・実現したいこと
勉強もかねてwebアプリを作り始めました。
ログインはSpring Securityで制御しています。
パスワードはSHA-256でハッシュ化されたものを使いたいので、入力されたパスワードを取得したいのですが、どのようにとってこればいいかわかりません。
コードの書き方等直すべきところがいろいろあるかもしれませんが、取り急ぎまずはログイン画面での入力値の取り方を教えてほしいです。
環境
・Windows
・Eclipse
・Spring Security
・Spring Boot
・Java
発生している問題・エラーメッセージ
エラーはなし
該当のソースコード
セキュリティ設定
SecurityConfig
1package webapp.config; 2 3import java.io.File; 4import java.security.MessageDigest; 5import java.util.Scanner; 6 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.beans.factory.annotation.Value; 9import org.springframework.context.annotation.Configuration; 10import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 11import org.springframework.security.config.annotation.web.builders.HttpSecurity; 12import org.springframework.security.config.annotation.web.builders.WebSecurity; 13import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 14import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 15 16 17 18 19@Configuration 20@EnableWebSecurity 21public class SecurityConfig extends WebSecurityConfigurerAdapter{ 22 23 /** 24 * ログインID、パスワード取得用ファイルパス 25 */ 26 @Value("${loginInfo.File.Path}") 27 private final String loginInfoFilePath = new String(); 28 29 @Override 30 public void configure(WebSecurity web) { 31 web 32 .ignoring() 33 .antMatchers("/css/system.css"); 34 35 } 36 37 @Override 38 public void configure(HttpSecurity http) throws Exception { 39 http.csrf().disable(); 40 http 41 .authorizeRequests() 42 .antMatchers("/", "/login").permitAll(); 43 http 44 .formLogin() 45 .loginProcessingUrl("/login") 46 .loginPage("/login") 47 .usernameParameter("loginId") 48 .passwordParameter("password") 49 .failureUrl("/login?error") 50 .defaultSuccessUrl("/main"); 51 http 52 .logout() 53 .logoutSuccessUrl("/login"); 54 } 55 56 @Autowired 57 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 58 59 File loginInfoFile = new File(loginInfoFilePath); 60 61 if(loginInfoFile.exists()) { 62 63 //ファイルからユーザーIDとパスワード(ハッシュ化されたもの)を取得 64 Scanner scanner = new Scanner(loginInfoFile); 65 String username = scanner.nextLine(); 66 String password = scanner.nextLine(); 67 scanner.close(); 68 69 //このあたりで入力されたパスワードを取得したい// 70 71 //HA-256ハッシュ化 72 byte[] cipher_byte; 73 try{ 74 MessageDigest md = MessageDigest.getInstance("ハッシュ化したい文字"); 75 md.update(password.getBytes()); 76 cipher_byte = md.digest(); 77 StringBuilder sb = new StringBuilder(2 * cipher_byte.length); 78 for(byte b: cipher_byte) { 79 sb.append(String.format("%02x", b&0xff) ); 80 } 81 System.out.println( sb ); 82 83 } catch (Exception e) { 84 e.printStackTrace(); 85 } 86 87 auth 88 .inMemoryAuthentication() 89 .withUser(username).password(password).roles("USER"); 90 } 91 } 92} 93
ログイン画面
login.html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5</head> 6<body> 7 <form id="loginForm" name="form" class="login-form" th:action="@{'/login'}" method="post"> 8 <label class="login-label" for="loginId">ユーザーID</label> 9 <input id="loginId" name="loginId" type="text" class="login-text" value="" maxlength="100"/> 10 <label class="login-label" for="password">パスワード</label> 11 <input id="password" name="password" type="password" class="login-text" value="" maxlength="20"/> 12 <button type="submit" class="login-button">ログイン</button> 13 <div th:if="${loginError}" class="login-spacing-bottom"> ユーザーID、または、パスワードが間違っています。</div> 14 </form> 15</body> 16</html>
あなたの回答
tips
プレビュー