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

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. 用java模拟银行柜台排队

    4年前在办理银行业务的时候,看到每个办理柜台窗口前都有很多人排队. 同时在那个时间段,我正好重温了数据结构这本书.好像里面有提到银行. 所以当时就用java写了一段小程序来模拟窗口排队的情况. 有兴趣 ...

  2. mvc vs iis默认页面

    有时候,再iis里面设置了web的默认页面index.html 希望跳转到这个页面index.html默认页面 而 mvc则跳转到路由里面的设置页面 怎么忽略这个呢. 设置路由可能是个好办法,能实现 ...

  3. temp gbk2utf8

    __author__ = 'root' # -*- coding: utf-8 -*- ps = '/data/poitestdata/行政地名.csv' pt = '/data/poitestdat ...

  4. hdu 4545 魔法串

    http://acm.hdu.edu.cn/showproblem.php?pid=4545 #include <cstdio> #include <cstring> #inc ...

  5. Delphi Ini 操作简单例子

    interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialog ...

  6. CentOS安装搜狗词库

    中文输入使用ibus-pinyin. 在ibus-pinyin里使用搜狗词库 # wget http://hslinuxextra.googlecode.com/files/sougou-phrase ...

  7. DateTime用法二

    任何项目,难免会碰到DateTime的显示问题,.net框架虽提供丰富多样的显示方法,但我很少使用,因老忘记细节,每次都要纠结到底月份在前还是年份在前:日期分隔符到底是“/”,还是“\”,还是“-”等 ...

  8. 【c#】Form调用百度地图api攻略及常见问题

    首先,在Form中调用百度地图api,我们需要使用webbrowser控件,这个在前面的文章中已经讲过了,可以参照(http://blog.csdn.net/buptgshengod/article/ ...

  9. Volley 百财帮封装

    Activity public class MainActivity extends Activity implements OnClickListener {     private Context ...

  10. 图片样式 scaleType 属性

    ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType),不同值的意义区别: