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

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. Ural 1099 Work Scheduling

    http://acm.timus.ru/problem.aspx?space=1&num=1099 题意:有n个人,很多对合作关系,每个人只能和一个人合作,求最多能选出多少人. 一般图匹配 # ...

  2. 导出数据到excel

    Protected Sub cmdOrderExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdOrderExp ...

  3. Keil C51里面lib文件生成和调用方法

    一.包含关系 LCD1602.C里面包含LCD1602.H LCD1602.H的文件格式 二.设置生成lib文件 三.Lib文件调用 添加lib文件对话框 添加后的lib文件 呵呵^_^,这样就可以删 ...

  4. [Delphi]检查URL是否有效的函数

    function CheckUrl(url: string): boolean; var hSession, hfile, hRequest: hInternet; dwindex, dwcodele ...

  5. POJ 1511 SPFA+邻接表 Invitation Cards

    题目大意: 计算从 1 点 到 其他所有点的 往返距离之和, 因为是 有向图, 所以我们需要将图反存 一次, 然后求两次单源最短路, 结果就出来了. #include <iostream> ...

  6. xamarin提供在线检查.net代码是否支援xamarin,ios,android

    大概是多少比你想的更多的移动.我们很高兴地宣布一个新的在线服务Xamarin NET移动扫描仪-扫描您的编译.NET代码的兼容性与Xamarin.iOSXamarin.Android是,Windows ...

  7. Apache-Tika解析PDF文档

    通常在使用爬虫时,爬取到网上的文章都是各式各样的格式处理起来比较麻烦,这里我们使用Apache-Tika来处理PDF格式的文章,如下: package com.mengyao.tika.app; im ...

  8. Controlling How NSThread and NSRunLoop Exit

    http://shaheengandhi.com/controlling-thread-exit/ While most concurrency can be dealt with by using ...

  9. L - Vases and Flowers - hdu 4614(区间操作)

    题意:有两种操作,第一种从A开始插花,如果有花就跳到下一个,然后输出最后一个花瓶的编号,如果花瓶不够把多余的花丢掉.操作2把区间清空 分析:很明显的线段树操作,就是插花的时候麻烦一下,需要先找出来他剩 ...

  10. Tomcat部署问题及解决方法

    1.gradle构建web时报错:Access restriction: The type JPEGImageEncoder is not accessible due to restriction ...