Rails3.2默认在控制器中启用了 protectfromforgery,它可以防止CSRF,而在application.html.erb这个布局模板中,包含一句 <%= csrfmetatags %>,它会在内生成一条
<meta content="authenticity_token" name="csrf-param"> <meta content="OuKzjaC0nrbmz69XERKtOmuDmJFAxc3mz+UBTBpRVgg=" name="csrf-token">
当使用Rails内置的formfor等表单方法时,它可以自动生成一个包含authenticitytoken值的隐藏域。通过与后台的token比对,完成验证。
如果是自定义的form,则可以通过
<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>"/>
,如果是自定义ajax,则需要在参数中添加authenticity_token参数。