laravel-admin 登录加图形验证码

引入

composer require mews/captcha

修改

在 config/app.php 下修改

'providers' => [
    // ...
    Mews\Captcha\CaptchaServiceProvider::class,
]   

 'aliases' => [
    // ...
    'Captcha' => Mews\Captcha\Facades\Captcha::class,
]

运行 php artisan vendor:publish

修改

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'))

在登录页面下添加js代码,点击验证码就可以刷新了

<script type="text/javascript" src="https://php-acad.28sjw.com/Statics/Assets/js/jquery.min-3.2.1.js"></script>
<script type="text/javascript">
    $(function(){
        var url=$('img').attr('src');
        $('img').click(function(){
            $(this).attr('src',url+Math.random())
        })
    });
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值