26

我想在 Rails 项目中实现验证码以提交表单,但我不确定该怎么做。我倾向于实现的简单性和使用时的可靠性,因为它过于复杂,因为我的应用程序不需要太高的安全级别。

有人有什么建议吗?

4

7 回答 7

19

将 CAPTCHA 添加到 Rails 应用程序的最简单方法是使用Ambethia reCAPTCHA

1.安装:

config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", 
      :source => "http://gems.github.com"

如果您愿意,也可以将其安装为插件。

2. 获取一个 reCAPTCHA 帐户:

您必须创建一个 reCAPTCHA 密钥。您可以在reCAPTCHA 网站上进行。

3. 用法:

用于recaptcha_tags输出必要的 HTML 代码,然后使用verify_recaptcha.

4. 延伸阅读:

于 2009-12-07T13:23:18.463 回答
5

安装

将以下内容添加到您的 GEMFILE

gem "galetahub-simple_captcha", :require => "simple_captcha"

或者

gem 'galetahub-simple_captcha', :require => 'simple_captcha',
    :git => 'git://github.com/galetahub/simple-captcha.git'

bundle install如果您使用的是 Bundler,则运行(或使用包管理器进行 Rails 配置)

设置

安装后,按照这些简单的步骤设置插件。设置将取决于您的应用程序使用的 Rails 版本。

rails generate simple_captcha
rake db:migrate

用法

基于控制器

在文件“app/controllers/application.rb”中添加以下行</p>

ApplicationController < ActionController::Base
  include SimpleCaptcha::ControllerHelpers
end

在表单标签内的视图文件中添加此代码

<%= show_simple_captcha %>

并在控制器的操作中将其验证为

if simple_captcha_valid?
  do this
else
  do that
end

基于模型

在表单标签内的视图文件中添加此代码

<%= show_simple_captcha(:object=>"user") %>

并在模型类中添加此代码

class User < ActiveRecord::Base
  apply_simple_captcha
end

FormBuilder 助手

<%= form_for @user do |form| -%>
  ...
  <%= form.simple_captcha :label => "Enter numbers.." %>
  ...
<% end -%>

使用验证码进行验证

注意:@user.valid?仍然可以正常工作,它不会验证验证码。

@user.valid_with_captcha?

使用验证码保存

注意:@user.save 仍然可以正常工作,它不会验证验证码。

@user.save_with_captcha

形式整合

SimpleCaptcha 检测您是否使用 Formtastic 并附加

“SimpleCaptcha::CustomFormBuilder”.

<%= form.input :captcha, :as => :simple_captcha %>

选项和示例

查看选项

*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’

全局选项

image_style - provides the specific image style for the captcha image.

该插件有八种不同的样式可供选择...</p>

  1. 简单的蓝色
  2. 简单的_red
  3. 简单的_绿色
  4. 炭灰色
  5. embossed_silver
  6. 全黑
  7. 扭曲的黑色
  8. 几乎看不见

默认样式是“simply_blue”。您还可以指定“随机”来选择随机图像样式。

distortion - handles the complexity of the image. The :distortion can be set to ‘low’, ‘medium’ or ‘high’. Default is ‘low’.

*创建“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

您可以添加自己的样式:

SimpleCaptcha.setup do |sc|
  sc.image_style = 'mycaptha'
  sc.add_image_style('mycaptha', [
      "-background '#F4F7F8'",
      "-fill '#86818B'",
      "-border 1",
      "-bordercolor '#E0E2E3'"])
end

您也可以提供安装 image_magick 的路径:

SimpleCaptcha.setup do |sc|
  sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert
end

您可以提供应存储 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

如何更改 SimpleCaptcha DOM 元素的 CSS?

您可以根据需要在此文件中更改 SimpleCaptcha DOM 元素的 CSS。

*/app/views/simple_captcha/_simple_captcha.erb*

查看示例

基于控制器的示例

<%= show_simple_captcha %>

<%= show_simple_captcha(:label => "human authentication") %>

基于模型的示例

<%= show_simple_captcha(:object => 'user', :label => "human authentication") %>

模型选项

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.

模型示例

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
于 2013-02-12T11:41:23.843 回答
3

好吧,ReCaptcha 会完成这项工作,网上有很多教程。

但是,您必须在控制器中编写正确的“def create”(创建方法),该方法将传递表单中的任何内容并同时验证 Recaptcha。然后它会很好地工作。

有一个小问题。在我将 ReCaptcha 插入表单后,表单验证停止工作。但是,可以通过在模型文件中插入一个简单的代码来修复它:

after_validation :on => :create 

(:create = 是控制器中的“def create”方法)。它将强制表单首先验证表单,然后验证 Recaptcha。

于 2012-08-24T18:32:18.017 回答
0

我在我的一个 PHP 项目中使用了 Recaptcha。http://recaptcha.net/
根据该站点,它也有 Ruby 插件(http://recaptcha.net/resources.html)。虽然第一个红宝石链接不起作用,但下一个链接仍然有效。http://svn.ambethia.com/pub/rails/plugins/recaptcha/ 检查它。

于 2009-12-07T10:34:19.487 回答
0

如果您需要验证的验证码,并且(几乎)与 reCAPTCHA 一样易于使用,请试一试我的 SlideCAPTCHA。(几天前写的,需要在实际使用中进行一些测试。)我的部署过程基于 reCAPTCHA 插件,但您可以使用 CSS 对其进行样式设置。

但是,它确实需要 Ruby/GD,所以如果您还没有 GD,我不能保证 GD 易于安装和使用!

于 2010-07-08T12:54:42.153 回答
0

我已经将 ambethia recapchat 用于 Rails 应用程序。它比其他最容易

于 2009-12-07T13:13:26.963 回答
0

就功能而言,用于 rails 的 reCAPTCHA 很棒。但是,如果您需要 XHTML 验证,快跑吧!这个插件不会(并且可能永远不会)验证。我发现整个网站上只有一个页面没有验证,这很尴尬——它是带有 reCAPTCHA 的页面。如果有其他选择,我会接受。

于 2010-06-23T02:17:38.477 回答