Rack::Auth::基本销毁会话(注销)

发布于 2024-10-21 18:06:01 字数 69 浏览 0 评论 0原文

我使用 Rack::Auth::Basic 进行授权,它有效,但我需要能够销毁用户会话并让他重新登录或注销。它是如何制作的?

I made authorization using Rack::Auth::Basic, it works, but I need to be able to destroy user session and let him re-login or log out. How it can be made?

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

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

发布评论

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

评论(2

逐鹿 2024-10-28 18:06:01

您好,抱歉这么晚才回复您,我刚刚看到您的帖子,也许它仍然可以帮助您:
此代码取自 Sunstone 应用程序,该应用程序是 OpenNebula 的一部分
http://opennebula.org/

use Rack::Session::Pool

def authorized?
    session[:ip] && session[:ip]==request.ip ? true : false
end

def build_session
    auth = Rack::Auth::Basic::Request.new(request.env)
    if auth.provided? && auth.basic? && auth.credentials
        user = auth.credentials[0]
        sha1_pass = Digest::SHA1.hexdigest(auth.credentials[1])

        rc = SunstoneServer.authorize(user, sha1_pass)
        if rc[1]
            session[:user]     = user
            session[:user_id]  = rc[1]
            session[:password] = sha1_pass
            session[:ip]       = request.ip
            session[:remember] = params[:remember]

            if params[:remember]
                env['rack.session.options'][:expire_after] = 30*60*60*24
            end

            return [204, ""]
        else
            return [rc.first, ""]
        end
    end

    return [401, ""]
end

def destroy_session
    session.clear
    return [204, ""]
end

Hello sorry for the late response I just saw your post, maybe it still could help you:
This code was take from the Sunstone application which is part of OpenNebula
http://opennebula.org/

use Rack::Session::Pool

def authorized?
    session[:ip] && session[:ip]==request.ip ? true : false
end

def build_session
    auth = Rack::Auth::Basic::Request.new(request.env)
    if auth.provided? && auth.basic? && auth.credentials
        user = auth.credentials[0]
        sha1_pass = Digest::SHA1.hexdigest(auth.credentials[1])

        rc = SunstoneServer.authorize(user, sha1_pass)
        if rc[1]
            session[:user]     = user
            session[:user_id]  = rc[1]
            session[:password] = sha1_pass
            session[:ip]       = request.ip
            session[:remember] = params[:remember]

            if params[:remember]
                env['rack.session.options'][:expire_after] = 30*60*60*24
            end

            return [204, ""]
        else
            return [rc.first, ""]
        end
    end

    return [401, ""]
end

def destroy_session
    session.clear
    return [204, ""]
end
可是我不能没有你 2024-10-28 18:06:01

没有与 HTTP 基本身份验证关联的会话,如果您想将身份验证与会话链接起来,您必须在应用程序级别执行此操作,这听起来像 Rack 中间件一样很难。

There is no session associated with HTTP Basic Authentication, if you want to link authentication with a session you'll have to do it at application level, sounds hard as a Rack middleware.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文