处理所有 Rails 请求共有的用户 ID 哈希值的最佳方式

发布于 2024-07-10 12:56:04 字数 188 浏览 9 评论 0原文

每个客户端都由一个哈希值来标识,该哈希值随每个请求一起传递到服务器。 在这种情况下,处理跟踪用户会话的最佳方法是什么?

我对用户帐户等使用restful_authentication。预计很大一部分请求是在没有用户帐户的情况下发起的,而只是唯一的哈希值。

我对处理会话的方式的理解是有限的,所以请记住这一点。 :)

Each client is identified by a hash, passed along with every request to the server. What's the best way to handle tracking a users session in this case?

I'm using restful_authentication for user accounts etc. A large percentage of requests are expected to originate without a user account but just the unique hash.

My understanding of the way handles sessions is limited so please bear that in mind. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凶凌 2024-07-17 12:56:04

在 URL 中使用此哈希意味着您没有 Rails 内置会话。 会话的目的是在请求之间提供某种状态感。 您已经提供了此状态,看到您正在传递此哈希,因此我认为您可以删除restful_authentication插件并执行类似以下操作:然后

class ApplicationController < ActionController::Base
  def require_login
    if params[:access_key]
      @current_user = User.find_by_access_key(params[:access_key]) || restrict_access
    else
      restrict_access
    end
  end

  def restrict_access
    flash[:error] = "You have to log in to access that."
    redirect_to root_path
  end
end

,在控制器中执行 before_filter :require_login需要登录才能访问。

Using this hash in the URL means that you don't have Rails built-in session. The point of the session is to provide some sense of state between requests. You're already providing this state, seeing that you are passing this hash, so in my opinion you could remove the restful_authentication plugin and do something like this instead:

class ApplicationController < ActionController::Base
  def require_login
    if params[:access_key]
      @current_user = User.find_by_access_key(params[:access_key]) || restrict_access
    else
      restrict_access
    end
  end

  def restrict_access
    flash[:error] = "You have to log in to access that."
    redirect_to root_path
  end
end

Then, do a before_filter :require_login in the controllers where login is required for access.

像极了他 2024-07-17 12:56:04

取决于您想要执行的操作,但 session 哈希可能会提供您想要的内容。 会话将自身存储在某个地方(加密的 cookie、数据库或服务器上的文件),并在 cookie 中向客户端发送唯一标识符(类似于您的“哈希”)。 在后续请求中,将读取 cookie,并将相应用户的会话数据恢复到 session 哈希中。

session[:user] = currently_logged_in_user.id
# ... next request ...
session[:user] # returns the currently logged in user's id

Depends on what you're trying to do, but the session hash might provide what you want. The session stores itself somewhere (either an encrypted cookie, the database, or a file on the server), and sends a unique identifier to the client (similar to your "hash") in a cookie. On subsequent requests, the cookie is read and the corresponding user's session data is restored to the session hash.

session[:user] = currently_logged_in_user.id
# ... next request ...
session[:user] # returns the currently logged in user's id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文