Compare commits
No commits in common. "ca95ebd4d63e9e8e98cfc723adc043070ba2b2fe" and "f0dc08c21c6bf94de7d408ab133d90f52c7404b6" have entirely different histories.
ca95ebd4d6
...
f0dc08c21c
|
|
@ -4,7 +4,6 @@ namespace App\Http\Controllers;
|
|||
|
||||
use App\Models\AdminPermission;
|
||||
use App\Models\AdminUser;
|
||||
use App\Services\CaptchaService;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
|
@ -16,19 +15,13 @@ class AuthController extends Controller
|
|||
) {
|
||||
}
|
||||
|
||||
public function login(Request $request, CaptchaService $captchaService)
|
||||
public function login(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'username' => 'required',
|
||||
'password' => 'required',
|
||||
'captcha_key' => 'required',
|
||||
'captcha_value' => 'required',
|
||||
]);
|
||||
|
||||
if (! $captchaService->testPhrase($request->input('captcha_key'), $request->input('captcha_value'))) {
|
||||
return $this->error('验证码错误');
|
||||
}
|
||||
|
||||
$username = $request->input('username');
|
||||
|
||||
$user = AdminUser::where(['username' => $username])->first();
|
||||
|
|
|
|||
|
|
@ -1,71 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\CaptchaService;
|
||||
use Gregwar\Captcha\CaptchaBuilder;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CaptchaController extends Controller
|
||||
{
|
||||
/**
|
||||
* 创建图形验证码
|
||||
*/
|
||||
public function store(Request $request, CaptchaService $captchaService)
|
||||
{
|
||||
$request->validate([
|
||||
'key' => ['bail', 'filled', 'string', 'max:32'],
|
||||
'w' => ['bail', 'int'],
|
||||
'h' => ['bail', 'int'],
|
||||
]);
|
||||
|
||||
$builder = $this->builder();
|
||||
$builder->build($request->input('w', 150), $request->input('h', 40));
|
||||
|
||||
$captchaService->put(
|
||||
$key = $request->input('key', Str::random(16)),
|
||||
$builder->getPhrase()
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'key' => $key,
|
||||
'img' => $builder->inline(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看图形验证码
|
||||
*
|
||||
* @param string $key
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Services\CaptchaService $captchaService
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($key, Request $request, CaptchaService $captchaService)
|
||||
{
|
||||
$builder = $this->builder();
|
||||
$builder->build(
|
||||
(int) $request->query('w', 150),
|
||||
(int) $request->query('h', 40),
|
||||
);
|
||||
|
||||
if (strlen($key) <= 32) {
|
||||
$captchaService->put($key, $builder->getPhrase());
|
||||
}
|
||||
|
||||
return response($builder->get())
|
||||
->header('Content-Type', 'image/jpeg')
|
||||
->header('Cache-Control', 'no-cache');
|
||||
}
|
||||
|
||||
/**
|
||||
* 图形验证码生成器
|
||||
*
|
||||
* @return \Gregwar\Captcha\CaptchaBuilder
|
||||
*/
|
||||
protected function builder(): CaptchaBuilder
|
||||
{
|
||||
return new CaptchaBuilder(Str::random(5));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\BizException;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
|
||||
class CaptchaService
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $cachePrefix = 'captchas_';
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Contracts\Cache\Repository $cache
|
||||
*/
|
||||
public function __construct(
|
||||
protected Cache $cache,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将验证码存入缓存中
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $phrase
|
||||
* @param integer $ttl
|
||||
* @return void
|
||||
*/
|
||||
public function put(string $key, string $phrase, int $ttl = 300): void
|
||||
{
|
||||
$this->cache->put($this->cacheKey($key), $phrase, $ttl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验验证码是否正确
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $phrase
|
||||
* @return bool
|
||||
*/
|
||||
public function testPhrase(string $key, string $phrase): bool
|
||||
{
|
||||
if ($phrase === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$value = (string) $this->cache->pull(
|
||||
$this->cacheKey($key)
|
||||
);
|
||||
|
||||
return strtolower($value) === strtolower($phrase);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验验证码是否正确
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $phrase
|
||||
* @return void
|
||||
*
|
||||
* @throws \App\Exceptions\BizException
|
||||
*/
|
||||
public function validatePhrase(string $key, string $phrase): void
|
||||
{
|
||||
if (! $this->testPhrase($key, $phrase)) {
|
||||
throw new BizException(__('Invalid captcha'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码缓存的 key
|
||||
*
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function cacheKey(string $key): string
|
||||
{
|
||||
return $this->cachePrefix.$key;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,6 @@
|
|||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "^8.0.2",
|
||||
"gregwar/captcha": "^1.2",
|
||||
"guzzlehttp/guzzle": "^7.2",
|
||||
"laravel/framework": "^9.19",
|
||||
"laravel/sanctum": "^3.0",
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "9aeb8a01dd24f14cb9b41ce5d214de3f",
|
||||
"content-hash": "d676aa18ac28c870bc3707e5247cdbe0",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
|
|
@ -981,69 +981,6 @@
|
|||
],
|
||||
"time": "2022-07-30T15:56:11+00:00"
|
||||
},
|
||||
{
|
||||
"name": "gregwar/captcha",
|
||||
"version": "v1.2.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Gregwar/Captcha.git",
|
||||
"reference": "6e5b61b66ac89885b505153f4ef9a74ffa5b3074"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/Gregwar/Captcha/zipball/6e5b61b66ac89885b505153f4ef9a74ffa5b3074",
|
||||
"reference": "6e5b61b66ac89885b505153f4ef9a74ffa5b3074",
|
||||
"shasum": "",
|
||||
"mirrors": [
|
||||
{
|
||||
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
|
||||
"preferred": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"ext-gd": "*",
|
||||
"ext-mbstring": "*",
|
||||
"php": ">=5.3.0",
|
||||
"symfony/finder": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6.4"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Gregwar\\": "src/Gregwar"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Grégoire Passault",
|
||||
"email": "g.passault@gmail.com",
|
||||
"homepage": "http://www.gregwar.com/"
|
||||
},
|
||||
{
|
||||
"name": "Jeremy Livingston",
|
||||
"email": "jeremy.j.livingston@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "Captcha generator",
|
||||
"homepage": "https://github.com/Gregwar/Captcha",
|
||||
"keywords": [
|
||||
"bot",
|
||||
"captcha",
|
||||
"spam"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/Gregwar/Captcha/issues",
|
||||
"source": "https://github.com/Gregwar/Captcha/tree/v1.2.0"
|
||||
},
|
||||
"time": "2023-03-24T22:12:41+00:00"
|
||||
},
|
||||
{
|
||||
"name": "guzzlehttp/guzzle",
|
||||
"version": "7.6.0",
|
||||
|
|
|
|||
|
|
@ -17,9 +17,6 @@ use Illuminate\Support\Facades\Route;
|
|||
|
||||
Route::post('auth/login', [AuthController::class, 'login']);
|
||||
|
||||
Route::post('captchas', [CaptchaController::class, 'store']);
|
||||
Route::get('captchas/{captcha}', [CaptchaController::class, 'show']);
|
||||
|
||||
Route::group([
|
||||
'middleware' => ['auth:sanctum'],
|
||||
], function () {
|
||||
|
|
|
|||
Loading…
Reference in New Issue