ruby-on-rails – 设计confirm_token无效
发布时间:2020-12-16 19:34:14 所属栏目:百科 来源:网络整理
导读:我的User.rb: class User ActiveRecord::Base devise :database_authenticatable,:registerable,:confirmable,:token_authenticatable,:recoverable,:rememberable,:trackable,:validatable,:authentication_keys = [:name] 我的路线: devise_for :users,:
我的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,包括此更改. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |