Rails3使用easy_captcha来实现验证码
编程技术  /  houtizong 发布于 3年前   88
gem 'easy_captcha'gem 'rmagick'
rails g easy_captcha:install
EasyCaptcha.setup do |config| # Cache # config.cache = true # Cache temp dir from Rails.root # config.cache_temp_dir = Rails.root.join('tmp', 'captchas') # Cache size # config.cache_size = 500 # Cache expire # config.cache_expire = 1.day # Chars # config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z) # Length # config.length = 6 # Image # config.image_height = 40 # config.image_width = 140 # eSpeak (default disabled) # config.espeak do |espeak| # Amplitude, 0 to 200 # espeak.amplitude = 80..120 # Word gap. Pause between words # espeak.gap = 80 # Pitch adjustment, 0 to 99 # espeak.pitch = 30..70 # Use voice file of this name from espeak-data/voices # espeak.voice = nil # end # configure generator # config.generator :default do |generator| # Font # generator.font_size = 24 # generator.font_fill_color = '#333333' # generator.font_stroke_color = '#000000' # generator.font_stroke = 0 # generator.font = File.expand_path('../../resources/afont.ttf', __FILE__) # Background color # generator.image_background_color = "#FFFFFF" # Or background image (e.g. transparent png) # generator.background_image = File.expand_path('../../resources/captcha_bg.png', __FILE__) # Wave # generator.wave = true # generator.wave_length = (60..100) # generator.wave_amplitude = (3..5) # Sketch # generator.sketch = true # generator.sketch_radius = 3 # generator.sketch_sigma = 1 # Implode # generator.implode = 0.1 # Blur # generator.blur = true # generator.blur_radius = 1 # generator.blur_sigma = 2 # endend
<p><%= captcha_tag %></p> <p><%= text_field_tag :captcha %></p>
if captcha_valid? params[:captcha] redirect_to :index, :notice => "验证成功" else flash[:alert] = "验证失败" end
# encoding: utf-8class SessionsController < Devise::SessionsController skip_before_filter :require_no_authentication, :only => [:new, :create] def create if captcha_valid? params[:captcha] super else build_resource flash[:error] = "验证码输入错误" respond_with_navigational(resource) { render :new } end end end
devise_for :users, controllers: {sessions: "sessions"}
$(document).ready -> # 验证码刷新 $("img[alt='captcha']").each (index, item) -> item.title = '看不清?点击刷新' $("img[alt='captcha']").bind 'click', (event) -> this.src = this.src + '?'
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接