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

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

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

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

Spring

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

Spring Boot

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

Q&A

解決済

1回答

487閲覧

Springセキュリティにあるログイン機能の動きについて

a0841_1974

総合スコア29

Java

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

Spring

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

Spring Boot

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

0グッド

0クリップ

投稿2018/04/18 09:39

いつもお世話になっております。

Springセキュリティにあるログイン機能を使用した際の動きについて
分からない事があります。
ご存知の方がいらっしゃれば教えて頂けますでしょうか。

【ご質問】
以下のようなconfig、ログイン画面、menu画面、コントローラーがあります。
この状態で、ログイン画面から担当者IDにadmin、パスワードにadminを入力して
ログインボタンを押すと、何故か再度ログイン画面が表示されます。

ログインボタンを押すと、menu画面に遷移させたいのですが、
どのようにすればmenu画面に遷移できるのでしょうか。

(補足)
1.コントローラーのreturn "login/login";、return "login/menu";にbreakpointをはり、
ログインボタンを押した際にどちらに来るかを確認したところ、
return "login/login";に来ました。

2.configのweb.ignoring().antMatchers()に/**を追加して、
ログイン画面から担当者IDにadmin、パスワードにadminを入力して
ログインボタンを押すとmenu画面に遷移できました。

config

@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/static/**","/fonts/**","/jasperreports/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/trans/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .usernameParameter("tantousya_id") .passwordParameter("password") .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("test1").password("test1").roles("USER") .and() .withUser("admin").password("admin").roles("USER","ADMIN"); } }

ログイン画面

<form id="loginForm" method="POST" action="/login/login" > <div class="form-group"> <label for="uname1">担当者ID</label> <input type="text" id="tantousya_id" name="tantousya_id" class="form-control form-control-lg rounded-0" required="required" > </div> <div class="form-group"> <label for="uname1">パスワード</label> <input type="password" id="password" name="password" class="form-control form-control-lg rounded-0" required="required" > </div> <button class="btn btn-success btn-lg float-right" id="btnLogin" type="submit">ログイン</button> </form>

コントローラー

@Controller @Scope("request") @RequestMapping(value="/login") public class RegistContainerLogic { @RequestMapping(method=RequestMethod.GET) public String index(Model model){ return "login/login"; } @RequestMapping(value="/login", method=RequestMethod.POST) public String login(@ModelAttribute("formModel") RegistForm input,Model model){ model.addAttribute("tantousya_id", input.getTantousya_id()); return "login/menu"; } }

menu画面

<!DOCTYPE html> <html lang="ja" xmlns="http://www.thymleaf.org"> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>menu</title> <link rel="stylesheet" th:href="@{/webjars/bootstrap/4.0.0-1/css/bootstrap.min.css}" /> </head> <body> <td th:text="${tantousya_id}"></td> <script type="text/javascript" th:src="@{/webjars/jquery/3.3.1/jquery.min.js}"></script> <script type="text/javascript" th:src="@{/webjars/bootstrap/4.0.0-1/js/bootstrap.min.js}"></script> </body> </html>

RegistForm

public class RegistForm implements Serializable { private static final long serialVersionUID = 1L; private String tantousya_id; private String password; public String getTantousya_id() { return tantousya_id; } public void setTantousya_id(String tantousya_id) { this.tantousya_id = tantousya_id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

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

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

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

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

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

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

guest

回答1

0

自己解決

以下の内容でmenu画面に遷移する事が出来ました。

config

@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { // SpringSecurityの制限を無視してほしい場所の指定 @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/static/**","/fonts/**","/jasperreports/**","/webjars/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated(); http .formLogin() .loginPage("/login") .usernameParameter("tantousya_id") .passwordParameter("password") .successForwardUrl("/menu") .failureForwardUrl("/login_failure") .permitAll() .and() .logout() .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); auth .inMemoryAuthentication() .withUser("user").password(passwordEncoder.encode("password")).roles("USER") .and() .withUser("admin").password(passwordEncoder.encode("password")).roles("ADMIN"); }

ログイン

<form id="loginForm" method="POST" th:action="@{/login}" > <div class="form-group"> <label for="uname1">担当者ID</label> <input type="text" id="tantousya_id" name="tantousya_id" class="form-control form-control-lg rounded-0" required="required" > </div> <div class="form-group"> <label for="uname1">パスワード</label> <input type="password" id="password" name="password" class="form-control form-control-lg rounded-0" required="required" > </div> <button class="btn btn-success btn-lg float-right" id="btnLogin" type="submit">ログイン</button> </form>

コントローラー

@Controller @Scope("request") public class RegistContainerLogic {     @RequestMapping(value="/login", method=RequestMethod.GET)   public String index(Model model){ return "login/login"; } @RequestMapping(value="/menu", method=RequestMethod.POST) public String menu(@ModelAttribute("formModel") RegistForm input,Model model){ model.addAttribute("tantousya_id", input.getTantousya_id()); return "login/menu"; } @RequestMapping(value="/login_failure", method=RequestMethod.POST) public String login_failure(@ModelAttribute("formModel") @Validated RegistForm input,BindingResult result,Model model){ return "login/login"; } }

menu画面,RegistFormの修正はありません。

投稿2018/04/19 14:10

編集2018/04/19 14:12
a0841_1974

総合スコア29

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問