用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
sanyking的公告
文章分类
    存档

    原创  在ruby on rails中实现验证码 收藏

     
    1.在windows上安装Rmagic,如果你是通过gem安装的,

    require ’Rmagic’ 

    要修改为: 

    require ’rubygems’
    require ’Rmagick’ 

    才能正确引入。


    2.网上那个例子,画布是使用Rmagic内置的图像格式,Rmagic内置的图像格式还有:


    gradient
    *
    梯度,比如gradient:red
    -blue


    granite
    花岗石,比如: 
    "granite:".


    logo
    logo型的图像. 如: 
    "logo:",后面会多显示一个五角星^_^


    netscape
    非常漂亮的彩条。如: 
    "netscape:"


    null*
    空白 使用方式: 
    "null:" 

    rose 

    玫瑰 使用方式 : 
    "rose:"


    xc
    *
    设置一个背景色,比如”xc:green” 

    一个修改的例子,在rails的models下存为noisy_image.rb,在Controller就可以这样调用NoisyImage.
    new(4) : 

    ruby 代码
    require 
    'rubygems'     
    require 
    'Rmagick'    
    class NoisyImage    
      include Magick    
      attr_reader :code, :code_image   
      Jiggle 
    = 15    
      Wobble 
    = 15    
          
      def initialize(len)    
        chars 
    = ('a'..'z').to_a - ['a','e','i','o','u']    
        code_array
    =[]    
        
    1.upto(len) {code_array << chars[rand(chars.length)]}    
        granite 
    = Magick::ImageList.new('xc:#EDF7E7')    
        canvas 
    = Magick::ImageList.new   
        canvas.new_image(
    32*len, 50, Magick::TextureFill.new(granite))    
        text 
    = Magick::Draw.new   
        text.font_family 
    = 'times'    
        text.pointsize 
    = 40    
        cur 
    = 10    
            
        code_array.each
    {|c|    
          rand(
    10> 5 ? rot=rand(Wobble):rot= -rand(Wobble)    
          rand(
    10> 5 ? weight = NormalWeight : weight = BoldWeight    
          text.annotate(canvas,
    0,0,cur,30+rand(Jiggle),c){    
            self.rotation
    =rot    
            self.font_weight 
    = weight    
            self.fill 
    = 'green'    
          }
        
          cur 
    += 30    
        }
        
        @code 
    = code_array.to_s    
        @code_image 
    = canvas.to_blob{    
          self.format
    ="JPG"     
        }
        
      end   
          
    end   
       

    3.与rails应用的结合,和一般的验证码原理一样,将产生的随机数存储在session或者request范围内,提交的时候进行比较验证即可。比如产生图片的时候将随机字母存储在session[:code]中: 

    def register
        session[:noisy_image] 
    = NoisyImage.new(4) #生成一个有4字符的图片
        session[:code] 
    = session[:noisy_image].code
      
    end

    验证的时候,比较提交的type_code与session[:code]即可,为了安全性考虑,最好还是不考虑使用客户端验证。 

     def code_check
        
    if params[:code]!=session[:noisy_image].code
             render_text 
    " 验证码错误!"
        
    else
            render_text 
    ""
        end   
      end

    controller.rb具体代码如下:

    java 代码
    class UserController < ApplicationController    
      model:noisy_image    
    def code_check    
        
    if params[:code]!=session[:noisy_image].code    
             render_text 
    " 验证码错误!"   
        
    else   
            render_text 
    ""   
        end       
      end    
    def register    
        session[:noisy_image] 
    = NoisyImage.new(5) #生成一个有5字符的图片    
        session[:code] 
    = session[:noisy_image].code    
          
      end    
          
      def code_image    
        image 
    = session[:noisy_image].code_image    
        send_data image, :type 
    => 'image/jpeg', :disposition => 'inline'    
      end    
      end   

     

    在页面显示图片,类似servlet一样直接调用Controller的action: 

    在register.rhtml中加入以下代码:

    ruby 代码
    ruby 代码
    <img height="30"  src= "/user/code_image">   

    发表于 @ 2007年09月04日 22:20:00 | 评论( loading... ) | 编辑| 举报| 收藏

    • 发表评论
    • 评论内容:
    •  
    Copyright © sanyking
    Powered by CSDN Blog