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

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

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

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

Spring Boot

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

Q&A

0回答

625閲覧

Spring BootでPOSTメソッドでアクセスするとログインページに飛ばされてしまう

mizuti

総合スコア10

Java

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

Spring Boot

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

0グッド

1クリップ

投稿2018/09/16 04:06

前提・実現したいこと

SpringBootでWebアプリケーションを制作しています。
ログインパスワードの変更処理を実装し、試してみたところ、[パスワード変更]ボタンを押すとログインページにリダイレクトされてしまいます。
この状態になったら最後、ログインしてもログインページから抜け出せなくなってしまいます。
まずはどうしたら、ログインページにリダイレクトされなくなるか教えてください。

(ログインページからのリダイレクトの件は別の問題もあるので別途質問します。また、暫定実装なので、パスワードチェック処理が甘いとか、そういうご指摘はご容赦ください)

発生している問題

mvn spring-boot runを実行

ログインして/passwordページを表示する

パスワードを入力して[パスワード変更]を押す

ログインページに飛ばされ、以後正しいユーザー名・パスワードを入力してもログインページから抜け出せなくなる

該当のソースコード

  • ユーザー名・パスワードは別途登録済み
  • スマートフォンからのアクセスは専用ページを表示するコードになっていますが、未実装です
  • パスワードチェックは最低限の実装のみです
  • package, import宣言は省略しています

PasswordController.java:

Java

1@Controller 2public class PasswordController 3{ 4 public static final String PAGE = "/password"; 5 private static final String HTML = "password"; 6 private static final String HTML_SP = "password_sp"; 7 8 private static final String SUCHTML = "passwordchanged"; 9 private static final String SUCHTML_SP = "passwordchanged_sp"; 10 11 @Autowired 12 private UserRepository ur; 13 14 @Autowired 15 PasswordEncoder passwordEncoder; 16 17 @Bean 18 PasswordEncoder passwordEncoder() { 19 return new BCryptPasswordEncoder(); 20 } 21 22 private static final Logger logger = LoggerFactory.getLogger(PasswordController.class); 23 24 @RequestMapping(value = PasswordController.PAGE, method=RequestMethod.GET) 25 public String password( 26 Model model, 27 @AuthenticationPrincipal UserEntity entity, 28 @RequestHeader("User-Agent") String useragent 29 ) 30 { 31 if (entity == null) 32 { 33 logger.info("GET:entity=NULL"); 34 return "redirect:login"; 35 } 36 logger.info("GET:entity="+entity.toString()); 37 38 if (Util.isMobile(useragent)) 39 { 40 return PasswordController.HTML_SP; 41 } 42 else 43 { 44 return PasswordController.HTML; 45 } 46 } 47 48 @RequestMapping(value = PasswordController.PAGE, method=RequestMethod.POST) 49 public String passwordChange( 50 Model model, 51 @AuthenticationPrincipal UserEntity entity, 52 @RequestHeader("User-Agent") String useragent, 53 @RequestParam("password0") String password0, 54 @RequestParam("password1") String password1, 55 @RequestParam("password2") String password2 56 ) 57 { 58 if (entity == null) 59 { 60 logger.debug("POST:entity=NULL"); 61 } 62 logger.debug("POST:entity="+entity.toString()); 63 64 logger.debug("password0 check"); 65 if (!password0.equals(entity.getPassword())) 66 { 67 logger.debug("password0 unmatch."); 68 throw new PasswordNotMatchException("現在のパスワードが一致しません"); 69 } 70 71 logger.debug("password1,2 check"); 72 if (!password1.equals(password2)) 73 { 74 logger.debug("password1,2 unmatch."); 75 throw new PasswordNotMatchException("新しいパスワードが一致しません"); 76 } 77 78 logger.debug("password updating..."); 79 entity.setPassword(passwordEncoder.encode(password1)); 80 ur.save(entity); 81 ur.flush(); 82 logger.debug("password updated!"); 83 84 return SUCHTML; 85 } 86}

password.html

html

1<!DOCTYPE HTML> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <link rel="stylesheet" type="text/css" href="text.css" /> 5 <meta Charset="utf-8" /> 6 <title>パスワード変更</title> 7</head> 8 <body> 9 <h1>パスワード変更</h1> 10 <form method="POST" th:action="@{'/password'}"> 11 <table> 12 <tr><td>現在のパスワード:</td><td><input type="password" name="password0" /></td></tr> 13 <tr><td>新しいパスワード:</td><td><input type="password" name="password1" /></td></tr> 14 <tr><td>新しいパスワード(確認用):</td><td><input type="password" name="password2" /></td></tr> 15 <tr><td colspan="2"> 16 <input type="submit" name="change" value="パスワード変更" /> 17 </td></tr> 18 </table> 19 </form> 20 </body> 21</html>

WebSecutiryConfig.java:

java

1@Configuration 2@ComponentScan 3@EnableWebSecurity 4public class WebSecurityConfig extends WebSecurityConfigurerAdapter 5{ 6 private UserDetailsService userDetailsService; 7 8 @Override 9 protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) 10 throws Exception 11 { 12 authenticationManagerBuilder 13 .userDetailsService(this.userDetailsService) 14 .passwordEncoder(new BCryptPasswordEncoder()); 15 16 } 17 18 @Override 19 protected void configure(HttpSecurity http) throws Exception { 20 // 認可の設定 21 http.authorizeRequests() 22 .antMatchers("/api/**", "/text.css", "/contrib/**").permitAll() // API,css,contribは全ユーザーアクセス許可 23 .anyRequest().authenticated() // それ以外は全て認証無しの場合アクセス不許可 24 .and() 25 .rememberMe() 26 .rememberMeParameter("keepsession") 27 .useSecureCookie(false) 28 .tokenValiditySeconds(7 * 24 * 60 * 60) 29 .tokenRepository(new InMemoryTokenRepositoryImpl());; 30 http.csrf().disable(); 31 // ログイン設定 32 http.formLogin() 33 .loginProcessingUrl("/auth") // 認証処理のパス 34 .loginPage(LoginController.PAGE) // ログインフォームのパス 35 // .failureHandler(new SampleAuthenticationFailureHandler()) // 認証失敗時に呼ばれるハンドラクラス 36 .defaultSuccessUrl(IndexController.PAGE) // 認証成功時の遷移先 37 .usernameParameter("username").passwordParameter("password") // ユーザー名、パスワードのパラメータ名 38 .permitAll(); 39 40 // ログアウト設定 41 http.logout() 42 .logoutRequestMatcher(new AntPathRequestMatcher("/logout**")) // ログアウト処理のパス 43 .logoutSuccessUrl("/"); // ログアウト完了時のパス 44 45 } 46 47 @Autowired 48 public void setUserDetailsService(UserDetailsService userDetailsService) 49 { 50 this.userDetailsService = userDetailsService; 51 } 52}

試したこと

Safari, Chromeの両方で試して同じ現象が出ています。
また、Chromeのデベロッパーツールで確認したところ、/passwordにPOSTリクエストのレスポンスとして、302コードおよびLocation:/loginへが返ってきていました。
また、コード中にログ表示を埋め込んでいますが、POST処理でのログが1行も表示されていないことから、SpringBoot側でloginページにリダイレクトしていると考えています。

localhostで閉じたテストなので、httpsではなくhttpで試しています。

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

macOS: 10.13.6
Java: 1.8.0_131
Maven: 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5)
MySQL: Ver 14.14 Distrib 5.6.25

実行時はmvn spring-boot:runでテストしています。

以下pom.xmlから抜粋

xml

1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <!-- 割愛 --> 3 4 <!-- Spring Boot の利用を宣言し、バージョンを指定【追加】 --> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>1.5.10.RELEASE</version> 9 </parent> 10 <dependencies> 11 <!-- Spring Boot の Web アプリケーションライブラリの利用を指定【追加】 --> 12 <dependency> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-web</artifactId> 15 </dependency> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-security</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-jpa</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-thymeleaf</artifactId> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-devtools</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.apache.commons</groupId> 34 <artifactId>commons-lang3</artifactId> 35 <version>3.5</version> 36 </dependency> 37 <!-- SpringBoot warファイル生成用 --> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-tomcat</artifactId> 41 <scope>provided</scope> 42 </dependency> 43 <!-- 割愛 --> 44 </dependencies> 45 <build> 46 <plugins> 47 <!-- Spring Boot の ビルド用 Maven プラグイン【追加】 --> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 </plugin> 52 </plugins> 53 </build> 54 55 <profiles> 56 <!-- jar出力用プロファイル --> 57 <profile> 58 <id>create-jar</id> 59 <activation> 60 <activeByDefault>true</activeByDefault> 61 </activation> 62 <properties> 63 <packaging.type>jar</packaging.type> 64 </properties> 65 <build> 66 </build> 67 </profile> 68 <!-- war出力用プロファイル --> 69 <profile> 70 <id>create-war</id> 71 <properties> 72 <packaging.type>war</packaging.type> 73 </properties> 74 </profile> 75 </profiles> 76 <properties> 77 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 78 <java.version>1.8</java.version> 79 </properties> 80</project>

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問