您好,欢迎访问一九零五行业门户网

ThinkPHP5中使用 Auth2进行验证的过程分析

本篇文章给大家分享的内容是关于thinkphp5中使用 auth2进行验证的过程分析,有需要的朋友可以参考一下,希望能帮助到大家。
在tp上实现的auth2验证的,在网上发现笔记很少, 不像yii, 故在此发表一下笔记,用来帮助有相关需求的朋友
ps: 鉴于oauth2有四种方案, 本实例是基于 客户端凭证 实现,其他三种就不讲述了
一、通过composer安装
composer require --prefer-dist bshaffer/oauth2-server-php
安装完成后,如图:
会出现相关的目录
二、实现授权文件
1) 创建对应的数据表
首先找到 pdo.php文件,如图:
然后找到该位置
目的,是告诉你创建表时的名称,应该和这里使用的表名称一致
关于创建的表,我直接上代码,方便各位可以直接复制粘贴:
create table oauth_access_tokens (access_token varchar(40) not null,client_id varchar(80) not null,user_id int(11) default null,expires varchar(19) not null,scope text, primary key (access_token), key fk_access_token_oauth2_client_client_id (client_id), key ix_access_token_expires (expires), constraint fk_access_token_oauth2_client_client_id foreign key (client_id) references pos_oauth2_client (client_id) on delete cascade on update cascade) engine=innodb default charset=utf8;
create table oauth_authorization_codes (authorization_code varchar(40) not null,client_id varchar(80) not null,user_id int(11) default null,redirect_uri text not null,expires int(11) not null,scope text, primary key (authorization_code), key fk_authorization_code_oauth2_client_client_id (client_id), key ix_authorization_code_expires (expires), constraint fk_authorization_code_oauth2_client_client_id foreign key (client_id) references pos_oauth2_client (client_id) on delete cascade on update cascade) engine=innodb default charset=utf8;
create table oauth_clients (client_id varchar(80) not null,client_secret varchar(80) not null,redirect_uri text not null,grant_type text,scope text,created_at int(11) default null,updated_at int(11) default null,created_by int(11) default null,updated_by int(11) default null, primary key (client_id)) engine=innodb default charset=utf8;
create table oauth_refresh_tokens (refresh_token varchar(40) not null,client_id varchar(80) not null,user_id int(11) default null,expires int(11) not null,scope text, primary key (refresh_token), key fk_refresh_token_oauth2_client_client_id (client_id), key ix_refresh_token_expires (expires), constraint fk_refresh_token_oauth2_client_client_id foreign key (client_id) references pos_oauth2_client (client_id) on delete cascade on update cascade) engine=innodb default charset=utf8;
create table oauth_scopes (scope text,is_default tinyint(1) default null) engine=myisam default charset=utf8;
添加一条数据
insert into oauth_clients(client_id,client_secret,redirect_uri,grant_type,scope,created_at,updated_at,created_by,updated_by) values ('admin','123456','http://','client_credentials',null,null,null,null,null);
ps,说明一下,如图:
在我实际使用中,只使用到这五张表,也就是上面创建的五张表,在这个config里面,剩下的几个选项我是全部 注销掉了的
另外还有一个情况,说明一下: 有可能各位,对数据表设置了表前缀, 也是需要在此进行相关修改的, 比如我创建的,见图:
所以我进行了相关的修改:
2) 创建授权文件 oauth2.php, 名字随便自己取
<?phpnamespace appcommon;/**@author jinyan@create 20180416*/use oauth2storagepdo;use thinkconfig;class oauth2{
/** * @register new oauth2 apply * @param string $action * @return boolean|\oauth2\server */function granttypeoauth2($action=null){ config::load(app_path.'database.php'); $storage = new pdo( [ 'dsn' => config('dsn'), 'username' => config('username'), 'password' => config('password') ] ); $server = new \oauth2\server($storage, array('enforce_state'=>false)); // 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)); // add the "user credentials" grant type (this is where the oauth magic happens) $server->addgranttype(new \oauth2\granttype\usercredentials($storage)); return $server;}/** * @校验token值 * @param unknown $server */protected function checkapiauthroize($server){ if (!$server->verifyresourcerequest(\oauth2\request::createfromglobals())) { $server->getresponse()->send(); exit; }}
}?>

3) 创建token文件, access.php
<?phpnamespace apprestfulcontroller;use appcommonoauth2;/**@uathor:jinyan*/class access extends oauth2{
protected $_server;/** * @授权配置 */public function __construct(){ return $this->_server = $this->granttypeoauth2();}/** * */private function _token(){ // handle a request for an oauth2.0 access token and send the response to the client $this->_server->handletokenrequest(\oauth2\request::createfromglobals())->send('json', 'oauth2_');}/** * @get access_token */public function access_token(){ $this->_token();}
}?>

那么如何请求一个access_token的值呢? 直接调用这个 acccess_token()的方法即可
request url: http://restful.thinkphp.com/r...
还请得之前创建数据表时,有添加了一条新数据吗? 其作用就是相当于用来获取access_token的账号密码之类的, 记得需要使用 post方式获取token
请求的参数
{client_id=adminclient_secret=123456grant_type=client_credentials //这个参数是固定的}
如果请求成功的话,会返回如下图所示:
贴上,通过ff浏览器httprequest的请求界面:
4) 通过 access_token 获取接口数据 ,sms.php
<?phpnamespace apprestfulcontroller;/**created by phpstorm.user: administratordate: 2018/7/29time: 22:02*/use appcommonoauth2;class sms extends oauth2{protected $_server;/** * @授权配置 */public function __construct(){ $this->_server = $this->granttypeoauth2();}public function test(){ //access_token验证 $this->checkapiauthroize($this->_server); echo '成功请求到数据';}}
三、 测试效果如图:
1)首先不带access_token请求, test()方法:
结果出现一个401未验证通过的状态
2)然后请求一个错误的access_token, test()方法
同样是一个401的状态,但此时,如图
有信息返回给我们
3) 最后,使用一个正确的access_token, test()方法
所以,基于第1种情况和第2种情况,你应该自定一个token未验证成功的方法,如图:
完结。
相关文章推荐:
php实现用于验证所有类型的信用卡类
thinkphp验证码的实现(form、ajax实现验证)_php实例
以上就是thinkphp5中使用 auth2进行验证的过程分析的详细内容。
其它类似信息

推荐信息