Rails ActionCable请求来源子域 [英] Rails ActionCable Request Origins Subdomain
问题描述
我正在使用公寓gem构建一个SaaS平台,并在引入ActionCable以实现聊天功能.我已经建立了大多数聊天功能,但是当传输到频道时,我得到以下信息:
I'm building a SaaS platform with the apartment gem and am introducing ActionCable for chat functionality. I have most of the chat functionality built, but when transmitting to the channel I get the following:
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
An unauthorized connection attempt was rejected
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-02-01 08:49:53 -0600
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-02-01 08:49:53 -0600
我在app/channels/connection.rb中使用此代码
I'm using this code in app/channels/connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
tenant = request.subdomain
Apartment::Tenant.switch!(tenant)
logger.add_tags "ActionCable", "User #{current_user.id}", "Tenant #{tenant}"
end
protected
def find_verified_user
if current_user = env['warden'].user
current_user
else
reject_unauthorized_connection
end
end
end
end
理论上,这应该切换承租人,并在正确的子域上传输Actioncable.但是我仍然遇到这些错误.
This in theory should switch the Tenant and get Actioncable transmitting on the right subdomain. But I'm still getting these errors.
我也在使用Devise并以用户身份登录,因此没有理由思考连接被拒绝的原因,因为它正在查找当前用户并验证该用户是基于登录的devise用户监狱长的ENV.
I'm also using Devise and am logged in as a user so there's no reason that I can think of why the connection was rejected since it's looking up the current user and validating that the user is a logged in devise user based off of the warden ENV.
我想知道我是否正在使用子域,是否应该以某种方式配置请求源.
I'm wondering since I have subdomains in play if I should be configuring the request origins in a certain fashion.
要对此进行测试,我在lvh.me本地域上对我正在使用的子域进行了硬编码,如下所示:
To test this I hardcoded one of my subdomains that I'm using on the lvh.me localhost domain as such:
Rails.application.config.action_cable.allowed_request_origins = ['http://acme.lvh.me:3000']
我想知道allowed_request_origins是否与此有关,如果可以,我如何使用正则表达式允许lvh.me:3000 url/domainspace的任何子域?
I was wondering if the allowed_request_origins has something to do with this and if so, how can I use regex to allow for any subdomain to the lvh.me:3000 url/domainspace?
非常感谢您的帮助.
推荐答案
在朋友的帮助下,我们了解到我们需要在建立连接之前切换租户.这是connection.rb
With the help of a friend we learned that we needed to switch tenants prior to establishing the connection. Here is the connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
tenant = request.subdomain
Apartment::Tenant.switch!(tenant)
self.current_user = find_verified_user
logger.add_tags "ActionCable", "User #{current_user.id}", "Tenant #{tenant}"
end
protected
def find_verified_user
if current_user = env['warden'].user
current_user
else
reject_unauthorized_connection
end
end
end
end
要处理development.rb中的正则表达式部分以处理请求的起源,我做到了:
To handle the regex portion in development.rb to handle the request origins I did this:
Rails.application.config.action_cable.allowed_request_origins = ['http:\/\/*.lvh.me:3000*']
到目前为止,它可以正常工作,并且AC正在与子域上的Apartment正常传输.至少在开发中.
So far it's working and AC is transmitting properly with Apartment on subdomains. At least in development.
这篇关于Rails ActionCable请求来源子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!