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

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

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

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

Java

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

Spring

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

Spring Boot

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

Q&A

解決済

1回答

1143閲覧

Spring Securityのルーティング

sanezane

総合スコア91

Spring Security

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

Java

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

Spring

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

Spring Boot

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

0グッド

0クリップ

投稿2019/03/23 03:12

ログイン機能をSpring Securityにて実装しようとしています。
ログインフォームから以下のように入力チェックを行い、エラーがないときは認証処理へ飛ばそうとしています。
入力チェックは以下のように正しく行うことができます。
イメージ説明
しかし、正しい情報を入力して認証処理パス/loginへ飛ばしているreturn "forward:" + "/login";の箇所でエラーで落ちてしまいます。(以下画像)
イメージ説明
他の処理へはアクセスできるため何かの設定ミスかと思いますが、手が止まっています。
ご指摘いただけると嬉しいです。
githubへプロジェクトを入れています。

LoginController

1 2/** 3 * 入力チェック 4 * 5 * @param form 6 * @param br 7 * @return 8 */ 9 @PostMapping("/loginForm") 10 public String index(@Validated @ModelAttribute LoginForm form, BindingResult br, 11 RedirectAttributes redirectAttributes) { 12 // 入力チェックエラーがある場合は、元の画面にもどる 13 if (br.hasErrors()) { 14 return "login/login"; 15 } 16 //20190309入力チェックが通った場合は、SecurityConfigで設定した認証処理にフォワードする 17 //20190309Postメソッドでなければいけないのでforwardを使う必要がある 18 return "forward:" + "/login"; 19 } 20

SecurityConfig

1 2package com.example.Todo; 3 4import com.example.Todo.common.security.DefaultAccessDeniedHandler; 5import com.example.Todo.common.security.DefaultAuthenticationEntryPoint; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.context.annotation.Bean; 8import org.springframework.context.annotation.Configuration; 9import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 10import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 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; 15import org.springframework.security.core.userdetails.UserDetailsService; 16import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 17import org.springframework.security.crypto.password.PasswordEncoder; 18import org.springframework.security.web.AuthenticationEntryPoint; 19import org.springframework.security.web.access.AccessDeniedHandler; 20 21import javax.sql.DataSource; 22 23import static com.example.Todo.common.WebConst.*; 24 25@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) 26@Configuration 27@EnableWebSecurity 28public class SecurityConfig extends WebSecurityConfigurerAdapter { 29 30 @Autowired 31 DataSource dataSource; 32 33 @Autowired 34 UserDetailsService userDetailsService; 35 36 @Bean 37 public PasswordEncoder passwordEncoder() { 38 return new BCryptPasswordEncoder(); 39 } 40 41 42 /** 43 * 静的ファイルには認証をかけない 44 * @param web 45 * @throws Exception 46 */ 47 @Override 48 public void configure(WebSecurity web) throws Exception { 49 web.ignoring().antMatchers("/favicon.ico", "/css/**", "/js/**", "/images/**", "/fonts/**", "/shutdown" /* for Demo */); 50 } 51 52 /** 53 * UserDetailsServiceインターフェースを実装した独自の認証レルムを使用する設定 54 * @param auth 55 * @throws Exception 56 */ 57 @Override 58 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 59 auth.userDetailsService(userDetailsService) 60 .passwordEncoder(passwordEncoder()); 61 } 62 63 @Override 64 protected void configure(HttpSecurity http) throws Exception { 65 http.authorizeRequests() 66 .antMatchers("/loginForm").permitAll() 67 .antMatchers("/user/**").permitAll() 68 .antMatchers("/new").permitAll()//test用(ユーザ登録) 69 .antMatchers("/index").permitAll()//test用(ユーザ登録後の遷移画面) 70 .antMatchers("/user/create").permitAll()//test用機能 71 .anyRequest().authenticated() 72 .and() 73 .exceptionHandling() 74 .authenticationEntryPoint(authenticationEntryPoint()) 75 .accessDeniedHandler(accessDeniedHandler()); 76 77 http.formLogin() 78 .loginPage("/loginForm") 79 .loginProcessingUrl("/login") 80 .failureUrl("/login?error") 81 .successForwardUrl("/success") 82 .usernameParameter("email") 83 .passwordParameter("password"); 84 http.logout() 85 .logoutUrl("/logout**") 86 .logoutSuccessUrl("/login"); 87 } 88 89 90 @Bean 91 public AccessDeniedHandler accessDeniedHandler() { 92 return new DefaultAccessDeniedHandler(); 93 } 94 95 @Bean 96 public AuthenticationEntryPoint authenticationEntryPoint() { 97 return new DefaultAuthenticationEntryPoint("/login", LOGIN_TIMEOUT_URL); 98 } 99 100 101 102} 103 104

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

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

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

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

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

guest

回答1

0

ベストアンサー

答え
基本ログイン認証はフィルターで制御されてるのでフォワードでは実施できません

以下アドバイス
認証機能の部分も Spring Security のドキュメントで調べて作成してみてはいかがですか?

投稿2019/03/23 03:52

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問