質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

0回答

557閲覧

Spring securityを用いたログイン機能でエラーが起きました

sawatomo

総合スコア3

Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2022/10/28 03:55

編集2022/10/28 03:58

前提

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

以上です。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問