【发布时间】:2017-11-06 11:18:26
【问题描述】:
我得到 ActionController::InvalidAuthenticityToken 保存一个简单的资源..
存在 CSRF 元标记:
<meta name="csrf-token" content="Z4fDRsIqCp4cvpCw1Dp6kN7z0uPNF5otp611C80YhUg/oB+AcUy+dz2b5qKyoMMo48LdEvbF3dcBn2d0GqeR+g==" />
以及表单的令牌字段:
<input type="hidden" name="authenticity_token" value="bIMZ5cndd/CTgOwjC3quOp02WlvWdDmhdNQCyKb920HIZz2Y8vvNDlTa7d4PKaJ5pUY6QkHKYCqiHikc2rFsyQ==" />
会话 ID 不会在请求之间保持不变,但我知道这种行为是在发生 CSRF 错误时有意为之。
表格代码:
<%= form_for document do |f| %>
<% if document.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(document.errors.count, "error") %> prohibited this document from being saved:</h2>
<ul>
<% document.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
控制器中的相关操作:
def new
@document = Document.new
end
def create
@document = Document.new(document_params)
respond_to do |format|
if @document.save
format.html { redirect_to @document, notice: 'Document was successfully created.' }
format.json { render :show, status: :created, location: @document }
else
format.html { render :new }
format.json { render json: @document.errors, status: :unprocessable_entity }
end
end
end
不知道如何解决这个问题,出于显而易见的原因,我不愿意关闭 CSRF 保护。
编辑:我正在通过一个 nginx 容器作为代理,也许我的配置不正确?这是配置:
upstream backend {
server service:3000;
}
server {
listen 80 default_server;
error_page 500 502 503 504 /500.html;
error_log /dev/stdout info;
access_log /dev/stdout;
location / {
proxy_pass http://backend;
}
}
【问题讨论】:
-
你在用设计吗?
-
不,还没有添加任何额外的宝石
-
用可能相关的信息更新了我的问题
-
你能告诉我们之前的动作链吗,在 Rails 5 中
protect_from_forgery默认为prepend false这意味着它不一定在依赖于真实性的之前的动作之前运行作为authenticate_user!。无论如何,请尝试将prepend: true添加到您的protect_from_forgery通话中。 -
Prepend: true 没有区别。
标签: ruby-on-rails ruby-on-rails-5 csrf csrf-protection