【问题标题】:Rack.session cookie not being deleted even after /logout code runs in Sinatra即使 /logout 代码在 Sinatra 中运行后,Rack.session cookie 也不会被删除
【发布时间】:2015-02-20 20:46:27
【问题描述】:

我在 Ruby 中开发了一个非常简单的 Sinatra 身份验证系统,当用户将应用程序的 url 放入浏览器时,它会向用户显示一个浏览器表单。然后他们输入他们的登录详细信息,如果这些信息正确,则允许他们进入限制区域。

我的问题是即使我运行这个:-

        get '/logout' do

            session[:session_id] = nil
            current_user = false

                File.open(LOG_FILE, "a"){ |f| f.puts "Session closed... #{current_user}" } 


        end 

当我使用 Chrome 浏览器开发工具检查元素时,我仍然可以看到机架会话 cookie。我希望 cookie 会被删除,但它没有。这样做的缺点是,当我输入应用程序的 url 时,我不再看到浏览器表单,因为 Sinatra 仍然觉得我还在已登录。

我已确保,对于我使用的任何浏览器,用户名和密码都不会保存,但本质上我希望在我将 /logout 路由放入浏览器时终止会话并删除 cookie。

有人可以告诉我我需要做什么吗?我已经添加了验证方法和 / 路由的代码。

get '/' do 
            begin

                authenticate


                session[:username] = @auth.credentials[0]

                current_user = session[:username]
                File.open(LOG_FILE, "a"){ |f| f.puts " ***session created #{Time.now} #{current_user}" }

                erb :index
            rescue Exception => err
                File.open(LOG_FILE, "a"){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err} " }
                File.open(LOG_FILE, "a"){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err.backtrace.join("\n")} " }
            end

        end

认证.rb

def 验证 如果授权返回true? headers['WWW-Authenticate'] = '基本领域="限制区域"' halt 401, "未授权\n" 结束

def authorized? 
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? and @auth.basic? and @auth.credentials and is_authenticated_by_ldap(@auth.credentials) 
end

def is_authenticated_by_ldap(凭据)

      EXTENSION = @praetor.qube 
      full_username = credentials[0] + EXTENSION

    ldap = Net::LDAP.new  :host => "just-ln1-wdc03.praetor.qube", # your LDAP host name or IP goes here,
                           :port => 389, # your LDAP host port goes here,
                      #:encryption => :simple_tls,
                           :base => "DC=praetor,DC=qube", # the base of your AD tree goes here,
                           :auth => 
                      {
                            :method => :simple,
                            #:username => credentials[0],
                            :username => full_username, # a user w/sufficient privileges to read from AD goes here,
                            :password => credentials[1] # the user's password goes here
                        }   

    is_authorized = ldap.bind


    return is_authorized
end

【问题讨论】:

    标签: ruby authentication ldap sinatra session-cookies


    【解决方案1】:

    session.clear 将删除会话变量。您仍然会在 chrome 中看到一个会话,因为它是由 Rack::Session::Cookie 中间件设置的您的操作运行之后,但这只是一个空白会话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 2019-05-08
      • 2012-09-30
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多