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

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

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

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

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

7146閲覧

ログイン成功で、遷移元画面に遷移するにはどうすればいいのでしょうか?

yuki1111

総合スコア72

Spring Security

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

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

1グッド

1クリップ

投稿2019/05/28 12:13

今、Webアプリケーションを作成中です。そのWebアプリケーションの各ページからログイン画面に遷移できるのですが、ログインに成功したらトップ画面に遷移してしまいます。
どうしたら、ログイン画面に遷移する前の画面にもどることができるのでしょうか?

Spring Securityの設定

Java

1package com.koikeya.project1.app.config; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.context.annotation.Bean; 5import org.springframework.context.annotation.Configuration; 6import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 7import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 9import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 10import org.springframework.security.core.userdetails.UserDetailsService; 11import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12import org.springframework.security.crypto.password.PasswordEncoder; 13 14/** 15 * Spring Security設定クラス 16 * @author Yuki Koike 17 */ 18@Configuration 19// @EnableWebSecurityを付与して、Spring SecurityのWeb連携機能(CSRF対策など)を有効にする 20@EnableWebSecurity 21public class SecurityConfig extends WebSecurityConfigurerAdapter { 22 23 /** 24 * UserDetailsService 25 */ 26 @Autowired 27 UserDetailsService userDetailsService; 28 29 /** 30 * パスワード符号化用オブジェクトを返す 31 * 32 * @return BCryptPasswordEncoderオブジェクト 33 */ 34 @Bean 35 PasswordEncoder passwordEncoder() { 36 return new BCryptPasswordEncoder(); 37 } 38 39 /* 40 * (非 Javadoc) 41 * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity) 42 */ 43 @Override 44 protected void configure(HttpSecurity httpSecurity) throws Exception { 45 httpSecurity.authorizeRequests().antMatchers("**", "**/**", "/css/**", "/images/**", "/js/**", "/webjars/**") 46 .permitAll().anyRequest().authenticated().and() 47 .formLogin().loginPage("/login") 48 //.defaultSuccessUrl("/index.html") 49 .failureUrl("/login?error=true").permitAll() 50 .and().logout().logoutSuccessUrl("/login").permitAll(); 51 } 52 53 @Override 54 protected void configure(AuthenticationManagerBuilder auth) throws Exception { 55 auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 56 } 57} 58

ログイン成功の設定

Java

1package com.koikeya.project1.app.controller; 2 3import java.io.IOException; 4 5import javax.servlet.ServletException; 6import javax.servlet.http.HttpServletRequest; 7import javax.servlet.http.HttpServletResponse; 8 9import org.springframework.security.core.Authentication; 10import org.springframework.security.web.DefaultRedirectStrategy; 11import org.springframework.security.web.RedirectStrategy; 12import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 13 14/** 15 * AuthenticationSuccessHandler実装クラス 16 */ 17public class AuthenticationSucessHandlerImpl implements AuthenticationSuccessHandler { 18 19 /* 20 * (非 Javadoc) 21 * @see org.springframework.security.web.authentication.AuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication) 22 */ 23 @Override 24 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 25 Authentication authentication) throws IOException, ServletException { 26 27 RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 28 redirectStrategy.sendRedirect(request, response, "/index.html"); 29 } 30} 31
mi_tech_otter👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

普通は、

  1. ログインが必要なページにアクセスしようとするがセッションが切れている
  2. ログイン画面にリダイレクト
  3. ログイン
  4. アクセスしようとしたページに遷移

という流れのなかで、「ログイン画面にリダイレクト」するときのURLに、たとえば?page=/xxx/xxx/xxxのような形で1のパスを渡しておきます。

3のときに一緒に隠しフォームなどでpageを渡すようにしておいて、3のログイン処理後のリダイレクトでそこにとばすようにします。

投稿2019/05/28 22:39

otolab

総合スコア765

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問