【发布时间】:2014-10-26 19:36:33
【问题描述】:
我正在尝试为启用 Spring Web 安全的应用程序创建自定义登录屏幕,但我不知道如何将 csrf 令牌传递给速度(不,我目前无法使用 JSP)。
模型看起来像这样:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username or password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
速度模板的相关部分看起来像(取自一个jsp示例并进行了修改):
<form name='loginForm' action="/login" method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='username' value=''></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
当然,${_csrf.parameterName} 和 ${_csrf.token} 变量是空的,所以这只有在我禁用 csrf 保护时才有效。所以我的主要问题是:如何将它们填充到模型中(或其他任何地方)?
【问题讨论】: