【发布时间】:2022-01-15 19:42:14
【问题描述】:
我有一种情况需要在自定义标头中发送会话 ID,因为 safari 不允许从 iframe 发送 cookie。
但是现在,我无法从机架中间件设置 cookie。
status, headers, body = @app.call(env)
session_id = headers['X-SESSION-ID'] || headers['HTTP_X_SESSION_ID'] || headers['x-session-id'] || ''
if !headers['Cookie'].present? && session_id.present?
headers['Cookie'] = {
'_session_id': {
value: session_id,
path: '/',
httpOnly: true
}
}
end
或者有没有办法手动设置会话 id 而不必从 cookie 中获取它?
更新 1:
修改机架请求文件确实有效。但是,我不知道如何继续这个。如果没有任何效果,我可能会在所有服务器中手动更新此文件。
def cookies
hash = @env["rack.request.cookie_hash"] ||= {}
string = @env["HTTP_COOKIE"] || "_session_id=#{@env["HTTP_X_SESSION_ID"]}"
return hash if string == @env["rack.request.cookie_string"]
hash.clear
# According to RFC 2109:
# If multiple cookies satisfy the criteria above, they are ordered in
# the Cookie header such that those with more specific Path attributes
# precede those with less specific. Ordering with respect to other
# attributes (e.g., Domain) is unspecified.
cookies = Utils.parse_query(string, ';,') { |s| Rack::Utils.unescape(s) rescue s }
cookies.each { |k,v| hash[k] = Array === v ? v.first : v }
@env["rack.request.cookie_string"] = string
hash
end
看起来像上面修改cookie方法不起作用。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 session-cookies