按照官方文档进行认证 发现不管怎么样都是失败

if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
// 用户状态永久保存...
}

研究他的源代码 Auth定义在 vendor/laravel/framework/src/Illuminate/Auth 

attempt方法在Guard.php 这个方法最关键的就是 $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); 这一句

$credentials就是我们上面传进来的数组了 此时var_dump($user) 这个变量是正确 有值的 所以也就是if ($this->provider->validateCredentials($user, $credentials)) 这句验证没通过

$provider 这个属性的定义

    /**
* The user provider implementation.
*
* @var \Illuminate\Auth\UserProviderInterface
*/
protected $provider;
UserProviderInterface 是一个抽象类
联系到 config/auth.php 中 driver 项有 Supported: "database", "eloquent" 所以 DatabaseUserProvider 或 EloquentUserProvider就是具体的实现了
validateCredentials 方法定义
	/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Auth\UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserInterface $user, array $credentials)
{
$plain = $credentials['password']; return $this->hasher->check($plain, $user->getAuthPassword());
}

$hasher 在 Illuminate\Hashing\HasherInterface

官方文档有这一句 The Laravel Hash class provides secure Bcrypt hashing: 也就是 BcryptHasher类的check方法

	/**
* Check the given plain value against a hash.
*
* @param string $value
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function check($value, $hashedValue, array $options = array())
{
return password_verify($value, $hashedValue);
}

用的是password_verify 跟我们常用的md5什么的完全不搭 难怪失败  

找到原因但在官方文档没找到解决的方法 无奈用 auth attempt md5  password hash等关键字google 才发现其实是有文档的  可见细心是多么重要

http://laravel.com/docs/extending#authentication

接着自己新建一个类 class HeyliUserProvider implements UserProviderInterface  密码验证方法validateCredentials就可以完全按照自己的业务逻辑了

Auth::extend('heyli',function($app) extend的第一个参数也就是验证方式的名称了 把 config/auth.php中的driver变成相对应的就可以了

PS:当你用户表主键不是 ID或密码字段不是password 的时候就会出错了  return new GenericUser((array) $user);   GenericUser类有下面方法

    /**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->attributes['id'];
} /**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->attributes['password'];
}

此时在HeyliUserProvider 这个类中的 retrieveById retrieveByCredentials 这两个方法 添加

if ( ! is_null($user))
{
$user->id = (string) $user->uid;  //添加这一句 像我的主键是UID  
return new GenericUser((array) $user);
}

最后记得在 app/start/global.php 添加上

Auth::extend('heyli', function($app) {
$provider = new \Example\Auth\HeyliUserProvider(); return new \Illuminate\Auth\Guard($provider, App::make('session.store'));
});

关于laravel框架的Auth::attempt验证失败的更多相关文章

  1. [麦先生]在Laravel框架里实现邮箱验证---发送邮件

    在经过一段时间的使用后,发现在项目中很多地方需要用到用户验证,以短信验证和邮箱验证为主流趋势,小麦总结了如果在Laravel框架中实现发送邮件功能,以后会陆续更上如何实现短信验证..... 在.env ...

  2. laravel框架中注册信息验证

    .路由配置 <?php Route::. 控制器分配页面及验证表单提交内容 <?php .form 表单验证 {{ Form::open(array().slideUp();   < ...

  3. 关于脱离laravel框架使用Illuminate/Validation验证器

    1.关于Illuminate/Validation验证器 Validation 类用于验证数据以及获取错误消息. github地址:github.com/illuminate/validation 文 ...

  4. Laravel框架 -- Validator 可用的验证规则

    accepted 字段值为 yes, on, 或是 1 时,验证才会通过.这在确认"服务条款"是否同意时很有用. active_url 字段值通过 PHP 函数 checkdnsr ...

  5. jenkins Auth fail验证失败

    重新设置密码

  6. [麦先生]Laravel框架实现发送短信验证

    今天在做到用户注册和个人中心的安全管理时,我借助实现第三方短信平台在Laravel框架中进行手机验证的设置;  由于我们做的是一个为客户提供医疗咨询和保健品网站,所以对客户个人隐私的保护显得尤为重要, ...

  7. laravel 框架登录 实际操作

    //登录中间件 Route::group(['middleware'=>'checkage'],function (){ Route::get('/mou/list','MouControlle ...

  8. Laravel 用户验证Auth::attempt fail的问题

    1.在laravel项目中,当使用Auth::attempt()用于用户验证时,Auth::attempt()会把密码通过Hash进行转换,变成一串不知啥的长字符,如果你在数据库里事先设置了明文的密码 ...

  9. Laravel 5.2服务----用户验证Auth相关问题

    关于laravel的auth()用户认证这一块,面前我也是,有用到,有碰到什么问题我就记录下来. 手动认证用户 <?php namespace App\Http\Controllers; use ...

随机推荐

  1. centos7 jsoup java.net.UnknownHostException

    [root@localhost ~]# vi /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.loc ...

  2. redis 基本使用

    Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...

  3. MapReduce Shuffle And Sort

    引言   MapReduce作出保证:进入每个Reducer的数据行都是有序的(根据数据行的键值进行排序).MapReduce将Mapper的输出进行排序并传递给Reducer作为输入的过程称为Shu ...

  4. Delphi TcxTreeList 读取 TcxImageComboBoxItem类型的值

    Delphi  TcxTreeList 读取  TcxImageComboBoxItem类型的值: Node.Values[wiNodeLevel.ItemIndex]://值 Node.Texts[ ...

  5. HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  6. jquery 随机数

    var jschars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', ...

  7. ssh远程连接不上ubuntu

    问题描述: 1.ubuntu安装了openssh-server,启动了ssh 守护进程,使用端口22 2.在本机可以 ssh 127.0.0.1 连接 3.通过ssh远程(比如用putty 或crt) ...

  8. nginx+keepalived实现nginx双主高可用的负载均衡

    http://kling.blog.51cto.com/3320545/1253474 一.前言: 在互联网上面,网站为用户提供原始的内容访问,同时为用户提供交互操作.提供稳定可靠的服务,可以给用户带 ...

  9. pat 1049. Counting Ones (30)

    看别人的题解懂了一些些    参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...

  10. Scala学习笔记之:tuple、array、Map

    [TOC] 本文<快学Scala>的笔记 tuple学习笔记 tuple的定义 对偶是元组(tuple)的最简单形态--元组是不同类型的值的聚集. 元组的值是通过将单个值包含在圆括号中构成 ...