私は以下のコードを使って投稿リクエストを模擬しようとしています。Spring MockMvcのリダイレクトが機能しない
私は春のセキュリティのログイン要求をテストしています。
MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).addFilter(springSecurityFilterChain)
.apply(springSecurity())
.build();
MvcResult mvcResult = mvc.perform(post("/j_spring_security_check?api=true").param("userName", USERNAME)
.param("password", PASSWORD)).andReturn();
コードは正常に動作し、ログインに成功した後、別のコントローラにリダイレクトしています。
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws IOException,
ServletException {
RequestDispatcher rd = request.getRequestDispatcher("/myURL");
rd.forward(request, response);
}
私がテストを実行すると、以下のログが取得されます。リダイレクトは発生せず、/ myURLにマップされたコントローラは呼び出されません。
11:59:55.839 [main] DEBUG o.s.mock.web.MockRequestDispatcher - MockRequestDispatcher: forwarding to [/myURL]
11:59:55.841 [main] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - The HttpSession is currently null, and the HttpSessionSecurityContextRepository is prohibited from creating an HttpSession (because the allowSessionCreation property is false) - SecurityContext thus not stored for next request
11:59:55.841 [main] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
エラーは報告されていません。
何か不足していますか? MockMvcの使用中にリダイレクトされませんか?
MockMvcインスタンスをどのように構築しますか?どのバージョンのSpring Security? – holmis83
は、関連する詳細で質問を更新しました。私は春のセキュリティを使用しています4.1.2 – lives