ruby-on-rails – 设计confirm_token无效

我的User.rb:
class User < ActiveRecord::Base

    devise :database_authenticatable,:registerable,:confirmable,:token_authenticatable,:recoverable,:rememberable,:trackable,:validatable,:authentication_keys => [:name]

我的路线:

devise_for :users,:controllers => { :sessions => "sessions",:confirmations => "confirmations",:passwords => "passwords",:registrations => "registrations" }

我的ConfirmationsController是一个标准控制器,但具有不同的重定向.

我的电子邮件链接如下:

/users/confirmation?confirmation_token=167bad44a15e02b0bd570b51e1bf927b88368d8855d92b9833a24017a2bad4be

数据库用户

confirmation_token:167bad44a15e02b0bd570b51e1bf927b88368d8855d92b9833a24017a2bad4be

但是,当我点击该链接时,我只看到页面

Resend confirmation instructions
 Confirmation token is invalid

我不做什么 – 我还需要设置什么.

CONFIRMATIONCONTROLLER:

def resource_params
 params.require(:user).permit(:confirmation_token)
   end
   private :resource_params


  def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])

if resource.errors.empty?
  set_flash_message(:notice,:confirmed) if is_navigational_format?
  sign_in(resource_name,resource)
  session['new_user'] = true
  respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name,resource) }
else
  respond_with_navigational(resource.errors,:status => :unprocessable_entity){ render :new }
end
  end

  protected
    # The path used after resending confirmation instructions.
    def after_resending_confirmation_instructions_path_for(resource_name)
      new_registration_path(resource_name)
    end

我说“标准控制器”,因为当我删除它并且不使用自定义控制器问题是一样的.

解决方法

您使用的是哪个版本的设计?如果您使用3.1.0或更高版本,则会出现此行为:

CHANGELOG.md

存储在数据库中的令牌不应与您在确认电子邮件中发送的令牌相匹配.见devise/lib/devise/models/confirmable.rb,现在包含以下内容

def confirm_by_token(confirmation_token)
  original_token     = confirmation_token
  confirmation_token = Devise.token_generator.digest(self,:confirmation_token,confirmation_token)

  confirmable = find_or_initialize_with_error_by(:confirmation_token,confirmation_token)

如您所见,您通过查询字符串参数传递的令牌由Devise.token_generator使用,该操作的结果是与数据库中的令牌进行比较以发现用户记录的结果.

看起来暂时可以(在3.1但不是3.2)通过设置关闭

config.allow_insecure_token_lookup = true

在您的设计初始化程序中.但是认行为已经改变,以使设计更安全.有关设计3.1中安全性改进的完整概述,请参阅this blog post,包括此更改.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...