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

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. PS5穿越云层3D文字

    妈的,搜狗浏览器有时候会出问题,保存的内容找不到了…… 视图--显示参考线或者“显示额外内容”会取消或者加上参考线,后者功能更强一些,ctrl+D有时可以代替后者的功能,后者可以去掉蒙版的参考线,前者 ...

  2. protractor protractor.conf.js [launcher] Process exited with error code 1 undefined:1190

    y@y:karma-t01$ protractor protractor.conf.js [launcher] Process exited with error code undefined: vl ...

  3. h.264 率失真优化

    Rate Distortion Optimization 搜索时,一个不可避免的问题就是如何对mv进行比较,从而得到最优 对于同一压缩算法来说,码率越高表示图像质量越好.失真越小,但是码率越高要求更大 ...

  4. COJ 1008 WZJ的数据结构(八) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...

  5. 【动态规划】XMU 1588 01序列计数

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1588 题目大意: 给n1个0和n2个1,连续的0不超过k1个,连续的1不超过k2个.问 ...

  6. Java---文件的切割与合并,已经实现图形界面(工具)

    实现对任意文件的切割,实现对切割后的文件的合并. 上次只写了特定目录下的文件切割与合并,有点遗憾, 这次,我写了一个图形界面来实现对文件的切割与合并. 文件切割: 用户可以自己选择需要切割的文件, 软 ...

  7. HDOJ 1019 Least Common Multiple(最小公倍数问题)

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  8. JQuery Mobile页面的载入方式

    1.JQM页面结构 jQuery Mobile是通过data-role属性来区分渲染界面样式的,JQM里面提供的data-role如下表: 参数 说明 page 页面容器,其内部的mobile元素将会 ...

  9. Java并发编程:ThreadLocal

    Java并发编程:深入剖析ThreadLocal   Java并发编程:深入剖析ThreadLocal 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用 ...

  10. analytics.js

    Analytics.js简介   analytics.js JavaScript代码段是一种可用于衡量用户与您网站的互动情况的全新方式.它与之前的跟踪代码ga.js类似,但为开发者自定义实现方案提供了 ...