gpt4 book ai didi

ruby-on-rails - Authlogic - 通过基本 HTTP 身份验证进行身份验证

转载 作者:行者123 更新时间:2023-12-01 11:09:27 28 4
gpt4 key购买 nike

我想使用“authenticate_ with_ http_ basic”,但我就是做不到工作。

在我的 RoR 应用程序中,Authlogic 工作正常,我为此使用了用户 session 。在保持该方法不变的同时,我需要使用 authenticate_with_http_basic。我有一个 iPhone SDK 应用程序,现在我需要从我的 web 应用程序中获取一些产品并显示为列表。所以我假设我需要像这样将请求发送到我的网络应用程序; http://username:password@192.168.1.9/products/

所以我的问题是验证此用户名和密码以及我需要对我的 UserSession Controller 做什么?

最佳答案

您不需要对 UserSessionController 做任何事情,因为该 Controller 只会处理登录表单提交和注销。

Authlogic 和 authenticate_with_http_basic 彼此无关。如果你想通过 HTTP basic 进行身份验证,你只需要创建一个方法来使用 Rails 提供的方法进行身份验证,并将该方法放在 before_filter 上。通过 HTTP 身份验证登录,我假设用户名和密码对于每个请求都是强制性的。

最后,您的 ProductsController 应该是这样的

class ProductsController < ApplicationController
before_filter :authenticate_via_http_basic

# In case you have some method to redirect user to login page, skip it
skip_before_filter :require_authentication

...

protected

def authenticate_via_http_basic
unless current_user
authenticate_with_http_basic do |username, password|
if user = User.find_by_username(username)
user.valid_password?(password)
else
false
end
end
end
end

关于ruby-on-rails - Authlogic - 通过基本 HTTP 身份验证进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1605838/

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