gpt4 book ai didi

ruby - Thin、Sinatra、拦截静态文件请求做CAS认证

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:24 27 4
gpt4 key购买 nike

我正在使用 casrack-the-authenticator gem 进行 CAS 身份验证。我的服务器在 Sinatra 上运行 Thin。我已经让 CAS 身份验证位正常工作,但我不确定如何告诉 Rack 拦截“/index.html”请求以确认 CAS 登录,如果不允许用户查看页面,则返回 HTTP 403 响应而不是提供实际页面。有人对此有经验吗?谢谢。

我的应用:

class Foo < Sinatra::Base
enable :sessions
set :public, "public"
use CasrackTheAuthenticator::Simple, :cas_server => "https://my.cas_server.com"
use CasrackTheAuthenticator::RequireCAS

get '/' do
puts "Hello World"
end
end

我的 rackup 文件:

require 'foo'

use Rack::CommonLogger
use Rack::Lint

run Foo

让 Rack 了解其文件服务中的身份验证的初步尝试(欢迎提出意见和想法):

builder = Rack::Builder.new do
map '/foo/index.html' do
run Proc.new { |env|
user = Rack::Request.new(env).session[CasrackTheAuthenticator::USERNAME_PARAM]
[401, { "Content-Type" => "text/html" }, "CAS Authentication Required"] unless user
# Serve index.html because we detected user
}
end

map '/foo' do
run Foo
end
end

run builder

最佳答案

Casrack-the-Authenticator 会将 CAS 信息放入 Rack session 中。您可以在另一个 Rack 中间件或您的 Sinatra 应用程序中提取它。

以下是针对 Rails 应用程序的,但概念与 Sinatra 或 Rack 中间件类似:

# in app/controllers/application_controller.rb:
protected

def require_sign_in!
render :nothing => true, :status => 403 unless signed_in?
end

def signed_in?
current_user.present?
end

def current_user
@current_user ||= Person.find_by_username(session[CasrackTheAuthenticator::USERNAME_PARAM])
end

关于ruby - Thin、Sinatra、拦截静态文件请求做CAS认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2700065/

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