Laravel-admin 登录添加验证码

Laravel-admin 登录添加验证码

最近在用laravel-amdin搭建后台,记录一下

一. 按照验证码库

  1. 引入: composer require mews/captcha

  2. 修改config/app.php

    'providers' => [
        // ...
        Mews\Captcha\CaptchaServiceProvider::class,
    ]   
    
     'aliases' => [
        // ...
        'Captcha' => Mews\Captcha\Facades\Captcha::class,
    ]
  3. php artisan vendor:publish

  4. 修改config/captcha.php的default

return [
            'default'   => [
                'length'    => 5,
                'width'     => 120,
                'height'    => 36,
                'quality'   => 90,
            ],
            // ...
        ];

二. 修改登录方法

修改app/Admin/Controllers/AuthController.php,如没有则复制vendor/encore/laravel-admin/src/Controllers/AuthController.php到app/Admin/Controllers/AuthController.php,修改代码如下:


<?php
namespace App\Admin\Controllers;
use Encore\Admin\Controllers\AuthController as BaseAuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
class AuthController extends BaseAuthController
{
    public function getLogin()
    {
        if (!Auth::guard('admin')->guest()) {
            return redirect(config('admin.route.prefix'));
        }
        return view('admin.login');
    }

    public function postLogin(Request $request)
    {
        $credentials = $request->only(['username', 'password','captcha']);
        $validator = Validator::make($credentials, [
            'username' => 'required',
            'password' => 'required',
            'captcha' => 'required|captcha'
        ]);
        if ($validator->fails()) {
            return Redirect::back()->withInput()->withErrors($validator);
        }
        unset($credentials['captcha']);
        if (Auth::guard('admin')->attempt($credentials)) {
            admin_toastr(trans('admin.login_successful'));
            return redirect()->intended(config('admin.route.prefix'));
        }
        return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);
}

    protected function getFailedLoginMessage()
    {
        return Lang::has('auth.failed')
        ? trans('auth.failed')
        : 'These credentials do not match our records.';
        }
    }

四、修改页面:

复制vendor/encore/laravel-admin/resources/views/login.blade.php到resources/views/admin/login.blade.php
在密码与记住我代码块中间添加代码

、、、

<div class="form-group has-feedback {!! !$errors->has('password') ?: 'has-error' !!}">
    @if($errors->has('password'))
        @foreach($errors->get('password') as $message)
            <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}}</label><br>
        @endforeach
    @endif
        <input type="password" class="form-control" placeholder="{{ trans('admin.password') }}" name="password">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>

<!-- 在这里添加代码  start-->
<div class="row">
    <div class="form-group has-feedback {!! !$errors->has('captcha') ?: 'has-error' !!}">
        @if($errors->has('captcha'))
            @foreach($errors->get('captcha') as $message)
                <label class="control-label" for="inputError" style="margin-left: 15px"><i class="fa fa-times-circle-o">{{$message}}</i></label></br>
            @endforeach
        @endif
        <input type="text" class="form-control" style="display: inline;width: 55%; margin-left: 15px" placeholder="{{ trans('admin.captcha') }}" name="captcha">
        <span class="glyphicon glyphicon-refresh form-control-feedback captcha" style="right:39%;z-index: 100"></span>
        <img class="captcha" src="{{ captcha_src('admin') }}">
    </div>
</div>
<!-- 在这里添加代码  end-->

<div class="row">
    <div class="col-xs-8">
    @if(config('admin.auth.remember'))

    、、、

完成

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 5

有轮子 https://github.com/xiaoxuan6/login-captcha

1年前 评论
july1115 (楼主) 1年前
wgang 1年前
bug_zs

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at admin@php.cn to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

你们不提示这个嘛 我是严格按照上面写的

4个月前 评论
july1115 (楼主) 4个月前
bug_zs (作者) 4个月前
bug_zs

if (!Auth::guard('admin')->guest()) { return redirect(config('admin.route.prefix')); }

这个是干嘛的 查什么的 我是第一次用 董的不是很多

4个月前 评论
july1115 (楼主) 4个月前
bug_zs (作者) 4个月前
july1115 (楼主) 4个月前
bug_zs (作者) 4个月前
bug_zs (作者) 4个月前
bug_zs

$validator = Validator::make($credentials, [ 'username' => 'required', 'password' => 'required', 'captcha' => 'required|captcha' ], [ 'captcha.required' => '请填写验证码', 'captcha.captcha' => '验证码错误', 'exists' => '该用户已停用!', ]); 这样提示就更好了

4个月前 评论

唯一的不足就是没有验证码的点击刷新触发,我把那个刷新的类样式去掉了,感觉也不是很需要,看上去还不错

            <!-- 在这里添加代码  start-->
            <div class="row">
                <div class="form-group has-feedback {!! !$errors->has('captcha') ?: 'has-error' !!}">
                    @if($errors->has('captcha'))
                        @foreach($errors->get('captcha') as $message)
                            <label class="control-label" for="inputError" style="margin-left: 15px"><i
                                    class="fa fa-times-circle-o">{{$message}}</i></label></br>
                        @endforeach
                    @endif
                    <input type="text" class="form-control" style="display: inline;width: 55%; margin-left: 15px"
                           placeholder="{{ trans('admin.captcha') }}" name="captcha">
                    <span class="glyphicon form-control-feedback captcha" style="right:39%;z-index: 100"></span>
                    <img class="captcha" id="code_img" onclick="changeCode()" src="{{ captcha_src('admin') }}">
                </div>
            </div>
            <!-- 在这里添加代码  end-->
``````javascript
    function changeCode() {
        var that = document.getElementById('code_img');
        that.src = that.src + '&' + "{{captcha_src('admin')}}";
    }
3个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!