私は自分のWebサイトにログインするために、スプリングセキュリティ4.1.3でWebSecurityConfigurerAdapter
を拡張しました。ここでは私のフォームは、私が実際に私のコードで/login
にマッピングされたPOST
コントローラ方法、ちょうどWebSecurityConfigurerAdapter
を持っていないこのSpring WebSecurityConfigurerAdapterログインが数回失敗してから動作します。
<form id="loginform" role="form" th:action="@{/login}"
method="post">
のように投稿するthymeleafアクションを使用しています
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setDefaultTargetUrl("/index");
http
.authorizeRequests()
.antMatchers("/register", "/registerattempt", "/registeractivate").permitAll()
.antMatchers("/assets/**", "/images/**", "/favicon**", "/min/**").permitAll()
.anyRequest().hasRole("USER")
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.permitAll()
.successHandler(successHandler)
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.logoutSuccessUrl("/login")
.and()
.exceptionHandling()
.accessDeniedHandler(new CustomAccessDeniedHandler());
}
私のconfigureメソッドです。私がログインしようとすると、ログインページを最初に2〜3回リロードし、ログインボタンをクリックすると、/login
POST
はネットワークログに302のステータスコードを持ち、さらに/login
の200 GETになります。その後、3回目または4回目には動作しますが、まだ/login
POST
の302がありますが、GET
私のindex.html
になります。
これは、私がしばらくのうちにログインしているときにのみ発生します。ログインしてログアウトしてもログインが正常に機能します。私は私のセッションを保存するためにredisを使用しています。ログインに何度か試行がかかるのはなぜですか?
私はレスポンスヘッダに見る唯一の違いは、それが
更新 デバッグログがある動作しない場合
それが動作Location:http://localhost:8090/index
と
Location:http://localhost:8090/login
です非常に冗長ですが、失敗したときにログに表示されますが、動作していない場合は
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.csrf.CsrfFilter - Invalid CSRF token found for http://localhost:8090/login
それは
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - No HttpSession currently exists
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: null. A new one will be created.
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.FilterChainProxy - /login at position 3 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter'
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.FilterChainProxy - /login at position 4 of 12 in additional filter chain; firing Filter: 'CsrfFilter'
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.csrf.CsrfFilter - Invalid CSRF token found for http://localhost:8090/login
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.header.writers.HstsHeaderWriter - Not injecting HSTS header since it did not match the requestMatcher org.springframework.se[email protected]1f8411e9
13 Jan 2017 20:20:58.923 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
13 Jan 2017 20:20:58.931 [http-nio-8090-exec-8] DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
とするとき、それはあなたのhttp(HttpSecurity)インスタンスへのログイン処理-URLを追加すること
13 Jan 2017 20:25:44.244 [http-nio-8090-exec-5] DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - HttpSession returned null object for SPRING_SECURITY_CONTEXT
13 Jan 2017 20:25:44.244 [http-nio-8090-exec-5] DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: org.springframework.session.web.http.SessionRepositoryFilter[email protected] A new one will be created.
13 Jan 2017 20:25:44.244 [http-nio-8090-exec-5] DEBUG org.springframework.security.web.FilterChainProxy - /login at position 3 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter'
13 Jan 2017 20:25:44.244 [http-nio-8090-exec-5] DEBUG org.springframework.security.web.FilterChainProxy - /login at position 4 of 12 in additional filter chain; firing Filter: 'CsrfFilter'
13 Jan 2017 20:25:44.244 [http-nio-8090-exec-5] DEBUG org.springframework.security.web.FilterChainProxy - /login at position 5 of 12 in additional filter chain; firing Filter: 'LogoutFilter'
ブラウザやサーバーのログに何も表示されません 変化する。私は何を探していますか? – gary69
successHandler.setDefaultTargetUrl( "/ index");を削除してください。 – Boldbayar
successHandlerオブジェクトを削除してdefaultSuccessUrl( "/ index")を代わりに使用すると、まだ発生します – gary69