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

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

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

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

Java

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

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

Q&A

解決済

1回答

1826閲覧

Spring Boot エラーメッセージをうまく画面に表示できない

Riki1122

総合スコア2

Spring Security

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

Java

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

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

0グッド

0クリップ

投稿2023/07/17 18:35

実現したいこと

Spring Boot 3.0.8とSpring Security6を使用してログイン処理失敗時に、エラーメッセージを画面に表示させたい。かつ、画面更新時にはそのエラーメッセージがなくなっているようにしたい。

発生している問題・エラーメッセージ

※下記はユーザー名は正しいがパスワードが間違った入力の場合のコンソール出力です。
コンソールを見ると/login-errorにPOSTリクエストが来た後に、/loginにリダイレクトされてしまい結果、画面にエラーメッセージが表示されずじまいといった形になってしまいます。

o.s.security.web.FilterChainProxy : Securing POST /login o.s.s.a.dao.DaoAuthenticationProvider : Failed to authenticate since password does not match stored value o.s.security.web.FilterChainProxy : Securing POST /login-error s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.controllers.RootController#onAuthenticationErrorPost(AuthenticationException, Model) s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.controllers.RootController#onAuthenticationErrorPost(AuthenticationException, Model) s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.controllers.RootController#onAuthenticationErrorPost(AuthenticationException, Model) o.s.s.w.a.AnonymousAuthenticationFilter : Set SecurityContextHolder to anonymous SecurityContext o.s.s.web.DefaultRedirectStrategy : Redirecting to http://localhost:18080/login o.s.security.web.FilterChainProxy : Securing GET /login o.s.security.web.FilterChainProxy : Secured GET /login o.s.web.servlet.DispatcherServlet : GET "/login", parameters={} s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.crewsync.controllers.RootController#login() o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] o.s.web.servlet.DispatcherServlet : Completed 200 OK

該当のソースコード

SecurityConfig.java

1package com.example.conf; 2 3import org.springframework.boot.autoconfigure.security.servlet.PathRequest; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; 7import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 9import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10import org.springframework.security.crypto.password.PasswordEncoder; 11import org.springframework.security.web.SecurityFilterChain; 12import org.springframework.security.web.authentication.ForwardAuthenticationFailureHandler; 13 14/** 15 * Spring Security設定クラスです 16 */ 17@Configuration 18@EnableWebSecurity 19@EnableMethodSecurity 20public class SecurityConfig { 21 22 @Bean 23 public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 24 http.formLogin(login -> login 25 .loginPage("/login") 26 .loginProcessingUrl("/login") 27 .defaultSuccessUrl("/") 28 .failureHandler(new ForwardAuthenticationFailureHandler("/login-error")) 29 .usernameParameter("email") 30 .passwordParameter("password") 31 .permitAll()) 32 .logout(logout -> logout 33 .logoutSuccessUrl("/login") 34 .invalidateHttpSession(true) 35 .deleteCookies("JSESSIONID")) 36 .authorizeHttpRequests(authz -> authz 37 .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() 38 .requestMatchers("/img/**").permitAll() 39 .requestMatchers("/").permitAll() 40 .requestMatchers("/register").permitAll() 41 .anyRequest().authenticated()); 42 return http.build(); 43 } 44 45 @Bean 46 public PasswordEncoder passwordEncoder() { 47 return new BCryptPasswordEncoder(); 48 } 49} 50

RootController.java

1package com.example.controllers; 2 3import javax.naming.AuthenticationException; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.security.crypto.password.PasswordEncoder; 7import org.springframework.security.web.WebAttributes; 8import org.springframework.stereotype.Controller; 9import org.springframework.ui.Model; 10import org.springframework.web.bind.annotation.GetMapping; 11import org.springframework.web.bind.annotation.ModelAttribute; 12import org.springframework.web.bind.annotation.PostMapping; 13import org.springframework.web.bind.annotation.RequestAttribute; 14import org.springframework.web.bind.annotation.RequestMapping; 15 16import com.example.auth.LoginUser; 17import com.example.controllers.forms.UserRegisterForm; 18import com.example.domains.services.UserRegisterService; 19 20@Controller 21public class RootController { 22 23 private final UserRegisterService userRegisterService; 24 25 private final PasswordEncoder passwordEncoder; 26 27 @Autowired 28 public RootController(UserRegisterService userRegisterService, PasswordEncoder passwordEncoder) { 29 this.userRegisterService = userRegisterService; 30 this.passwordEncoder = passwordEncoder; 31 } 32 33 @GetMapping("/") 34 public String root() { 35 return "index"; 36 } 37 38 @RequestMapping("/login") 39 public String login() { 40 return "login"; 41 } 42 43 @PostMapping("/login-error") 44 public String onAuthenticationErrorPost( 45 @RequestAttribute(WebAttributes.AUTHENTICATION_EXCEPTION) AuthenticationException ex, 46 Model model) { 47 model.addAttribute("loginError", ex); 48 return "login"; 49 } 50 51 @GetMapping("/register") 52 public String signup() { 53 return "signup"; 54 } 55 56 @PostMapping("/register") 57 public String register(@ModelAttribute UserRegisterForm form) { 58 LoginUser user = new LoginUser(); 59 user.setEmail(form.getEmail()); 60 user.setUsername(form.getUsername()); 61 user.setPassword(passwordEncoder.encode(form.getPassword())); 62 userRegisterService.registerUser(user); 63 return "redirect:/login"; 64 } 65}

login.html

試したこと

色々調べたり、ChatGPTに質問したが解決できなかったため質問させていただきます。

補足情報(FW/ツールのバージョンなど)

Java17
Spring Boot 3.0.8
Spring Security 6.0

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

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

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

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

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

guest

回答1

0

自己解決

エラーオブジェクトをセッションに保存してセッションスコープから取り出す処理に変更したため、自己解決

投稿2023/07/18 16:13

Riki1122

総合スコア2

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問