我試圖嘲弄使用下面的代碼POST請求。春天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
更新了相關問題的相關細節。我正在使用spring security 4.1.2 – lives