Ruby on Rails 㧠CAPTCHA ãè¡ã
ãé·å¹´ã¡ã³ããã³ã¹ãã¦ããå¹¾å¤ã®æ²ç¤ºæ¿ CGI ãã¹ãã ã«ç¯ããã¦ããã®ãç®ã®å½ããã«ãã¦ããã®ã§ãæè¿ã¯ CAPTHCA ã«å¤§æ³¨ç®ã®ç§ãªã®ã§ãããä»æ¥ã¯ãRuby on Rails ã«ãã Web ã¢ããªã±ã¼ã·ã§ã³ã«å²ã¨ç°¡åã« CAPTCHA ãå°å ¥ããæ¹æ³ãç´¹ä»ãã¾ãã
ã使ãã®ã¯ Ruby/CAPTCHA ã¨ããã©ã¤ãã©ãªã§ããå人çã«ã¯ãããã»ã©æ´ç·´ãããã©ã¤ãã©ãªã ã¨ã¯æã£ã¦ããªãã®ã§ããããã¨ãããã CAPTCHA ãã¤ããããã¨è¨ã£ãå ´åã«ã¯å¿ è¦ååã§ãã
ãã¾ããRuby/GD ãã¤ã³ã¹ãã¼ã«ãã¾ããã©ããã® Blog 㧠Ruby/CAPTHCA 㯠RMagick ã«ä¾åããã¨æ¸ãã¦ããã¾ããããRuby/GD ãæ£è§£ã§ããã¤ã³ã¹ãã¼ã«ã®ä»æ¹ã¯ä»¥ä¸ãåç §ãã¦ãã ãããããã Ruby/GD ã¯å ¥ã£ã¦ããï¼ãã¨ããæ¹ã¯ãfreetype ããµãã¼ããããã«ããªãã·ã§ã³ãã¤ãã¦ã¤ã³ã¹ãã¼ã«ãããã確èªãã¦ãã ããã
ãã¤ãã« Ruby/CAPTCHA ãã¤ã³ã¹ãã¼ã«ãã¾ãããã㯠gem ã§ãµã¤ãã«å ¥ãã¾ãã
# gem install captcha
ã次㫠Controller ãç·¨éãã¾ãï¼ä»¥ä¸ã¯ã¯ã©ã¹å®£è¨é¨çã¯çç¥ãã¦ãã¾ãï¼ã
gem 'ruby-gd' gem 'captcha' require 'captcha' def captcha w = CAPTCHA::Web.new # ãã©ã³ãã®æå® w.font = "/usr/share/fonts/bitstream-vera/VeraBd.ttf" # çæãã CAPTCHA ç¨ç»åã®ä¿åå w.image_dir = "/path/to/rails/public/images/captcha" w.font_size = 23 w.rotation = 28 w.x_spacing = 6 w.y_wiggle = 22 @image = w.file_name @digest = w.digest @image_width = w.image.width @image_height = w.image.height if params[:digest] && params[:key] if CAPTCHA::Web.is_valid(params[:key], params[:digest]) session[:captcha] = true redirect_to :action => "new" else flash[:notice] = "ããªãã¯ãæ¬å½ã«äººéã§ããï¼" end end end
ãæå¾ã« view ãç·¨éãã¾ãã
<% form_tag :action => 'captcha' do %> <p><%= image_tag "captcha/#{@image}", :size => "#{@image_width}x#{@image_height}", :alt => "" %></p> <input type="text" name="key" value="" /> <input type="hidden" name="digest" value="<%= @digest %>" /> <%= submit_tag "èªè¨¼" %> <% end %>
ããã㧠OK ã§ãã
ããã®æ¹æ³ã®é£ç¹ãããã¾ãã¨
- ç»åãã¡ã¤ã«ãå¢ãç¶ããã
- CAPTCHA ç¨ç»åã®ç¨®ã丸è¦ãï¼HTMLã½ã¼ã¹ãã®ããã¨è¦ããã¨ããæå³ï¼ã§ã¡ãã£ã¨æãã
- è¦è¦é害è ã使ããªãã
1 ã«é¢ãã¦ã¯ãRuby/CAPTHCA ã§ã clean ã¨ããã¡ã½ããã§å¤ãç»åãã¡ã¤ã«ãæ¶ããã¨ãã§ããã®ã§ãããSAFE ã¬ãã«ã 2 以ä¸ã ã¨åããªãã®ã§ç§ã¯ä½¿ã£ã¦ãã¾ããããã ãä»å¾ FIX ããããã¨ãæå¾ ãã¦ä»¥ä¸ã®ããã«ãªã¼ãã¼ã©ã¤ããã¦ä½¿ã£ã¦ãã¾ãã
module CAPTCHA class Web def clean Dir.foreach(@image_dir) do |entry| next if entry !~ /\.png$/ entry.untaint if Time.now - File.stat(File.join(@image_dir, entry)).mtime > @clean_up_interval File.delete(File.join(@image_dir, entry)) end end end end #class Web end #module CAPTCHA
ãããç¨ããå ´å㯠file_name ã¡ã½ãããå¼ã°ããç¬éã«ç»åãã¡ã¤ã«ãçæãããã®ã§ãã®ç´åã«ä»¥ä¸ã®ããã«è¿½å ãã¾ãã
# 以ä¸ã§æå®ããç§æ°ä»¥åã®ãã¡ã¤ã«ã¯åé¤ãã w.clean_up_interval = 600 w.clean
2 ã«é¢ãã¦ã¯ãã¡ãã»ã¼ã¸ãã¤ã¸ã§ã¹ããã CAPTCHA æååãæ¨æ¸¬ããã®ã¯å®¹æãªãã¨ã§ã¯ãªããããã£ãã¨ãã¦ãæãã人ãæã§æã¤ããé
ãã§ããããããCAPTCHA ã®å½¹ç®ãèããæãªããããã¨ã¯ãªãã§ãããï¼CAPTHCA ç»åã OCR ã§çªç ´ãããã®ã¨åããããã®ãªã¹ã¯ããªï¼ï¼ã
3 ã«é¢ãã¦ã¯ãªããã¹ãªãã§ãã以åç´¹ä»ãã JCapthca ãªãé³å£° CAPTCHA ãã§ãããããªã®ã§ããããã£ãå¥ã®ã½ãªã¥ã¼ã·ã§ã³ãç¨ãããããªãã¨æãã¾ãã