tp上实现的auth2验证,网上找的笔记很少,不像yii,所以在这里发个笔记,帮助有相关需求的朋友PS:由于oauth2有四种方案,所以本例是基于客户端Credentials实现了,其他三个不描述。1.通过composer安装composerrequire--prefer-distbshaffer/oauth2-server-php。安装完成后如图:会出现相关目录。对于数据表,首先找到Pdo.php文件,如图:然后找到位置。目的是告诉你表创建时的名称。复制并粘贴:CREATETABLEoauth_access_tokens(access_tokenvarchar(40)NOTNULL,client_idvarchar(80)NOTNULL,user_idint(11)DEFAULTNULL,expiresvarchar(19)NOTNULL,scopetext,PRIMARYKEY(access_token),KEYfk_token_access_(client_id),密钥ix_access_token_token_expires(expires),condraintfk_access_oauth2_oauth2_client_client_client_idforefer_idforefer_idefirness_idkeiles_ID)copter_oauth2_clientpos_oauth2_client(client_idcasterycasectioncascadecascade)client_idvarchar(80)NOTNULL,user_idint(11)DEFAULTNULL,redirect_uritextNOTNULL,expiresint(11)NOTNULL,scopetext,PRIMARYKEY(authorization_code),KEYfk_authorization_code_oauth2_client_client_id(client_id),KEYix_authorization_code_expires(expires),CONSTRAINTfk_authorization_code_oauth2_client_client_idFOREIGNKEY(client_id)REFERENCESpos_oauth2_client(client_id)ONDELETECASCADEONUPDATECASCADE)ENGINE=InnoDBDEFAULTCHARSET=utf8;创建表oauth_clients(client_idvarchar(80)NOTNULL,client_secretvarchar(80)NOTNULL,redirect_uritextNOTNULL,grant_typetext,scopetext,created_atint(11)DEFAULTNULL,updated_atint(11)DEFAULTNULL,created_byint(11)DEFAULTNULL,updated_byint(11)DEFAULTNULL,PRIMARYKEY(client_id))ENGINE=InnoDBDEFAULTCHARSET=utf8;创建表oauth_refresh_tokens(refresh_tokenvarchar(40)NOTNULL,client_idvarchar(80)NOTNULL,user_idint(11)DEFAULTNULL,expiresint(11)NOTNULL,scopetext,PRIMARYKEY(refresh_token),KEYfk_refresh_token_oauth2_client_client_id(client_id),KEYix_refresh_token_expires(过期),约束fk_refresh_token_oauth2_client_client_id外键(client_id)参考pos_oauth2_client(client_id)在更新级联上删除级联)引擎=InnoDB默认字符集=utf8;创建表oauth_scopes(范围文本=1)DEFAULTAMtinyint(范围文本=1)_defaultAMtinyintDEFAULTCHARSET=utf8;添加一条数据insertintooauth_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,我解释一下,如图:我在实际使用中,只用到了这五个表,也就是上面创建的五个表。在这个配置中,剩下的我已经取消了所有的选项。还有一种情况,我解释一下:有可能你给数据表设置了表前缀,需要在这里做相关的修改。比如我创建的,见图:所以做了相关修改:2)创建授权文件Oauth2.php,自己命名config('dsn'),'username'=>config('username'),'password'=>config('password')]);$server=new\OAuth2\Server($storage,array('enforce_state'=>false));//添加“ClientCredentials”授权类型(这是最简单的授权类型)$server->addGrantType(new\OAuth2\GrantType\ClientCredentials($storage));//添加“授权码”授权类型(这是oauth魔法发生的地方)$server->addGrantType(new\OAuth2\GrantType\AuthorizationCode($storage));//添加“用户凭证”授权类型(这是oauth魔法发生的地方)$server->addGrantType(new\OAuth2\GrantType\UserCredentials($storage));return$server;}/***@校验令牌值*@paramunknown$server*/protectedfunctioncheckApiAuthroize($server){if(!$server->verifyResourceRequest(\OAuth2\Request::createFromGlobals())){$server->getResponse()->send();出口;}}}?>3)创建令牌文件,Access.php_server=$this->grantTypeOauth2();}/****/privatefunction_token(){//处理对OAuth2.0访问令牌的请求并将响应发送到客户端$this->_server->handleTokenRequest(\OAuth2\Request::createFromGlobals())->send('json','oauth2_');}/***@getaccess_token*/publicfunctionaccess_token(){$this->_token();}}?>那么如何请求一个access_token值呢?直接调用access_token()方法请求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_server=$this->grantTypeOauth2();}publicfunctiontest(){//access_token验证$this->checkApiAuthroize($this->_server);echo'successfullyrequesteddata';}}3.测试效果如图:1)首先请求不带access_token,test()方法:结果显示状态为401未验证2)然后请求错误access_token,test()方法也是401状态,但是此时如图,有信息返回给我们3)最后,使用正确的access_token,test()方法所以,基于第一种情况而第二种情况,需要自定义一个token验证成功的方法,如图:完成。如有疑问,请加入学习群2751786,谢谢
