如何使用 csrf 令牌正确扩展我的表单?

How can I extend my form with csrf token properly?

我用的是Play2!斯卡拉 2.3.8。 我想为我的发件人添加一个默认的 csrfToken,但我知道怎么做。我通过这个例子试过了 https://www.playframework.com/documentation/2.3.8/ScalaCsrf

我创建了一个自定义全局对象并使用 :

扩展了我的表单
@helper.form(action = routes.Books.submitBook) {
@helper.CSRF.formField

但是我在发送表格后收到了这个错误:

could not find implicit value for parameter token: play.filters.csrf.CSRF.Token

我知道CSRF.formField需要一个参数,但是例子中没有包含。

文档指出 following

The form helper methods all require an implicit token or request to be available in scope. This will typically be provided by adding an implicit RequestHeader parameter to your template, if it doesn’t have one already.

为此,您必须将模板的参数列表更改为如下所示:

@(<your parameters>)(implicit request: RequestHeader)