oauth2.0 php server,Oauth2.0 Server PHP 与tp5中的使用

####Oauth2.0 的4中授权模式介绍

1.授权码模式--对应的grant_type类型:authorization_code

2.客户端模式--grant_type类型:client_credentials

3.简化模式--grant_type类型:implicit

4.密码模式--grant_type类型:refresh_token

####tp5中使用外部类有三种方式

1.composer下载 这种方式下载的类支持自动加载 使用时只需要像use think\Request;一样 如果使用的类支持composer引入name推

荐这种方式 下载的包默认在vender目录下

2.extend目录是扩展包默认存放位置 可以把包下载下来放到extend目录下 然后需要注意命名空间

3.其他 不想考虑太多命名空间的问题 如支付宝的SDK 我们可以在项目目录下新建一个sdk文件夹 使用时可以通过require引入 或者直接在入口文件中引入 只要配置好路径~

这里通过github搜索oauth2.0或者通过https://github.com/bshaffer/oauth2-server-php 或者通过官网

https://oauth.net/code/ 来下载类库

从git上我们进入到官方文档可以看到composer安装方式 ,composer安装使用的很简单但是这里我们使用下extend方式,下载包放到entend目录(可以只复制src/OAuth2这个文件夹 其他的test测试文件夹不需要)

####演示

http://bshaffer.github.io/oauth2-server-php-docs/cookbook/ 文档中有详细的步骤

1.按照文档 首先是数据库的创建 可以直接从文档中复制 (实际上关于数据库创建的部分在OAuth2/Storage/Pdo.php文件下 构造方法中指明了表名 getBuildSql方法指明了表的字段)

注意:如果你在创建表时加入了表前缀 如tp_oauth2_client加入了表前缀tp_那么就需要在Pdo.php文件中把oauth2_client也改成tp_oauth2_client

2.创建server.php(我们选择在tp5/application/api模块下创建控制器Server.php 可以通过php think make:controller api/Server --plain创建)

```

namespace app\api\controller;

use think\Controller;

/**

* 创建OAuth2 Server对象

*/

class Server extends Controller

{

protected function oauthServer(){

//$dsn = 'mysql:dbname=oauth;host=localhost';

$type = config('database.type');

$database = config('database.database');

//$hostname = config('database.hostname');

$hostname = '127.0.0.1';

$dsn = $type . ':dbname=' . $database . ';host=' . $hostname;

$username = config('database.username');

$password = config('database.password');

// error reporting (this is a demo, after all!)

ini_set('display_errors',1);error_reporting(E_ALL);

\OAuth2\Autoloader::register();

// $dsn is the Data Source Name for your database, for exmaple

"mysql:dbname=my_oauth2_db;host=localhost"

$storage = new \OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' =>

$password));

// Pass a storage object or array of storage objects to the OAuth2 server class

$server = new \OAuth2\Server($storage);

// Add the "Client Credentials" grant type (it is the simplest of the grant types)

$server->addGrantType(new \OAuth2\GrantType\ClientCredentials($storage));

// Add the "Authorization Code" grant type (this is where the oauth magic happens)

$server->addGrantType(new \OAuth2\GrantType\AuthorizationCode($storage));

return $server;

}

/**

*验证access_token

*/

protected function checkOauth($server){

if (!$server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {

$server->getResponse()->send();

die('');

}

}

}

```

3.创建Token.php(同样在tp5/application/api模块下)

```

namespace app\api\controller;

use app\api\controller\Server;

class Token extends Server

{

private $server;

public function _initialize(){

$this->server = $this->oauthServer();

}

public function index(){

// Handle a request for an OAuth2.0 Access Token and send the response to the client

$this->server->handleTokenRequest(\OAuth2\Request::createFromGlobals())->send();

}

}

```

4.插入一条数据 进行测试

```

INSERT INTO oauth_clients (client_id, client_secret, redirect_uri,grant_types) VALUES ("testclient", "testpass",

"http://fake/","client_credentials");

```

打开cmd或者使用git运行curl -u testclient:testpass http://localhost/tp5Auth2/public/api/Token -d grant_type=client_credentials 成功后如图所示

如图:

![](/edituploads/20190107/bf548957ac5bfa5614e7faf8601b8e21.png)

或者使用post请求测试(注意一定是post请求) 这里我们使用postman工具进行测试 传递client_id,client_secret,grant_type参数

成功后如图

![](/edituploads/20190107/cf2c3c3687e2bcb05f229c90e81c1ef4.png)

5.通过获取的access_token进行数据请求测试

比如获取个人文章信息

api模块下新建Article.php

```

namespace app\api\controller;

use app\api\controller\Server;

class Article extends Server

{

private $server;

public function _initialize(){

$this->server = $this->oauthServer();

}

public function index(){

$this->checkOauth($this->server);//验证失败 则终止执行下面代码

//验证成功 这里写请求数据逻辑

//测试返回

return json(['code'=>200,'msg'=>'请求成功','data'=>'需要的数据']);

}

}

```

进行测试

首先不传入access_token 报错如图

![](/edituploads/20190107/44cea442c09370410bf4694e017120a9.png)

传入错误的access_token 报错如图

![](/edituploads/20190107/001b14a826f6aafba5f49e29bde7e9d4.png)

传入正确access_token(上面请求Token.php返回的access_token值) 返回如图

![](/edituploads/20190107/2dfac0893afcc6864385dad15bf30464.png)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值