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

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

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

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

Java

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

Spring Boot

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

Q&A

解決済

1回答

3478閲覧

Spring Security ログイン後画面遷移しない

nepia2000

総合スコア1

Spring Security

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

Java

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

Spring Boot

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

0グッド

0クリップ

投稿2021/03/22 12:49

編集2021/03/22 12:51

ログイン後はhttp://localhost:8080/sampleになりますが
Whitelabel Error Pageと表示され404が表示されます。
templateの下にsample.htmlを配置しています。
コードを見ても、どこが違うのか分からず。
おかしい箇所があれば、教えて頂きたいです。

java

1package com.example.demo; 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 SampleController { 10 @GetMapping("/login") 11 public String form() { 12 return "login.html"; 13 } 14 15 @GetMapping("/error1")//errorが発生した際はエラーメッセージを表示したいので違う処理に入るように制御する 16 public String getLoginError(Model model) { 17 model.addAttribute("ErrorMessage","*ユーザー名もしくはパスワードが一致しません"); 18 return "login"; 19 } 20 21 //デフォルトではPostでリクエストが発生。カスタマイズ時はユーザーが指定したmethodに従う。 22 @PostMapping("/login") 23 public String success() { 24 return "sample"; 25 } 26} 27

java

1package com.example.demo; 2 3import org.springframework.context.annotation.Configuration; 4import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5import org.springframework.security.config.annotation.web.builders.WebSecurity; 6import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 7import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 8 9import org.springframework.beans.factory.annotation.Autowired; 10import org.springframework.context.annotation.Bean; 11import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 12import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 13import org.springframework.security.crypto.password.PasswordEncoder; 14 15 16 17/** 18 * SpringSecurityを利用するための設定クラス 19 * ログイン処理でのパラメータ、画面遷移や認証処理でのデータアクセス先を設定する 20 */ 21@Configuration 22@EnableWebSecurity 23public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 24 25 @Autowired 26 private UserDetailsServiceImpl userDetailsService; 27 28 @Bean 29 public PasswordEncoder passwordEncoder() { 30 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); 31 return new BCryptPasswordEncoder(); 32 } 33 34 @Override 35 public void configure(WebSecurity web) throws Exception { 36 web.ignoring() 37 .antMatchers("/resources/**"); 38 } 39 40 @Override 41 protected void configure(HttpSecurity http) throws Exception { 42 http 43 .authorizeRequests() 44 .antMatchers("/login").permitAll() 45 .anyRequest().authenticated(); 46 47 http 48 .formLogin() 49 .loginPage("/login")//ログインページとして使用するurlを設定する 50 .usernameParameter("username")//Usernameのパラメータとして使用する項目のnameを設定する 51 .passwordParameter("password")//Passwordのパラメータとして使用する項目のnameを設定する 52 .failureUrl("/error1")//エラー発生時として使用するurlを設定する 53 .defaultSuccessUrl("/sample", true) 54 .permitAll();//エラー発生画面も未認証でアクセス出来るようにしないといけない。(この記述がないと指定のurlに遷移せずloginにリダイレクトされる) 55 56 } 57 58 /** 59 * 認証時に利用するデータソースを定義する設定メソッド 60 * ここではDBから取得したユーザ情報をuserDetailsServiceへセットすることで認証時の比較情報としている 61 */ 62 @Autowired 63 public void configure(AuthenticationManagerBuilder auth) throws Exception{ 64 //UserDetailsServiceを設定してDaoAuthenticationProviderを有効化する 65 auth.userDetailsService(userDetailsService). 66 //上記作成のエンコードを設定しハッシュ化する 67 passwordEncoder(passwordEncoder()); 68 } 69}

html

1<!DOCTYPE html> 2<html 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>

html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"> 5<title>Insert title here</title> 6</head> 7<body> 8 ログイン完了 9</body> 10</html>

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

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

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

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

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

guest

回答1

0

ベストアンサー

WebSecurityConfigクラスを見ると、
ログイン成功後は、/sampleへリダイレクトされるような設定になってるので、
それに対応するコントローラのメソッドが必要だと思います。

現状は、/sampleへマップされたメソッドが無いので、404エラーが発生するのでしょう。

例えばSampleControllerクラスに、

java

1@GetMapping("/sample") 2public String sample() { 3 return "sample"; 4}

↑このようなメソッドを追加すれば、「ログイン完了」が出るんじゃないかと思います。

投稿2021/03/22 13:39

gpsoft

総合スコア1323

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

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

nepia2000

2021/03/23 13:11

解決しました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問