Api-2 用户注册接口
添加用户注册路由
$api->post('url','UsersController@store'); //用户注册
创建api请求验证类和控制器
php artisan make:controller name; php artisan make:request name
写入验证方法和用户创建
调用请求类和用户模型
- 首先请求类会验证key是否存在
Cache::get($request->verification_key); 获取缓存
- 我们只要在缓存中获取到key,如果获取到key那么验证码就已经失效了
if(!缓存){return $this->response->error('验证码已失效', 422);}
- 如果有验证码,匹配验证码
if (!hash_equals($verifyData['code'], $request->verification_code)){//返回验证码匹配失败,hash_equals可避开时序破解验证码}
- 匹配成功后,讲用户添加到数据库
User:create(['name']=>$request->name,..,...,)
- 清除验证码 返回OK
Cache::forget($request->..);
POSTMAN测试
模拟获取key
模拟用户注册提交
数据库检测用户
api-3 防止恶意攻击调用接口
限制api接口调用
中间件api.throttle控制调用频率
$api->version('v1', [
'namespace' => 'App\Http\Controllers\Api',
], function($api) {
$api->group([
'middleware' => 'api.throttle',
'limit' => 1,
'expires' => 1,
], function($api) {
// 短信验证码
$api->post('verificationCodes', 'VerificationCodesController@store')
->name('api.verificationCodes.store');
// 用户注册
$api->post('users', 'UsersController@store')
->name('api.users.store');
});
});
测试
config api文件配置
config配置
'rate_limits' => [
// 访问频率限制,次数/分钟
'access' => [
'expires' => env('RATE_LIMITS_EXPIRES', 1),
'limit' => env('RATE_LIMITS', 60),
],
// 登录相关,次数/分钟
'sign' => [
'expires' => env('SIGN_RATE_LIMITS_EXPIRES', 1),
'limit' => env('SIGN_RATE_LIMITS', 10),
],
],
api 配置 ‘limit’ => config(‘api.rate_limits.sign.limit’),
声明:本文为原创作品,版权归作者所有。未经许可,不得转载或用于任何商业用途。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
谢谢大佬指导
api必须加密做限制
[aru_52]好