牛骨文教育服务平台(让学习变的简单)
博文笔记

在Yii2框架中搭建OAuth2服务,并用POSTMAN进行调试

创建时间:2018-03-16 投稿人: 浏览次数:199

OAuth2是目前比较流行的认证方式,本文主要阐述如何在Yii2框架中搭建该服务。

一、安装Yii2的OAuth扩展

在Yii2的根目录下运行:

composer require bshaffer/oauth2-server-php: ~1.7

运行成功后,会在vendor目录下自动建立bshaffer/oauth2-server-php扩展目录。

二、在数据库中建立必要的数据表

此处数据库假定名称 testdb:

CREATE TABLE oauth_clients (
  client_id             VARCHAR(80)   NOT NULL,
  client_secret         VARCHAR(80),
  redirect_uri          VARCHAR(2000),
  grant_types           VARCHAR(80),
  scope                 VARCHAR(4000),
  user_id               VARCHAR(80),
  PRIMARY KEY (client_id)
);

CREATE TABLE oauth_access_tokens (
  access_token         VARCHAR(40)    NOT NULL,
  client_id            VARCHAR(80)    NOT NULL,
  user_id              VARCHAR(80),
  expires              TIMESTAMP      NOT NULL,
  scope                VARCHAR(4000),
  PRIMARY KEY (access_token)
);

CREATE TABLE oauth_authorization_codes (
  authorization_code  VARCHAR(40)     NOT NULL,
  client_id           VARCHAR(80)     NOT NULL,
  user_id             VARCHAR(80),
  redirect_uri        VARCHAR(2000),
  expires             TIMESTAMP       NOT NULL,
  scope               VARCHAR(4000),
  id_token            VARCHAR(1000),
  PRIMARY KEY (authorization_code)
);

CREATE TABLE oauth_refresh_tokens (
  refresh_token       VARCHAR(40)     NOT NULL,
  client_id           VARCHAR(80)     NOT NULL,
  user_id             VARCHAR(80),
  expires             TIMESTAMP       NOT NULL,
  scope               VARCHAR(4000),
  PRIMARY KEY (refresh_token)
);

CREATE TABLE oauth_users (
  username            VARCHAR(80),
  password            VARCHAR(80),
  first_name          VARCHAR(80),
  last_name           VARCHAR(80),
  email               VARCHAR(80),
  email_verified      BOOLEAN,
  scope               VARCHAR(4000),
  PRIMARY KEY (username)
);

CREATE TABLE oauth_scopes (
  scope               VARCHAR(80)     NOT NULL,
  is_default          BOOLEAN,
  PRIMARY KEY (scope)
);

CREATE TABLE oauth_jwt (
  client_id           VARCHAR(80)     NOT NULL,
  subject             VARCHAR(80),
  public_key          VARCHAR(2000)   NOT NULL
);

同时插入一条测试数据:

INSERT INTO oauth_clients (client_id, client_secret, redirect_uri) VALUES ("testclient", "testpass", "http://localhost/");

三、建立Controller

创建文件frontend/controllers/TestController.php,首先引入必要的类:

namespace frontend/controllers;
use OAuth2StoragePdo;
use OAuth2Server;
use OAuth2GrantTypeAuthorizationCode;
use OAuth2GrantTypeClientCredentials;
use OAuth2GrantTypeUserCredentials;
use OAuth2Request;

编写可以共用的获取服务器的类,其中testdb为数据库名称,testusername为数据库用户名,testpassword为数据库密码。

    public function getServer()
    {
        $dsn = "mysql:dbname=testdb;host=localhost";
        $username = "testusername";
        $password = "testpassword";
        $storage = new Pdo(array("dsn" => $dsn, "username" => $username, "password" => $password));
        $server = new Server($storage);
        $server->addGrantType(new ClientCredentials($storage));
        return $server;
    }
接在在该类中编写测试方法:
public function actionToken(){
        $server = $this->getServer();
        var_dump($server->handleTokenRequest(Request::createFromGlobals())->send());
    }

四、用Postman进行测试

首先在Postman中设定Headers和Body,建立上面的TestController之后,由于默认OAuth2采用POST方式提交,所以需要进行特别设定,首先在Headers中设定:

Content-Type:application/x-www-form-urlencoded
然后在Body中设定:
grant_type: client_credentials
client_id: testclient
client_secret: testpass

然后在Postman中,选择“POST”方式,输入:

http://localhost/yii2/index.php?r=test/token

如何运行无误,则在Postman的结果栏中会显示类似:

{"access_token":"fa0f0f52ad6f24082ef84e1b925cf97c23bd7588","expires_in":3600,"token_type":"Bearer","scope":null}

的数据。

此处表明Yii2框架下搭建OAuth2服务成功。





声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。