前提
Javaのフレームワーク、Spring bootの勉強をしております。
ログイン機能を作成中にエラーが起きてしまいました。
正しいユーザー名とパスワードを入力したときに下記のエラーが表示されます。
実現したいこと
Spring scurityを使用し、ログイン完了画面への遷移
発生している問題・エラーメッセージ
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Oct 28 12:46:20 JST 2022 There was an unexpected error (type=Not Found, status=404). No message available
該当のソースコード
UserController.java
1package com.gold.controller; 2 3import org.springframework.stereotype.Controller; 4import org.springframework.ui.Model; 5import org.springframework.web.bind.annotation.GetMapping; 6import org.springframework.web.bind.annotation.PostMapping; 7 8@Controller 9public class UserController { 10 11 @GetMapping("/login") 12 public String getLogin(Model model) { 13 return "login"; 14 } 15 16 @GetMapping("/error") 17 public String getLoginError(Model model) { 18 model.addAttribute("ErrorMessage","*ユーザー名もしくはパスワードが一致しません"); 19 return "login"; 20 } 21 22 @PostMapping("/login") 23 public String postLogin(Model model) { 24 return "sample"; 25 } 26 27}
SecurityConfig.java
1package com.gold.security; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 7import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8import org.springframework.security.config.annotation.web.builders.WebSecurity; 9import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 10import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 11import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12import org.springframework.security.crypto.password.PasswordEncoder; 13 14import com.gold.service.UserDetailsServiceImpl; 15 16/** 17 * SpringSecurityを利用するための設定クラス 18 * ログイン処理でのパラメータ、画面遷移や認証処理でのデータアクセス先を設定する 19 */ 20@SuppressWarnings("deprecation") 21@EnableWebSecurity 22@Configuration 23public class SecurityConfig extends WebSecurityConfigurerAdapter { 24 25 //UserDetailsServiceを利用出来るように@Autowiredしておく 26 @Autowired 27 UserDetailsServiceImpl userDetailsService; 28 @Autowired 29 //AuthenticationSuccessHandlerImpl authenticationSuccessHandlerImpl; 30 31 //認証用パスワードはハッシュ化して扱うためPasswordをハッシュ化する際に必要なBCryptPasswordEncoder()を返すメソッドを作成しておく。 32 @Bean 33 public PasswordEncoder passwordEncoder() { 34 return new BCryptPasswordEncoder(); 35 } 36 37 /** 38 * 認可設定を無視するリクエストを設定 39 * 静的リソース(image,javascript,css)を認可処理の対象から除外する 40 */ 41 @Override 42 public void configure(WebSecurity web) throws Exception { 43 } 44 45 /** 46 * 認証・認可の情報を設定する 47 * SpringSecurityのconfigureメソッドをオーバーライドしています。 48 */ 49 @Override 50 protected void configure(HttpSecurity http) throws Exception { 51 // ログイン不要ページの設定 52 http 53 .authorizeRequests() 54 .antMatchers("/login").permitAll() 55 .anyRequest().authenticated(); 56 http 57 .formLogin() 58 .loginPage("/login")//ログインページとして使用するurlを設定する 59 .usernameParameter("username")//Usernameのパラメータとして使用する項目のnameを設定する 60 .passwordParameter("password")//Passwordのパラメータとして使用する項目のpasswordを設定する 61 .defaultSuccessUrl("/sample", true) 62 .failureUrl("/error")//失敗時の遷移先 63 .permitAll(); 64 65 66 http.csrf().disable(); 67 } 68 69 /** 70 * 認証時に利用するデータソースを定義する設定メソッド 71 * ここではDBから取得したユーザ情報をuserDetailsServiceへセットすることで認証時の比較情報としている 72 */ 73 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 74 // UserDetailのOverride 75 auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());//実装クラスでの認証 76 } 77}
login.html
1<!DOCTYPE html> 2<html lang="ja" xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <title>ログイン</title> 6</head> 7<body> 8<p th:if="${ErrorMessage!=null}" th:text="${ErrorMessage}"> <!--ログインエラーの際はエラーメッセージを表示する --> 9<p>ログイン</p> 10 <form th:action="@{/login}" method="post"> 11 <div >ユーザー名</div> 12 <input type="text" id="username" name="username" placeholder="ユーザ名" required autofocus> 13 <div >パスワード</div> 14 <input type="password" id="password" name="password" placeholder="パスワード" required> 15 <div ><button type="submit">ログイン</button></div> 16 </form> 17</body> 18</html>
以上です。
よろしくお願いいたします。

あなたの回答
tips
プレビュー