Ruby on rails Rails:用户身份验证在正确输入用户名/密码后不重定向

Ruby on rails Rails:用户身份验证在正确输入用户名/密码后不重定向,ruby-on-rails,redirect,http-headers,authentication,Ruby On Rails,Redirect,Http Headers,Authentication,我有一个rails应用程序,由于某些原因,我的登录操作无法工作。我在中输入了正确的用户名/密码,但它不会重定向到所需的“菜单”操作。它只是每次都将我重定向到登录操作(我已将其设置为在登录不成功时发生)。我声明,除非会话[:user\u id]。当我故意输入错误的密码时,flash消息是正确的,它说“无效用户名/密码”,当输入正确的密码时,它不正确,这意味着它能识别它,不知何故会话没有被创建。下面是我的代码 应用程序控制器 protected def confirm_logged_in u

我有一个rails应用程序,由于某些原因,我的登录操作无法工作。我在中输入了正确的用户名/密码,但它不会重定向到所需的“菜单”操作。它只是每次都将我重定向到登录操作(我已将其设置为在登录不成功时发生)。我声明
,除非会话[:user\u id]
。当我故意输入错误的密码时,flash消息是正确的,它说“无效用户名/密码”,当输入正确的密码时,它不正确,这意味着它能识别它,不知何故会话没有被创建。下面是我的代码

应用程序控制器

protected
def confirm_logged_in
    unless session[:user_id]
        flash[:notice] = "Please Log In"
        redirect_to(:controller => 'access', :action => 'login')
        return false
    else
        return true
    end
end
访问控制器(魔法发生的地方)

Class AccessController[:登录,:尝试\u登录,:注销]
def索引
菜单
渲染('菜单')
结束
def菜单
#显示文本和链接
结束
def登录
#登录表单
结束
def尝试登录
授权用户=AdminUser.authenticate(参数[:用户名],参数[:密码])
如果是授权用户
flash[:注意]=“您现在已登录”
将_重定向到(:action=>“menu”)
其他的
flash[:注意]=“无效的用户名/密码”
将_重定向到(:action=>“login”)
结束
结束
def注销
会话[:用户\u id]=无
会话[:用户名]=无
flash[:注意]=“您已注销”
将_重定向到(:action=>“login”)
结束
结束
管理员用户模型

require 'digest/sha1'

class AdminUser < ActiveRecord::Base

# because we created a migration to change the name of the users tabe to admin_users we have   to specify
# set_table_name("admin_users")
# or we can change the class name and file name like we did
attr_accessible :first_name, :last_name, :username, :email 
attr_accessor :password
attr_protected :hashed_password, :salt

scope :named, lambda {|first,last| where(:first_name => first, :last_name => last)}

has_and_belongs_to_many :pages
has_many :section_edits
has_many :sections, :through => :section_edits

EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z)0-9.-]+\.[A-Z]{2,4}$/i

validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :username

validates_length_of :first_name, :maximum => 25
validates_length_of :last_name, :maximum => 50
validates_length_of :username, :within => 3..25

validates_length_of :password, :within => 8..25, :on => :create

validates_uniqueness_of :username

validates :email, :presence => true, :length => {:maximum => 100}, :format => EMAIL_REGEX, :confirmation => true

before_save :create_hashed_password
after_save :clear_password

def self.authenticate(username="", password="")
user = AdminUser.find_by_username(username)
if user && user.password_match?(password)
  return user
else
  return false
end
end

def password_match?(password="")
hashed_password == AdminUser.hash_with_salt(password,salt) 
end

def self.make_salt(username="")
Digest::SHA1.hexdigest("User #{username} with #{Time.now} to make salt")
end

def self.hash_with_salt(password="", salt="")
Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
end

private

def create_hashed_password
unless password.blank?
  self.salt = AdminUser.make_salt(username) if salt.blank?
  self.hashed_password = AdminUser.hash_with_salt(password,salt)
end
end

def clear_password
self.password = nil
end

end
需要“摘要/sha1”
类AdminUserfirst,:last_name=>last)}
_和_属于_多个:页面
有许多:节编辑
有多个:节,:至=>:节\u编辑
电邮地址(REGEX=/^[A-Z0-9.[UZ0%+-]+@[A-Z)0-9.-]+\[A-Z]{2,4}$/i
验证是否存在:first\u name
验证是否存在:姓氏
验证是否存在:用户名
验证的长度为:first\u name,:max=>25
验证的长度为:last\u name,:max=>50
验证:username,:within=>3..25的长度
验证以下内容的长度:password,:within=>8..25,:on=>:create
验证用户名的唯一性
验证:email,:presence=>true,:length=>{:maximum=>100},:format=>email\u REGEX,:confirmation=>true
保存前:创建散列密码
保存后:清除密码
定义自我验证(用户名=”,密码=”)
user=AdminUser.find\u by\u用户名(username)
如果用户和用户密码匹配?(密码)
返回用户
其他的
返回错误
结束
结束
def密码匹配?(密码=)
hashed_password==AdminUser.hash_with_salt(password,salt)
结束
def self.make_salt(用户名=”)
摘要:SHA1.hexdigest(“用户#{username}与#{Time.now}一起制作盐”)
结束
定义self.hash_与_salt(密码=”,salt=”)
摘要:SHA1.hexdigest(“将{salt}放在{password}上”)
结束
私有的
def创建散列密码
除非密码为空?
self.salt=AdminUser。如果salt.blank,是否生成_salt(用户名)?
self.hashed_password=AdminUser.hash_with_salt(密码,salt)
结束
结束
def清除密码
self.password=nil
结束
结束

我找到了解决方案。这很简单。问题是我在登录时没有创建会话,这就是为什么登录没有识别会话,因为它们没有初始化。 在Access Controller中,我只是将其更改为:

def attempt_login
    authorised_user = AdminUser.authenticate(params[:username], params[:password])
    if authorised_user
        session[:user_id] = authorised_user.id
        session[:username] = authorised_user.username
        flash[:notice] = "You are now logged in"
        redirect_to(:action => 'menu')
    else
        flash[:notice] = "Invalid username/password"
        redirect_to(:action => 'login')
    end
end

修正案是代码中添加的两个会话行

我确定这与会话未被识别有关,因为如果登录失败,我会将flash消息设置为“请登录”,并且会这样做。当我在没有会话的情况下进行时,它会直接通过。这是我第一次看到传说中的
,除非另有
块。我想买张彩票。
def attempt_login
    authorised_user = AdminUser.authenticate(params[:username], params[:password])
    if authorised_user
        session[:user_id] = authorised_user.id
        session[:username] = authorised_user.username
        flash[:notice] = "You are now logged in"
        redirect_to(:action => 'menu')
    else
        flash[:notice] = "Invalid username/password"
        redirect_to(:action => 'login')
    end
end