【发布时间】:2016-09-11 11:03:47
【问题描述】:
我在 Spring Boot 中使用 Freemarker 并进行 mvc 单元测试。在我的 freemarker 模板中,我有一个隐藏的 csrf 令牌输入字段,如下所示:
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
那我还有一个mvc单元测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebAppConfiguration
@ActiveProfiles("test")
public class MvcTests {
@Autowired
WebApplicationContext ctx;
MockMvc mvc;
HttpSessionCsrfTokenRepository httpSessionCsrfTokenRepository;
@Before
public void setUp() throws Exception {
mvc = webAppContextSetup(ctx).build();
httpSessionCsrfTokenRepository = new HttpSessionCsrfTokenRepository();
}
@Test
public void testInValidVoucherNumber() throws Exception {
CsrfToken csrfToken = httpSessionCsrfTokenRepository.generateToken(new MockHttpServletRequest());
mvc.perform(post("/voucher/claim")
.sessionAttr("_csrf.parameterName", "_csrf")
.sessionAttr("_csrf.token", csrfToken.getToken())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("code", "ABC")
.param("_csrf", csrfToken.getToken()))
.andExpect(status().isOk())
.andExpect(view().name("claim-voucher"))
.andExpect(content().string(containsString("Code is invalid")));
}
}
当我运行单元测试时,出现以下 freemarker 错误:
testInValidVoucherNumber(MvcTests) Time elapsed: 0.904 sec <<< ERROR!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> _csrf [in template "claim-voucher.ftl" at line 25, column 34]
----
Tip: If the failing expression is known to be legally refer to something that's null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${_csrf.parameterName} [in template "claim-voucher.ftl" at line 25, column 32]
~ Reached through: #nested [in template "layout.ftl" in macro "basic" at line 16, column 9]
这里是控制器:
@RequestMapping(value = "/voucher/claim", method = RequestMethod.POST)
public String checkVoucherCode(@Valid ClaimVoucherForm claimVoucherForm, Model model) {
//Business logic
}
似乎 freemarker 无法访问 csrf parameterName 和 csrf 令牌。有没有办法在不更改控制器的情况下包含 csrf 信息或单元测试有问题?
【问题讨论】:
标签: c# unit-testing spring-boot csrf freemarker