加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用条件启动sign_up页面?

发布时间:2020-12-16 20:28:00 所属栏目:百科 来源:网络整理
导读:我想询问用户一个问题,只有在用户正确回答我的问题时,才能注册.我搜查了 devise how-to acticles,但我的情况似乎不在那里. 有没有一个惯用的方式来处理这种情况? 第一个想法可能是使用javascript,但答案存储在LDAP中,我希望在rails中更容易处理. 我也在考虑
我想询问用户一个问题,只有在用户正确回答我的问题时,才能注册.我搜查了 devise how-to acticles,但我的情况似乎不在那里.

有没有一个惯用的方式来处理这种情况?

第一个想法可能是使用javascript,但答案存储在LDAP中,我希望在rails中更容易处理.

我也在考虑禁用/ users / sign_up路由,手动调用动作(devise / registration#new)并呈现视图(devise / registration / new).

我可以想到的另一种方法是运行一个后台进程,它会收集会话ID,用户正确地回答了问题.在正确的答案用户将重定向到公开的注册页面,这将使用守护进程检查用户的会话ID.

解决方法

为了提高以前的建议的安全性,最好的一个似乎是以核心的方式,但它是不安全的(无论cookie是否加密,请参阅我对OP的评论)
# app/controllers/preauth_controller.rb
def new
end

def create
  if params[:answer] == 'correct answer'
    # Create a secret value,the `token`,we will share it to the client
    # through the `session` and store it on the server in `Rails.cache`
    # (or you can use the database if you like)
    #
    # The fact that this token expires (by default) in 15 minutes is
    # a bonus,it will secure the system against later use of a stolen
    # cookie. The token is also secure against brute force attack
    @token = SecureRandom.base64
    session[:preauthorization_token] = @token
    Rails.cache.write("users/preauthorization_tokens/#{@token}","OK")
    redirect_to sign_up_path
  else
    flash[:error] = 'Incorrect answer'
    render :new
  end
end


# app/controllers/users_controller.rb
before_filter :verify_preauth,only: [:new,:create]

def verify_preauth
  # When we approve preauthorization we confirm that the
  # `token` is known to the client,if the client knows the token
  # let him sign up,else make him go away
  token = session[:preauthorization_token]
  redirect_to new_preauth_path unless token and Rails.cache.read("users/preauthorization_tokens/#{token}") == "OK"
end

可选择的事情/玩….

>创建用户时,删除成功使用的Rails.cache条目>玩:expires_in设置,如果你想,你通常希望它尽可能短,只要需要:)但是,默认15分钟的轨道是相当不错>有更好的方法来解决这个和类似的安全问题的cookie – 即你可以创建一个server_session对象,它基本上与会话相同,但是将数据存储在Rails.cache中,并将一个随机可取的令牌存储在用于访问的会话中缓存条目与我们在这里做的一样>只需去服务器端会话,不用担心会话的安全性,但这意味着由于你的Rails.cache往返(redis,memcache,AR,…)的响应时间更长.>而不是OK进入缓存值,你可以存储值的哈希值,如果你需要更多的数据,安全地存储在主机上,这样做> …

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读