lareval 实现jwt的步骤

Composer安装jwt扩展包:

1.composer require tymon/jwt-auth

安装完成后,需要在config/app.php中注册相应的服务提供者

2. Tymon\JWTAuth\Providers\LaravelServiceProvider::class,

然后注册需要用到的对应门面:

'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class

'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class

然后发布相应配置文件:

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

最后生成密钥:php artisan jwt:secret

3.修改config/auth.php下的为这个

'api' => [
    'driver' => 'jwt',
    'provider' => 'admin_users',//需要改成对应的表名
],
'providers' => [
    'admin_users' => [
        'driver' => 'myeloquent',
        'model' =>  App\Model\User::class,//需要对应的对应的用户的model
    ],

3.1 修改对应的用户的Model

class User extends Authenticatable implements JWTSubject,加上下面的2个方法
public function getJWTIdentifier()
{
    // TODO: Implement getJWTIdentifier() method.
    return $this->getKey();
}

/**
 * Return a key value array, containing any custom claims to be added to the JWT.
 *
 * @return array
 */
public function getJWTCustomClaims()
{
    // TODO: Implement getJWTCustomClaims() method.
    return [];
}

 

4.在Providers增加了一个重写的文件AuthServiceProvider.php文件 ,在boot()的这个方法增加了

\Auth::provider('myeloquent', function ($app, $config) {
    return new MyEloquentUserProvider($this->app['hash'], $config['model']);
});

其中的方法实现具体的登陆的实现

  /**
   * Validate a user against the given credentials.
   *
   * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
   * @param  array  $credentials
   * @return bool
   */
  public function validateCredentials(UserContract $user, array $credentials)
  {
      $name = $credentials['name'];
      $password = $credentials['password'];
      try {
          $data = DB::table('bbn_users')->where('name', $name)->first();
          if ($data == null) {
              throw new Exception('用户不存在', 5002);
          }

          if ($data->password != md5($password)) {
              throw new Exception('密码错误', 5003);
          }

          //写入用户的访问ip
          $user_ip = $_SERVER["REMOTE_ADDR"];
          WbUsersIp::addUserIp($data->id,$user_ip);*/

          return true;
      } catch (Exception $e) {
          \Log::info("login error msg:".$e->getCode().'##'.$e->getMessage());
          return false;
      }
  }

5. 调用的登陆的方法login的具体实现

public function login(Request $request)
{
    $credentials = $request->only('name', 'password');
    //$this->validateLogin($request);
    if ($token = auth('api')->attempt($credentials)) {
        $request->session()->regenerate();
        $this->clearLoginAttempts($request);
        $user = auth('api')->user();
        $data = [
            'token' => $token,
            'nickname' => $user->nick_name ? $user->nick_name : substr_replace($user->name, '****', 3, 4),
            'company' => $user->company,
            'mobile' => $user->name,
            'rsa_mobile' => substr_replace($user->name, '****', 3, 4)
        ];

        $this->successInfo['data']['error_code'] = 200;
        $this->successInfo['data']['success'] = 'true';
        $this->successInfo['data']['msg'] = '登录成功';
        $this->successInfo['data']['data'] = $data;
        return $this->successInfo;

        //return echoSuccessInfo(200, '登录成功', $data);
    }

    return echoErrorInfo(401, '账号或密码错误');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值