con*_*isi 23 captcha ruby-on-rails
我想在Rails项目中实现一个表单提交的验证码,但我不知道该怎么做.我倾向于简单的实现,使用时的可靠性,因为它太复杂,因为我的应用程序不需要太高的安全级别.
有人有什么建议吗?
Dan*_*ski 17
将CAPTCHA添加到Rails应用程序的最简单方法是使用Ambethia reCAPTCHA:
1.安装:
config.gem "ambethia-recaptcha", :lib => "recaptcha/rails",
:source => "http://gems.github.com"
Run Code Online (Sandbox Code Playgroud)
如果您愿意,也可以将其安装为插件.
2.获取reCAPTCHA帐户:
您必须创建一个reCAPTCHA密钥.您可以在reCAPTCHA站点上执行此操作.
3.用法:
使用recaptcha_tags输出所需的HTML代码,然后验证与输入verify_recaptcha.
4.进一步阅读:
将以下内容添加到您的 GEMFILE
gem "galetahub-simple_captcha", :require => "simple_captcha"
Run Code Online (Sandbox Code Playgroud)
或者
gem 'galetahub-simple_captcha', :require => 'simple_captcha',
:git => 'git://github.com/galetahub/simple-captcha.git'
Run Code Online (Sandbox Code Playgroud)
bundle install如果您使用的是 Bundler,则运行(或使用包管理器进行 Rails 配置)
安装后,请按照以下简单步骤设置插件。设置将取决于您的应用程序使用的 rails 版本。
rails generate simple_captcha
rake db:migrate
Run Code Online (Sandbox Code Playgroud)
在“app/controllers/application.rb”文件中添加以下行
ApplicationController < ActionController::Base
include SimpleCaptcha::ControllerHelpers
end
Run Code Online (Sandbox Code Playgroud)
在表单标签内的视图文件中添加此代码
<%= show_simple_captcha %>
Run Code Online (Sandbox Code Playgroud)
并在控制器的操作中将其验证为
if simple_captcha_valid?
do this
else
do that
end
Run Code Online (Sandbox Code Playgroud)
在表单标签内的视图文件中添加此代码
<%= show_simple_captcha(:object=>"user") %>
Run Code Online (Sandbox Code Playgroud)
并在模型类中添加此代码
class User < ActiveRecord::Base
apply_simple_captcha
end
Run Code Online (Sandbox Code Playgroud)
<%= form_for @user do |form| -%>
...
<%= form.simple_captcha :label => "Enter numbers.." %>
...
<% end -%>
Run Code Online (Sandbox Code Playgroud)
注意:@user.valid?仍然可以正常工作,它不会验证验证码。
@user.valid_with_captcha?
Run Code Online (Sandbox Code Playgroud)
注意:@user.save 仍然可以正常工作,它不会验证验证码。
@user.save_with_captcha
Run Code Online (Sandbox Code Playgroud)
SimpleCaptcha 检测您是否使用 Formtastic 并附加
“SimpleCaptcha::CustomFormBuilder”.
<%= form.input :captcha, :as => :simple_captcha %>
Run Code Online (Sandbox Code Playgroud)
*label* - provides the custom text b/w the image and the text field, the default is “type the code from the image”
*object* - the name of the object of the model class, to implement the model based captcha.
*code_type* - return numeric only if set to ‘numeric’
Run Code Online (Sandbox Code Playgroud)
image_style - provides the specific image style for the captcha image.
Run Code Online (Sandbox Code Playgroud)
该插件有八种不同的样式,如……
默认样式是“simply_blue”。您还可以指定 'random' 以选择随机图像样式。
distortion - handles the complexity of the image. The :distortion can be set to ‘low’, ‘medium’ or ‘high’. Default is ‘low’.
Run Code Online (Sandbox Code Playgroud)
*创建“rails_root/config/initializers/simple_captcha.rb”*
SimpleCaptcha.setup do |sc|
# default: 100x28
sc.image_size = '120x40'
# default: 5
sc.length = 6
# default: simply_blue
# possible values:
# 'embosed_silver',
# 'simply_red',
# 'simply_green',
# 'simply_blue',
# 'distorted_black',
# 'all_black',
# 'charcoal_grey',
# 'almost_invisible'
# 'random'
sc.image_style = 'simply_green'
# default: low
# possible values: 'low', 'medium', 'high', 'random'
sc.distortion = 'medium'
end
Run Code Online (Sandbox Code Playgroud)
您可以添加自己的样式:
SimpleCaptcha.setup do |sc|
sc.image_style = 'mycaptha'
sc.add_image_style('mycaptha', [
"-background '#F4F7F8'",
"-fill '#86818B'",
"-border 1",
"-bordercolor '#E0E2E3'"])
end
Run Code Online (Sandbox Code Playgroud)
您也可以提供安装 image_magick 的路径:
SimpleCaptcha.setup do |sc|
sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert
end
Run Code Online (Sandbox Code Playgroud)
您可以提供存储 tmp 文件的路径。当您无法访问 /tmp(默认目录)时,这很有用
SimpleCaptcha.setup do |sc|
sc.tmp_path = '/tmp' # or somewhere in project eg. Rails.root.join('tmp/simple_captcha').to_s, make shure directory exists
end
Run Code Online (Sandbox Code Playgroud)
如何更改 SimpleCaptcha DOM 元素的 CSS?
您可以根据需要在此文件中更改 SimpleCaptcha DOM 元素的 CSS。
*/app/views/simple_captcha/_simple_captcha.erb*
基于控制器的示例
<%= show_simple_captcha %>
<%= show_simple_captcha(:label => "human authentication") %>
Run Code Online (Sandbox Code Playgroud)
<%= show_simple_captcha(:object => 'user', :label => "human authentication") %>
Run Code Online (Sandbox Code Playgroud)
模型选项
message - provides the custom message on failure of captcha authentication the default is “Secret Code did not match with the Image”
add_to_base - if set to true, appends the error message to the base.
Run Code Online (Sandbox Code Playgroud)
模型示例
class User < ActiveRecord::Base
apply_simple_captcha
end
class User < ActiveRecord::Base
apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true
end
Run Code Online (Sandbox Code Playgroud)
我在我的 PHP 项目之一中使用了 Recaptcha。http://recaptcha.net/
据该网站称,它还有 Ruby 插件 ( http://recaptcha.net/resources.html )。虽然第一个 ruby 链接不起作用,但下一个链接仍然有效。http://svn.ambethia.com/pub/rails/plugins/recaptcha/
检查一下。