0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Spring Security その3.5〜defaultSuccessUrl

Posted at

前回まで
Spring Security その1〜ログイン機能の実装
Spring Security その2〜DB認証
Spring Security その2.5〜デフォルトのログアウト画面
Spring Security その3〜ログイン画面のカスタマイズ

前回作成したログイン画面のカスタマイズで少しハマってしまったこと。
「defaultSuccessUrl」の役割

defaultSuccessUrlに/toppage と指定しても、認証後に/toppageに遷移しないことがある。
例えば:認証前に「/secondPage」 にアクセスした場合、一度ログインページに遷移し、認証が完了すると「/secondPage」に遷移する。

? defaultSuccessUrlの説明ではログイン認証後に遷移するURL という説明が多かった。
なぜ、toppageに遷移しないんだ?

公式の説明を読むと、答えは簡単だった

ユーザーが認証前にセキュリティで保護されたページにアクセスしなかった場合、認証後にユーザーがリダイレクトされる場所を指定します。

上記の例では認証前にセキュリティで保護されたページ(secondPage)にアクセスをしたので、認証後はそのURLに遷移となった。

これはspringSecurityでは直前にアクセスしていたURLを「SavedRequest」に保存され、認証後にはこの保存したURLにリダイレクトとなるからだ。

では、絶対にdefaultSuccessUrlに指定したページに遷移させたい場合はどうするのか。
defaultSuccessUrlにパラメータ「alwaysUse」にTrueを設定するだけ。

実際にはこんな感じ

     .defaultSuccessUrl("/toppage",true)  //②

※コードの全体像はSpring Security その3〜ログイン画面のカスタマイズを参照してください

これで、ログイン認証後は必ず/toppageへ遷移することとなる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?