gpt4 book ai didi

javascript - 接收未经授权的连接尝试被拒绝 w/ActionCable 检查空用户

转载 作者:行者123 更新时间:2023-11-30 14:45:24 25 4
gpt4 key购买 nike

我试图阻止 ActionCable 持续检查未登录服务器的用户。我删除了测试环境中的所有用户以检查此问题是否会持续存在,但它没有结束的迹象,即使在我向 JS 文件添加条件语句以检查 current_user 的 id 之后也是如此。我怎样才能最终结束 ActionCable 尝试连接不存在或未登录的用户?顺便说一句,我正在使用 JQuery3 和 Rails 5。

控制台日志

Started GET "/cable" for 127.0.0.1 at 2018-02-27 00:11:41 -0500
(1.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-02-27 00:11:42 -0500
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (2.0ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT $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 2018-02-27 00:11:42 -0500
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-02-27 00:11:42 -0500
Started GET "/cable" for 127.0.0.1 at 2018-02-27 00:12:09 -0500

连接.rb

module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user

def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', current_user.username
end

protected
def find_verified_user
verified_user = User.find_by(id: cookies.signed['user.id'])
if verified_user && cookies.signed['user.expires_at'] > Time.now
verified_user
else
reject_unauthorized_connection
end
end
end
end

电缆.js

// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
this.App || (this.App = {});

App.cable = ActionCable.createConsumer();

}).call(this);

聊天室.js

$(document).on('turbolinks:load', function () {
if ($("meta[name='current-user']").length > 0) {
(function () {
App.chatrooms = App.cable.subscriptions.create("ChatroomsChannel", {
connected: function () {
},
disconnected: function () {
},
received: function (data) {
var active_chatroom;
active_chatroom = $("[data-behavior='messages'][data-chatroom-id='" + data.chatroom_id + "']");
if (active_chatroom.length > 0) {
if (document.hidden) {
if ($(".strike").length === 0) {
active_chatroom.append("<div class='strike'><span>Unread Messages</span></div>");
}
if (Notification.permission === "granted") {
new Notification(data.username, {
body: data.body
});
}
} else {
App.last_read.update(data.chatroom_id);
}
return active_chatroom.append("<div class='media message'> <div class='media-body'> <h5 class='mt-0 message-username-pos'>" + data.username + "</h5> <p>" + data.body + "</p></div></div>");
} else {
return $("[data-behavior='chatroom-link'][data-chatroom-id='" + data.chatroom_id + "']").css("font-weight", "bold");
}
},
send_message: function (chatroom_id, message) {
return this.perform("send_message", {
chatroom_id: chatroom_id,
body: message
});
}
});

}).call(this);
}
});

last_read.js

$(document).on('turbolinks:load', function () {
if ($("meta[name='current-user']").length > 0) {
(function () {
App.last_read = App.cable.subscriptions.create("LastReadChannel", {
connected: function () {
},
disconnected: function () {
},
received: function (data) {
},
update: function (chatroom_id) {
return this.perform("update", {
chatroom_id: chatroom_id
});
}
});

}).call(this);
}
});

application.html.erb

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Test App</title>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js' %>
<%= javascript_include_tag 'https://js.stripe.com/v3/' %>
<%= favicon_link_tag 'favicon.ico' %>
<% if user_signed_in? %>
<%= tag :meta, name: 'current-user', data: {id: current_user.id} %>
<% end %>
</head>

<body>
</body>
</html>

warden_hooks.rb

Warden::Manager.after_set_user do |user,auth,opts|
scope = opts[:scope]
auth.cookies.signed["#{scope}.id"] = user.id
auth.cookies.signed["#{scope}.expires_at"] = 30.minutes.from_now
end

Warden::Manager.before_logout do |user, auth, opts|
scope = opts[:scope]
auth.cookies.signed["#{scope}.id"] = nil
auth.cookies.signed["#{scope}.expires_at"] = nil
end

最佳答案

我不确定我是否理解您的问题。然而,连接是在这里发起的:

(function() {
this.App || (this.App = {});

App.cable = ActionCable.createConsumer();

}).call(this);

因此,如果您不想连接,可以在此处添加支票。

关于javascript - 接收未经授权的连接尝试被拒绝 w/ActionCable 检查空用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com