Ruby on Rails 上的验证码建议

发布于 2024-08-13 22:46:49 字数 1536 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

聊慰 2024-08-20 22:46:49

将验证码添加到 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.进一步阅读:

The easiest way to add a CAPTCHA to your Rails application is using Ambethia reCAPTCHA:

1. Installation:

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

You can install it as a plugin, too, if you like.

2. Get a reCAPTCHA account:

You have to create a reCAPTCHA key. You can do it on the reCAPTCHA site.

3. Usage:

Use recaptcha_tags to output the necessary HTML code and then verify the input with verify_recaptcha.

4. Further reading:

二货你真萌 2024-08-20 22:46:49

安装

将以下内容添加到您的 GEMFILE 中

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

,或者

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

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

设置

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

rails generate simple_captcha
rake db:migrate

使用

基于控制器

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

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 helper

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

Validating with captcha

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

@user.valid_with_captcha?

使用验证码保存

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

@user.save_with_captcha

Formtastic 集成

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.

该插件有八种不同的样式可供选择,例如:

  1. simple_blue
  2. simple_red
  3. simple_green
  4. charcoal_grey
  5. embosed_silver
  6. all_black
  7. Distorted_black
  8. Almost_invisible

默认样式为“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

Installation

Add the following to your GEMFILE

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

or

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

Then run bundle install if you're using Bundler (or use the package manager for your Rails configuration)

Setup

After installation, follow these simple steps to setup the plugin. The setup will depend on the version of rails your application is using.

rails generate simple_captcha
rake db:migrate

Usage

Controller Based

Add the following line in the file “app/controllers/application.rb”

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

In the view file within the form tags add this code

<%= show_simple_captcha %>

and in the controller’s action authenticate it as

if simple_captcha_valid?
  do this
else
  do that
end

Model Based

In the view file within the form tags add this code

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

and in the model class add this code

class User < ActiveRecord::Base
  apply_simple_captcha
end

FormBuilder helper

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

Validating with captcha

NOTE: @user.valid? will still work as it should, it will not validate the captcha code.

@user.valid_with_captcha?

Saving with captcha

NOTE: @user.save will still work as it should, it will not validate the captcha code.

@user.save_with_captcha

Formtastic integration

SimpleCaptcha detects if your use Formtastic and appends

“SimpleCaptcha::CustomFormBuilder”.

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

Options & Examples

View Options

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

Global options

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

There are eight different styles available with the plugin as…

  1. simply_blue
  2. simply_red
  3. simply_green
  4. charcoal_grey
  5. embosed_silver
  6. all_black
  7. distorted_black
  8. almost_invisible

Default style is ‘simply_blue’. You can also specify ‘random’ to select the random image style.

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

*Create “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

You can add your own style:

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

You can provide the path where image_magick is installed as well:

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

You can provide the path where should be stored tmp files. It’s usefull when you dont have acces to /tmp (default directory)

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

How to change the CSS for SimpleCaptcha DOM elements?

You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file.

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

View’s Examples

Controller Based Example

<%= show_simple_captcha %>

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

Model Based Example

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

Model Options

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.

Model’s Example

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
莫相离 2024-08-20 22:46:49

嗯,ReCaptcha 可以完成这项工作,并且网上有很多关于它的教程。

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

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

after_validation :on => :create 

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

Well, ReCaptcha will do the job and there are many tutorials on it online.

However, you have to write a correct "def create" (create method) in your controller that will pass whatever is in your form plus validate Recaptcha at the same time. Then it will work nicely.

There was one little problem with it. After I inserted ReCaptcha into my form, the form validation stopped working. However, it can be fixed with an easy code inserted into the model file:

after_validation :on => :create 

(:create = is the "def create" method in your controller). It will force the form to validate the form first and then validate Recaptcha.

べ繥欢鉨o。 2024-08-20 22:46:49

我在我的 PHP 项目之一中使用了 Recaptcha。 http://recaptcha.net/
据该网站称,它还有 Ruby 插件 (http://recaptcha.net/resources.html)。虽然第一个 ruby​​ 链接不起作用,但下一个链接仍然有效。 http://svn.ambethia.com/pub/rails/plugins/recaptcha/< /a>
检查一下。

I've used Recaptcha in one of my PHP project. http://recaptcha.net/
According to the site, it also has plugins for Ruby (http://recaptcha.net/resources.html). Although first ruby link didn't work, next link still works. http://svn.ambethia.com/pub/rails/plugins/recaptcha/
Check it.

长亭外,古道边 2024-08-20 22:46:49

我已经使用 ambethia recapchat 进行 Rails 应用程序。它比其他的最容易

I've used ambethia recapchat for rails application. it most easy than other

栩栩如生 2024-08-20 22:46:49

就功能而言,用于 Rails 的 reCAPTCHA 非常棒。但是,如果您需要 XHTML 验证,请远离!该插件不会(并且可能永远不会)验证。我觉得很尴尬的是,我的整个网站上只有一个页面没有验证 - 这是带有 reCAPTCHA 的页面。如果有其他选择,我会选择。

reCAPTCHA for rails is great, in terms of functionality. However, if you require XHTML validation, RUN AWAY! This plugin does not (and probably never will) validate. I find it embarrassing that only one page on my entire site does not validate - it is the page with reCAPTCHA. If there was ANY other choice, I would take it.

对风讲故事 2024-08-20 22:46:49

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

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

if you're after a CAPTCHA that validates, and is (almost) as easy to use as reCAPTCHA, please give my SlideCAPTCHA a try. (Wrote it a few days ago, needs some tests in real-life usage.) I based its deployment process on the reCAPTCHA plugin, but you can style it with CSS.

It does require Ruby/GD, however, so if you don't have GD already, I can't promise that GD is easy to install and use!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文