我正在使用带有Rail 5.1 API的rack-cors gem.
根据文档我有以下初始化程序:
配置/初始化/ cors.rb
module Api
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins ['http://localhost:4200','https://app.mydomain.com/']
resource '*',
headers: :any,
:expose => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是,这意味着当部署到生产时,我的api将接受来自任何localhost:4200
来源的请求.
如何将这些设置分开,以便不同的环境可以具有不同的允许来源?
我正在使用rack-cors gem在我的rails应用程序中实现CORS,但我不确定如何为不同的来源定义不同的资源.
我需要这样的东西:
config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'http://localhost:3000'
resource '/api/*', headers: :any, methods: [:get, :post, :options, :put, :delete]
end
allow do
origins 'http://localhost:6000'
resource '*', headers: :any, methods: [:get, :post, :options, :put, :delete]
end
end
Run Code Online (Sandbox Code Playgroud)
所以它允许" http:// localhost:3000 "只访问'/ api/*'并允许' http:// localhost:6000 '访问所有.可能吗?
上面的代码是正确的代码/语法吗?
谢谢.
我正在开发Reactjs
用作前端和Rails5 api only
应用程序作为后端的Web应用程序
这是我发送到服务器的数据 Request payload
------WebKitFormBoundaryCD1o71UpVNpU4v86
Content-Disposition: form-data; name="user[username]"
oeuoeoaeaoe
------WebKitFormBoundaryCD1o71UpVNpU4v86
Content-Disposition: form-data; name="user[profile_image]"; filename="gggg.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryCD1o71UpVNpU4v86--
Run Code Online (Sandbox Code Playgroud)
这是我的控制器
def update_with_image
user = current_user
if user.update_attributes(user_update_params)
# Handle a successful update.
render json: user, status: 200
else
render json: { errors: user.errors }, status: 422
end
end
private
def user_update_params
params.require(:user).permit(:username,:profile_image)
end
Run Code Online (Sandbox Code Playgroud)
所以当我试图将图像上传到Rails服务器时,我遇到了这个错误
ActionController::BadRequest (Invalid request parameters: invalid %-encoding ("user[username]"
oeuoeoaeaoe
------WebKitFormBoundaryCD1o71UpVNpU4v86
Content-Disposition: form-data; name="user[profile_image]"; filename="gggg.jpg"
Content-Type: image/jpeg
????JFIF????@6"??
??F!1AQ "aq?
#2???B?????$3Rb?%Cr?????? ??A!1A"Qaq?2???BR???#b??3rS?$Cs????
??%)):
rack …
Run Code Online (Sandbox Code Playgroud) 我有一个仅限 Rails 5 API 的应用程序,想要在 JSON 请求的响应中发送 cookie。当我使用 ActionDispatch::Cookies 在请求的响应中设置 cookie 时,它不会Set-Cookie
在响应中设置标头。虽然response.set_cookie
确实有效。
我还在应用程序控制器中进行了钩子测试after_action
,我看到Set-Cookie
header 不存在于 response.headers 中,但存在 cookies['name_of_cookie'] 。我还想问的一个问题是这些 cookie(ActionDispatch::Cookies) 何时设置响应头?它发生在机架中吗?
withCredentials
Access-Control-Allow-Credentials: true
和Access-Control-Allow-Origin: <origin (request.headers['origin'])>
存在config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, key: '_namespace_key'
Run Code Online (Sandbox Code Playgroud)
after
Rails 应用程序中的任何回调挂钩中,还是发生在机架中?因为直到after_action
在 applicaton_controller 中标题才不存在。domain
cookie的设置有关系?我还在生产中使用 secure 和 httponly cookie,但在开发中仅使用 httponly。 …我在Rails5上,我想在我的一条路线上允许CORS.以下是我可以在所有路由中使用CORS的方法,但有没有办法只为一个端点列入白名单?
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
Run Code Online (Sandbox Code Playgroud) 我正在为我的 rails 服务器配置 CORS 设置 - 我希望能够在本地运行时测试我的后端,使用本地主机上的前端。
但是,据我所知,CORS 是针对 CSRF(?) 的重要安全机制,因此当应用程序投入生产时,我希望后端只允许实时网站的来源。像这样:
if development:
allow do
origins 'localhost:3000', 'localhost:3001', 'https://my-app.com'
resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
if production:
allow do
origins 'https://my-app.com'
resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
Run Code Online (Sandbox Code Playgroud)
我知道让一个未知用户在本地主机上运行一个恶意应用程序并且不知道他们在做什么是相当困难的,但仍然比我说的抱歉更安全。
我该怎么办?为什么这没有在 Rack::CORS 的 Github 页面上显着地记录?我有一种感觉,我错过了一些东西。请赐教。
我想我的Rails 5 API专用应用程序,现在正在运行http://localhost:3000
,只接受来自我的NodeJS前端应用程序的请求,现在正在运行http://localhost:8888
.
所以我配置/config/initializers/cors.rb
如下:
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins "http://localhost:8888"
resource "*",
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
Run Code Online (Sandbox Code Playgroud)
我写了这个测试:
#/spec/request/cors_request_spec.rb
RSpec.feature "CORS protection", type: :request do
it "should accept a request from a whitelisted domain" do
get "/api/v1/bodies.json", nil, "HTTP_ORIGIN": "http://localhost:8888"
expect(response.status).to eql(200)
end
it "should reject a request from a non-whitelisted domain" do
get "/api/v1/bodies.json", nil, "HTTP_ORIGIN": "https://foreign.domain"
expect(response.status).to eql(406)
end
end
Run Code Online (Sandbox Code Playgroud)
第一个测试按预期传递.但第二个是失败,响应代码为200.为什么?
(顺便说一句,我没有使用406响应代码;只是表示请求不会被执行的代码.)