确认链接点击使用设计宝石后避免登录?

Dur*_*sad 38 ruby-on-rails devise ruby-on-rails-3 devise-confirmable

我正在使用devisegem,点击确认链接后,我想直接登录.目前要求再次登录.

最近我在devise初始化文件中添加了以下内容:

config.allow_insecure_token_lookup = true
config.secret_key = 'a8d814803c0bcc735ce657adc77793459d00154cdd7532c13d3489600dc4e963f86e14beb593a32cbe9dbbe9197c9ce50a30102f363d90350052dc8d69930033'
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Raj*_*Das 61

在以前的Devise版本中,用户在确认后自动登录.这意味着任何可以访问确认电子邮件的人都可以通过单击链接登录某人的帐户.

在电子邮件重新确认工作流程中自动签名用户也可能有害.想象一下,用户决定更改他的电子邮件地址,并在这样做时,他在新的电子邮件地址上输入错误.电子邮件将被发送到另一个地址,该地址随着手中的令牌,将能够登录该帐户.

如果用户立即纠正电子邮件,则不会造成任何伤害.但如果没有,其他人可以登录该帐户,用户不会知道它发生了.

因此,Devise 3.1确认后不再自动签署用户.通过在config/initializers/devise.rb中设置以下内容,可以在升级后暂时恢复旧行为:

config.allow_insecure_sign_in_after_confirmation = true

此选项仅暂时可用于帮助迁移.


Lou*_*eau 52

config.allow_insecure_sign_in_after_confirmationDevise中不再支持该标志.

虽然您应该了解在用户确认帐户时自动登录用户可能存在的安全问题(http://blog.plataformatec.com.br/2013/08/devise-3-1-now-with-more-secure -defaults /),对于某些应用程序而言,在用户体验方面的好处可能值得安全权衡.

毕竟,安全风险是:a)用户错误输入他们的电子邮件,b)他们没有立即纠正他们的错误,c)他们输入的电子邮件对应于有效和有效的电子邮件,d)错误接收的人电子邮件会打开它并单击链接.

如果这是您的应用程序可接受的风险配置文件,您可以覆盖设计ConfirmationsController:

class ConfirmationsController < Devise::ConfirmationsController
  def show
    self.resource = resource_class.confirm_by_token(params[:confirmation_token])
    yield resource if block_given?

    if resource.errors.empty?
      set_flash_message(:notice, :confirmed) if is_flashing_format?
      sign_in(resource) # <= THIS LINE ADDED
      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
end
Run Code Online (Sandbox Code Playgroud)

并在您的路线routes.rb:

devise_for :users, controllers: { confirmations: 'confirmations' }
Run Code Online (Sandbox Code Playgroud)

  • 如果你这样做,有必要更改`config/locales/devise.en.yml`中的`en:devise:confirmations:confirmed`字符串,说你已经登录了... (3认同)

Sjo*_*ost 16

使用更新版本的Devise,您可以执行以下操作.

config/routes.rb:

devise_for :users, controllers: { confirmations: 'users/confirmations' }
Run Code Online (Sandbox Code Playgroud)

app/controllers/users/confirmations_controller.rb:

class Users::ConfirmationsController < Devise::ConfirmationsController
  def show
    super do |resource|
      sign_in(resource)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 是的,无论令牌是否有效,都会签名.但是你可以通过将sign_in行更改为`sign_in(resource)来解决这个问题,如果是resource.errors.empty? (2认同)