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

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

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

Vuexは、Vue.js アプリケーションのための状態管理ライブラリです。アプリケーション内で使用するコンポーネントのための集中データストアを提供。コンポーネント同士でデータをやり取りし、処理のフローを一貫させたり、データの見通しを良くすることができます。

MongoDB

MongoDBはオープンソースのドキュメント指向データベースの1つです。高性能で、多くのリトルエンディアンシステムを利用することができます。

Spring

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

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

Q&A

解決済

1回答

3292閲覧

Spring Security ログイン成功リダイレクト うまくいかない

ransuS_T

総合スコア106

Vuex

Vuexは、Vue.js アプリケーションのための状態管理ライブラリです。アプリケーション内で使用するコンポーネントのための集中データストアを提供。コンポーネント同士でデータをやり取りし、処理のフローを一貫させたり、データの見通しを良くすることができます。

MongoDB

MongoDBはオープンソースのドキュメント指向データベースの1つです。高性能で、多くのリトルエンディアンシステムを利用することができます。

Spring

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

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

0グッド

0クリップ

投稿2021/01/07 11:33

編集2021/01/09 04:27

ログイン画面で認証成功時にmainページへリダイレクトすることを考えていますが
今は以下に記載する動作をしてしまいます。

ログイン処理をなげてユーザー認証がうまくいきリダイレクト先もresponse headerに入っているのを
確認できていますが実際の画面表示がログイン画面のままで原因がいまいちわかりません。
リダイレクト先のリクエストも開発者ツールのネットワークを見る限り
mainページへ遷移するリクエストが投げられておりステータスコードが200で返ってきています。

(都度トークンは生成されるので問題ないと思いますが一応マーキングさせていただきます。)
イメージ説明

以下に原因となりそうなSpring Securityの設定部分と成功時のハンドルを記載いたします。

セキュリティ設定部分

java

1package com.ransu.lastperiodweb.webconfig; 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.builders.WebSecurity; 9import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 10import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 11import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12import org.springframework.security.crypto.password.PasswordEncoder; 13import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 14import org.springframework.security.web.csrf.CookieCsrfTokenRepository; 15 16import com.ransu.lastperiodcommon.service.imp.LoginServiceImp; 17import com.ransu.lastperiodweb.security.handler.SuccessHandler; 18 19 20@Configuration 21@EnableWebSecurity 22public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 23 24 @Autowired 25 LoginServiceImp loginService; 26 27 @Override 28 public void configure(WebSecurity web) throws Exception { 29 web.ignoring().antMatchers("/css/**", "/js/**", "/webjars/**"); 30 } 31 32 @Override 33 protected void configure(HttpSecurity http) throws Exception { 34 35 http.authorizeRequests() 36 // ログインとアカウント作成ページを認証不要に 37 .antMatchers("/loginPage").permitAll() 38 .antMatchers("/createAccountPage").permitAll() 39 .antMatchers("/createAccount").permitAll() 40 41 // ユニットの一覧検索ページを認証不要に 42 .antMatchers("/mainPage").permitAll() 43 .antMatchers("/unitListPage").permitAll() 44 .antMatchers("/getUnitDataList").permitAll() 45 46 // その他はログインが必要 47 .anyRequest().authenticated() 48 49 .and() 50 // トークンをcookieで持つように設定 51 .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) 52 53 .and() 54 .formLogin() 55 // ログイン処理をするURL 56 .loginProcessingUrl("/login") 57 // ログイン画面のURL 58 .loginPage("/loginPage") 59 .usernameParameter("mail") 60 .passwordParameter("password") 61 .successHandler(appAuthenticationSuccessHandler()); 62 } 63 64 @Bean 65 public AuthenticationSuccessHandler appAuthenticationSuccessHandler() { 66 return new SuccessHandler(); 67 } 68 69 @Bean 70 public PasswordEncoder passwordEncoder() { 71 return new BCryptPasswordEncoder(); 72 } 73 74 @Autowired 75 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{ 76 auth.userDetailsService(loginService) 77 .passwordEncoder(passwordEncoder()); 78 } 79} 80

認証成功時のハンドラ

java

1package com.ransu.lastperiodweb.security.handler; 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.authentication.SimpleUrlAuthenticationSuccessHandler; 11import org.springframework.stereotype.Component; 12 13@Component 14public class SuccessHandler extends SimpleUrlAuthenticationSuccessHandler{ 15 16 /** 17 * 認証成功時のハンドラ 18 */ 19 @Override 20 protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 21 response.sendRedirect(request.getContextPath() + "/mainPage"); 22 } 23} 24

リダイレクト先に設定している mainPage コントローラー部分

java

1package com.ransu.lastperiodweb.controller.page; 2 3import java.util.ArrayList; 4import java.util.List; 5 6import org.bson.types.ObjectId; 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Controller; 9import org.springframework.web.bind.annotation.RequestMapping; 10import org.springframework.web.bind.annotation.RequestMethod; 11import org.springframework.web.bind.annotation.RequestParam; 12import org.springframework.web.servlet.ModelAndView; 13 14import com.ransu.lastperiodcommon.dto.AbilityDTO; 15import com.ransu.lastperiodcommon.dto.AttributeDTO; 16import com.ransu.lastperiodcommon.dto.BoardDTO; 17import com.ransu.lastperiodcommon.dto.ConditionDTO; 18import com.ransu.lastperiodcommon.dto.RealmDTO; 19import com.ransu.lastperiodcommon.dto.SexDTO; 20import com.ransu.lastperiodcommon.dto.StatusDTO; 21import com.ransu.lastperiodcommon.dto.TypeDTO; 22import com.ransu.lastperiodcommon.dto.UnitDTO; 23import com.ransu.lastperiodcommon.entity.AbilityEntity; 24import com.ransu.lastperiodcommon.entity.AttributeEntity; 25import com.ransu.lastperiodcommon.entity.BoardEntity; 26import com.ransu.lastperiodcommon.entity.ConditionEntity; 27import com.ransu.lastperiodcommon.entity.RealmEntity; 28import com.ransu.lastperiodcommon.entity.SexEntity; 29import com.ransu.lastperiodcommon.entity.StatusEntity; 30import com.ransu.lastperiodcommon.entity.TypeEntity; 31import com.ransu.lastperiodcommon.entity.UnitEntity; 32import com.ransu.lastperiodcommon.service.AbilityService; 33import com.ransu.lastperiodcommon.service.AttributeService; 34import com.ransu.lastperiodcommon.service.BoardService; 35import com.ransu.lastperiodcommon.service.ConditionService; 36import com.ransu.lastperiodcommon.service.RealmService; 37import com.ransu.lastperiodcommon.service.SexService; 38import com.ransu.lastperiodcommon.service.StatusService; 39import com.ransu.lastperiodcommon.service.TypeService; 40import com.ransu.lastperiodcommon.service.UnitService; 41 42@Controller 43public class PageController { 44 45 @Autowired 46 private UnitService unitService; 47 48 @Autowired 49 private SexService sexService; 50 51 @Autowired 52 private RealmService realmService; 53 54 @Autowired 55 private TypeService typeService; 56 57 @Autowired 58 private StatusService statusService; 59 60 @Autowired 61 private AttributeService attributeService; 62 63 @Autowired 64 private AbilityService abilityService; 65 66 @Autowired 67 private BoardService boardService; 68 69 @Autowired 70 private ConditionService conditionService; 71 72 @RequestMapping(value="/loginPage", method=RequestMethod.GET) 73 public ModelAndView loginDisp(ModelAndView mav) { 74 mav.setViewName("login"); 75 return mav; 76 } 77 78 @RequestMapping(value="/mainPage", method=RequestMethod.GET) 79 public ModelAndView mainDisp(ModelAndView mav) { 80 mav.setViewName("main"); 81 return mav; 82 } 83}

ログイン画面

html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3 <head th:replace="common/header :: meta_header(${title},~{::link},~{::script})"> 4 </head> 5 <body> 6 <div id="content"> 7 8 <h1>{{label.title}}</h1> 9 10 <el-form v-model="loginForm" label-width="200px"> 11 12 <el-form-item :label="label.mail" prop="mail"> 13 <el-input v-model="loginForm.mail"></el-input> 14 </el-form-item> 15 16 <el-form-item :label="label.password" prop="password"> 17 <el-input v-model="loginForm.password"></el-input> 18 </el-form-item> 19 20 </el-form> 21 <el-button @click="login">{{label.loginBtn}}</el-button> 22 <a th:href="@{/createAccountPage}">{{label.accountCreate}}</a> 23 </div> 24 <script type="application/javascript" th:inline="javascript"> 25 /*<![CDATA[*/ 26 new Vue({ 27 el:'#content', 28 data: { 29 loginForm: {}, 30 label: { 31 title: 'ログイン画面', 32 mail: 'メールアドレス', 33 password: 'パスワード', 34 loginBtn: 'ログイン', 35 accountCreate: 'アカウントの新規作成' 36 } 37 }, 38 methods: { 39 login() { 40 41 set_csrftoken(); 42 43 let _this = this; 44 $.ajax({ 45 type: 'POST', 46 url: 'login', 47 data: this.loginForm, 48 dataType: 'json', 49 }).then(function(result) { 50 51 }, function () { 52 _this.$message({ 53 type: 'error', 54 message: '画面エラー' 55 }) 56 }) 57 } 58 } 59 }) 60 /*]]>*/ 61 </script> 62 </body> 63</html>

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

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

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

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

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

guest

回答1

0

自己解決

通信にajax通信を使用しており、リダイレクトするなら
非同期通信は使うな!という結論になりました。

お騒がせしてすみません。

投稿2021/01/10 07:14

ransuS_T

総合スコア106

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問