极验 (Geetest) Laravel 5 集成开发包
14

Laravel Geetest

Laravel Geetest

Laravel Geetest is a package for Laravel 5 developed by
Germey. It provides simple usage for laravel of Geetest.

Installation

Laravel 5.0.0 or later is required.

To get the latest version of Laravel Markdown, simply require the project using Composer:

$ composer require germey/geetest

Or you can add following to require key in compser.json.

"germey/geetest": "~1.0"

then run

$ composer update

Next, You should need to register the service provider. Open up config/app.php and add following into the providers key.

Germey\Geetest\GeetestServiceProvider::class


And you can register the Geetest Facade in the aliases of config/app.php if you want.

'Geetest' => Germey\Geetest\Geetest::class,

Configuration

To get started, you need to publish all vendor assets using following command.

$ php artisan vendor:publish

This will create a config file named config/geetest.php which you can configure geetest as you like.

It will also generate a views folder resources/views/vendor/geetest, here you can configure frontend method of geetest.

Usage

Firstly, You need to register in Geetest. Creating an app and get ID and KEY.

Then configure the in your .env file because you'd better not make them public.

Add following to .env.

GEETEST_ID=0f1097bef7xxxxxx9afdeced970c63e4
GEETEST_KEY=c070f0628xxxxxxe68e138b55c56fb3b

Next, You need to configure an Ajax validation url. Default is /auth/geetest. So you can use Trait Germey\Geetest\CaptchaGeetest in AuthController which routing '/auth'.

use Germey\Geetest\CaptchaGeetest;
class AuthController extends Controller {
    use CaptchaGeetest;
}

Then an Ajax url is configured successfully.

Also you can use this Trait in other Controller but you need to configure geetest_url in config/geetest.php.

Finally, You can use in views like following.

{!! Geetest::render() !!}

Frequently, It will be used in form.

<form action="/" method="post">
    <input name="_token" type="hidden" value="{{ csrf_token() }}">
    <input type="text" name="name">
    {!! Geetest::render() !!}
    <input type="submit" value="submit">
</form>

When you click the submit button, it will verify the Geetest Code. If you didn't complete the validation, it will alert some text and prevent the form from submitting.

Or you can set other style of Geetest.

{!! Geetest::render('embed') !!}
{!! Geetest::render('popup') !!}

Then it will be embed or popup style in the website. Default to float.

Also, you can set Geetest Ajax Url by following way.

{!! Geetest::setGeetestUrl('/auth/geetest')->render() !!}

By setGeetestUrl method you can set Geetest Ajax Url. If it is configured, it will override geetest_url configured in config/geetest.php.

If the validation is completed, the form will be submitted successfully.

Server Validation

What's the reason that Geetest is safe? If it only has client validation of frontend, can we say it is complete? It also has server validation to ensure that the post request is validate.

First I have to say that you can only use Geetest of Frontend. But you can also do simple things to achieve server validation.

You can use $this->validate() method to achieve server validation. Here is an example.

use Illuminate\Support\Facades\Config;
use Illuminate\Http\Request;

class BaseController extends Controller 
{
  /**
   * @param Request $request
   */
  public function postValidate(Request $request)
  {
    $result = $this->validate($request, [
      'geetest_challenge' => 'geetest',
    ], [
      'geetest' => Config::get('geetest.server_fail_alert')
    ]);
    if ($request) {
      return 'success';
    }
  }
} 

If we use Geetest, the form will post three extra parameters geetest_challenge geetest_validate geetest_seccode. Geetest use these three parameters to achieve server validation.

If you use ORM, we don't need to add these keys to Model, so you should add following in Model.

protected $guarded = ['geetest_challenge', 'geetest_validate', 'geetest_seccode'];

You can define alert text by altering server_fail_alert in config/geetest.php

Also you can use Request to achieve validation.

<?php namespace App\Http\Requests;
use App\Http\Requests\Request;

class ValidationRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'geetest_challenge' => 'geetest'
        ];
    }

    /**
     * Get validation messages.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'geetest' => 'Validation Failed'
        ];
    }
}

We can use it in our Controller by Request parameter.

use Illuminate\Support\Facades\Config;
use Illuminate\Http\Request;
use App\Http\Requests\ValidationRequest;

class BaseController extends Controller 
{
  /**
   * @param Request $request
   */
  public function postValidate(ValidationRequest $request)
  {
    // is Validate
  }
} 

Language

Geetest supports different language.

  • Simplified Chinese
  • Traditional Chinese
  • English
  • Japanese
  • Korean

You can configure it in config/geetest.php .

Here are key-values of Languge Configuration.

  • zh-cn (Simplified Chinese)
  • zh-tw (Traditional Chinese)
  • en (English)
  • ja (Japanese)
  • ko (Korean)

for example, If you want to use Korean, just change lang key to ko

'lang' => 'ko'

Contribution

If you find something wrong with this package, you can send an email to cqc@cuiqingcai.com

Or just send a pull request to this repository.

Pull Requests are welcome.

Author

Germey , from Beijing China

License

Laravel Geetest is licensed under The MIT License (MIT).

本帖已被设为精华帖!
本帖由 Summer 于 2年前 加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 11
Summer

file

一个验证码服务器的扩展包。

好东西,我加了一张图片,方便阅读此文章的人理解 :smile:

2年前

js报错,这个是什么问题?
GeeTest Error: request http://api.geetest.com/get.php?gt=xxxxx&challenge=xxxxx&product=float&offline=false〈=zh-cn can not access

2年前

@xyxu ID或者KEY没设置好吧?

2年前
Summer

话说,这个扩展包基本上应该是国人使用的吧?

你是在练英文么,建议使用中文书写文档哈 :laughing:

2年前
Summer

文档写的蛮好的,还是要赞一个,就是建议使用中文

2年前

@Germey 谢谢。 重新注册了号,然后就好了。

2年前

@Summer 哈哈哈哈,歪果仁也可以用呀,中文版也有哒,http://cuiqingcai.com/2988.html

2年前

支持下~~
好东西!

2年前

很不错呀!

2年前

谢谢

1个月前

  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,使用方法请见 Emoji 自动补全来咯,可用的 Emoji 请见 :metal: :point_right: Emoji 列表 :star: :sparkles:
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
  • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
  请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!