世界上最伟大的投资就是投资自己的教育

全场限时 5 折

首页Ruby
Mikasa·Ackerman · 真仙

Rails 完美的图形验证码

Mikasa·Ackerman发布于2593 次阅读

RuCaptcha

完美的 Ruby 图形验证码 Gem - RuCaptcha

由 ruby china 社区创始人之一 李华顺打造!

效果展示

特色

  • 不需要 RMagick,所以安装不再有困难、内存泄露也不再有了!
  • 调用系统 ImageMagick 命令生成图片,效率高,和 mini_magick 机制一样;
  • 使用简单;
  • 自动化的文件缓存,减少图片生成时对 CPU 的开销;
  • 漂亮!

使用方法 这里引入 官方文档

Gemfile:

    gem 'rucaptcha'

config/routes.rb 增加 mount RuCaptcha:

    Rails.application.routes.draw do
    ...
    mount RuCaptcha::Engine : "/rucaptcha"
    ...
    end

Controller app/controller/account_controller.rb

    class AccountController < ApplicationController
      def create
        @user = User.new(params[:user])
        if verify_rucaptcha?(@user) && @user.save
          redirect_to root_path, notice: 'Sign up successed.'
        else
          render 'account/new'
        end
      end
    end

View app/views/account/new.html.erb

    <form>
      ...
      <div class="form-group">
        <%= rucaptcha_input_tag(class: 'form-control', placeholder: 'Input Captcha') %>
        <%= rucaptcha_image_tag(alt: 'Captcha') %>
      </div>
      ...
    </form>

如果你需要定制一下,可以新建 config/initializes/rucaptcha.rb

    RuCaptcha.configure do
      # Color style, default: :colorful, allows: [:colorful, :black_white]
      # self.style = :colorful
      # Custom captcha code expire time if you need, default: 2 minutes
      # self.expires_in = 120
      # [Requirement / 重要]
      # Store Captcha code where, this config more like Rails config.cache_store
      # default: Read config info from `Rails.application.config.cache_store`
      # But RuCaptcha requirements cache_store not in [:null_store, :memory_store, :file_store]
      # 默认:会从 Rails 配置的 cache_store 里面读取相同的配置信息,并尝试用可以运行的方式,用于存储验证码字符
      # 但如果是 [:null_store, :memory_store, :file_store] 之类的,你可以通过下面的配置项单独给 RuCaptcha 配置 cache_store
      self.cache_store = [:memory_store, '127.0.0.1'] # 缓存方式需要和 application.rb 保持一致
      # 如果想要 disable cache_store 的 warning,就设置为 true,default false
      # self.skip_cache_store_check = true
      # Chars length, default: 5, allows: [3 - 7]
      self.length = 4 # 长度我设置为4,默认是5
      # enable/disable Strikethrough.
      self.strikethrough = false # 划线我也给去除了降低难度吧,如果加难度就加上
      # enable/disable Outline style
      # self.outline = false
    end

如果你简单的验证一下验证码的话也提供了方法,查看源码我们可以看到例子

    # exmaples:
    #
    #   verify_rucaptcha?
    #   verify_rucaptcha?(user, keep_session: true)
    #   verify_rucaptcha?(nil, keep_session: true)
    #   verify_rucaptcha?(nil, captcha: params[:user][:captcha])
    #

    def verify_rucaptcha?(resource = nil, opts = {})
      opts ||= {}

      store_info = RuCaptcha.cache.read(rucaptcha_sesion_key_key)
      # make sure move used key
      RuCaptcha.cache.delete(rucaptcha_sesion_key_key) unless opts[:keep_session]

      # Make sure session exist
      if store_info.blank?
        return add_rucaptcha_validation_error
      end

      # Make sure not expire
      if (Time.now.to_i - store_info[:time]) > RuCaptcha.config.expires_in
        return add_rucaptcha_validation_error
      end

      # Make sure parama have captcha
      captcha = (opts[:captcha] || params[:_rucaptcha] || '').downcase.strip
      if captcha.blank?
        return add_rucaptcha_validation_error
      end

      if captcha != store_info[:code]
        return add_rucaptcha_validation_error
      end

      true
    end

由于有这个判断

    captcha = (opts[:captcha] || params[:_rucaptcha] || '').downcase.strip

也就是说我们的参数只要有 params[:_rucaptcha] 即使没有 user 也是一样可以判断的,前提是表单使用的是前面的表单。

本站文章均为原创内容,如需转载请注明出处,谢谢。

1 条回复
喜欢
统计信息
    学员: 29204
    视频数量: 1985
    文章数量: 489

© 汕尾市求知科技有限公司 | Rails365 Gitlab | Qiuzhi99 Gitlab | 知乎 | b 站 | 搜索

粤公网安备 44152102000088号粤公网安备 44152102000088号 | 粤ICP备19038915号

Top