larave-admin重构后台登陆,添加验证码验证
2018年9月3日1复制控制器代码并修改
复制vendor/encore/laravel-admin/src/Controllers/AuthController.php
到app/Admin/Controllers/AuthController.php
,修改代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php namespace App\Admin\Controllers; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Validator; class AuthController extends Controller { /** * Login page. * * @return \Illuminate\Contracts\View\Factory|Redirect|\Illuminate\View\View */ public function getLogin() { if (!Auth::guard('admin')->guest()) { return redirect(config('admin.route.prefix')); } return view('admin.login'); } /** * @param Request $request * * @return mixed */ 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()]); } /** * @return string|\Symfony\Component\Translation\TranslatorInterface */ protected function getFailedLoginMessage() { return Lang::has('auth.failed') ? trans('auth.failed') : 'These credentials do not match our records.'; } } |
2 添加错误提示语言
在resources/lang/zh-CN/validation.php中添加
1 2 3 4 5 |
'captcha' => ':attribute 错误', 'attributes' => [ 'captcha' => '验证码', //... ] |
3.复制视图文件并修改
复制vendor/encore/laravel-admin/resources/views/login.blade.php
到resources/views/admin/login.blade.php
修改内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>{{config('admin.title')}} | {{ trans('admin.login') }}</title> <!-- Tell the browser to be responsive to screen width --> <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> <!-- Bootstrap 3.3.5 --> <link rel="stylesheet" href="{{ admin_asset("/vendor/laravel-admin/AdminLTE/bootstrap/css/bootstrap.min.css") }}"> <!-- Font Awesome --> <link rel="stylesheet" href="{{ admin_asset("/vendor/laravel-admin/font-awesome/css/font-awesome.min.css") }}"> <!-- Theme style --> <link rel="stylesheet" href="{{ admin_asset("/vendor/laravel-admin/AdminLTE/dist/css/AdminLTE.min.css") }}"> <!-- iCheck --> <link rel="stylesheet" href="{{ admin_asset("/vendor/laravel-admin/AdminLTE/plugins/iCheck/square/blue.css") }}"> <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="//oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script> <script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body class="hold-transition login-page" @if(config('admin.login_background_image'))style="background: url({{config('admin.login_background_image')}}) no-repeat;background-size: cover;"@endif> <div class="login-box"> <div class="login-logo"> <a href="{{ admin_base_path('/') }}"><b>{{config('admin.name')}}</b></a> </div> <!-- /.login-logo --> <div class="login-box-body"> <p class="login-box-msg">{{ trans('admin.login') }}</p> <form action="{{ admin_base_path('auth/login') }}" method="post"> <div class="form-group has-feedback {!! !$errors->has('username') ?: 'has-error' !!}"> @if($errors->has('username')) @foreach($errors->get('username') as $message) <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}} </label></br> @endforeach @endif <input type="input" class="form-control" placeholder="{{ trans('admin.username') }}" name="username" value="{{ old('username') }}"> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div> <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> <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"><i class="fa fa-times-circle-o"></i>{{$message}} </label></br> @endforeach @endif <input type="text" class="form-control" style="display: inline;width: 60%" placeholder="{{ trans('admin.captcha') }}" id="captcha" name="captcha"> <img style="display: inline;width: 37%;height: 36px;float: right" class="captcha" src="{{ captcha_src('admin') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片重新获取验证码"> </div> <div class="row"> <!-- /.col --> <div class="col-xs-4 col-md-offset-4"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <button type="submit" class="btn btn-primary btn-block btn-flat">{{ trans('admin.login') }}</button> </div> <!-- /.col --> </div> </form> </div> <!-- /.login-box-body --> </div> <!-- /.login-box --> <!-- jQuery 2.1.4 --> <script src="{{ admin_asset("/vendor/laravel-admin/AdminLTE/plugins/jQuery/jQuery-2.1.4.min.js")}} "></script> <!-- Bootstrap 3.3.5 --> <script src="{{ admin_asset("/vendor/laravel-admin/AdminLTE/bootstrap/js/bootstrap.min.js")}}"></script> <!-- iCheck --> <script src="{{ admin_asset("/vendor/laravel-admin/AdminLTE/plugins/iCheck/icheck.min.js")}}"></script> <script> $(function () { $('input').iCheck({ checkboxClass: 'icheckbox_square-blue', radioClass: 'iradio_square-blue', increaseArea: '20%' // optional }); }); </script> </body> </html> |
4.设置路由
app/Admin/routes.php中新增
1 2 |
$router->get('auth/login', 'AuthController@getLogin'); #重构登陆页面 $router->post('auth/login', 'AuthController@postLogin'); #重构登陆验证页面 |