oauth2.0服務端與客戶端搭建 - 推酷php
今天搭建了oauth2.0服務端與客戶端。把搭建的過程記錄一下。具體實現的功能是:client.ruanwenwu.cn的用戶可以經過 server.ruanwenwu.cn的用戶名和密碼登錄client.ruanwenwu.cn。而且登錄 後,client.ruanwenwu.cn的用戶能獲取server.ruanwenwu.cn哪裏的一些資源。html
個人我的博客原文地址: http://www.ruanwenwu.cn/2015/12/oauth-server-and-client-install.html html5
1、oauth2.0的做用mysql
一、搭建第三方登陸平臺(就像你用不少網站支持qq登陸。實際上是qq提供的oauth2.0服務端支持。網站做爲第三方,只要用oauth2.0標準請求服務端,求能獲得用戶信息,並實現登陸)。web
二、公共資源管理。ajax
就 像你(Y)想經過一個打印照片的網站(A)來打印你存放在google(B)的照片。你就要經過這個A來獲取B的照片。B確定要驗證是不是他的用戶Y的請 求,驗證經過才把照片傳遞給A實現打印。問題是Y不想把帳號密碼直接給A。用oauth2.0就是來解決這個問題的:sql
A先 把Y導向B的站點進行受權。受權經過,B向A發送oauth_code。A拿到這個oauth_code,連同A的client_id和 oauth_secrete發送給B,若是數據正確,B就會給A發送oauth_token。有了這個token,A就能夠獲取B的相關資源的。mongodb
基本的流程是這樣。下面是具體的實現過程。thinkphp
2、oauth2.0服務端的搭建。數據庫
oauth2.0的服務端分爲驗證服務器和資源服務器。這兩個服務器也能夠是同一個服務器(只不過把這兩個功能用一個 服務器來完成罷了)。
說明:個人實現都在Thinkphp3.2.3框架下實現。
一、下載thinkphp3.2.3。
二、配置虛擬機server.ruanwenwu.cn
三、在Thinkphp的Home應用下修改配置文件(server.ruanwenwu.cn/Application/Home/Conf/config.php)以下所示:
<?php return array( //'配置項'=>'配置值' //數據庫設置 'DB_TYPE' => 'mysql', 'DB_HOST' => '127.0.0.1',//localhost 'DB_NAME' => 'oauth', 'DB_USER' => 'root', 'DB_PWD' => 'root', 'DB_PORT' => '3306', 'SCOPE_INFO' => array( //這是受權選項。根據你本身的項目來 array( 'name' => '我的信息', 'value' => 'basicinfo' ), array( 'name' => '論壇發帖回帖', 'value' => 'bbsinfo' ), ), 'OAUTH2_CODES_TABLE' =>'oauth_code', //這裏是oauth項目須要用的三個基礎表 'OAUTH2_CLIENTS_TABLE' =>'oauth_client', 'OAUTH2_TOKEN_TABLE' =>'oauth_token', 'SECRETKYE' => 'Mumayi!@#', //下面是一些網站自定義的項目。能夠根據本身的狀況來寫或者不寫 //session 有效期 'SESSION_EXPIRES' => 1200, //key 有效期 'PASS_KEY_EXPIRES' => 86400, //key 有效期 'PHONE_KEY_EXPIRES' => 300, //key 加密 整型 數字 必須爲 int 'PASS_KEY_CALC' => 1314, );
四、建oauth表。
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for oauth_client -- ---------------------------- DROP TABLE IF EXISTS `oauth_client`; CREATE TABLE `oauth_client` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `client_id` varchar(32) NOT NULL, `client_secret` varchar(32) NOT NULL, `redirect_uri` varchar(200) NOT NULL, `create_time` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for oauth_code -- ---------------------------- DROP TABLE IF EXISTS `oauth_code`; CREATE TABLE `oauth_code` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `client_id` varchar(32) NOT NULL, `user_id` int(11) NOT NULL DEFAULT '1', `code` varchar(40) NOT NULL, `redirect_uri` varchar(200) NOT NULL, `expires` int(11) NOT NULL, `scope` varchar(250) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=57 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for oauth_token -- ---------------------------- DROP TABLE IF EXISTS `oauth_token`; CREATE TABLE `oauth_token` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `client_id` varchar(32) NOT NULL, `user_id` int(11) NOT NULL, `access_token` varchar(40) NOT NULL, `refresh_token` varchar(40) NOT NULL, `expires_in` int(11) NOT NULL, `scope` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
五、引入oauth2.0服務端類文件。
5-一、在\server.ruanwenwu.cn\ThinkPHP\Library\Vendor\目錄下創建oauth目錄。
5-二、引入oauth2.0服務端PHP腳本。
在剛創建的oauth目錄下引入OAuth2.class.php這個文件基本不用作修改。咱們的操做都在繼承這個類的子類上完成。類的代碼以下:
<?php /** * @mainpage * OAuth 2.0 server in PHP, originally written for * <a href="http://www.opendining.net/"> Open Dining</a>. Supports * <a href="http://tools.ietf.org/html/draft-ietf-oauth-v2-10">IETF draft v10</a>. * * Source repo has sample servers implementations for * <a href="http://php.net/manual/en/book.pdo.php"> PHP Data Objects</a> and * <a href="http://www.mongodb.org/">MongoDB</a>. Easily adaptable to other * storage engines. * * PHP Data Objects supports a variety of databases, including MySQL, * Microsoft SQL Server, SQLite, and Oracle, so you can try out the sample * to see how it all works. * * We're expanding the wiki to include more helpful documentation, but for * now, your best bet is to view the oauth.php source - it has lots of * comments. * * @author Tim Ridgely <tim.ridgely@gmail.com> * @author Aaron Parecki <aaron@parecki.com> * @author Edison Wong <hswong3i@pantarei-design.com> * * @see http://code.google.com/p/oauth2-php/ */ /** * The default duration in seconds of the access token lifetime. */ define("OAUTH2_DEFAULT_ACCESS_TOKEN_LIFETIME", 3600); /** * The default duration in seconds of the authorization code lifetime. */ define("OAUTH2_DEFAULT_AUTH_CODE_LIFETIME", 30); /** * The default duration in seconds of the refresh token lifetime. */ define("OAUTH2_DEFAULT_REFRESH_TOKEN_LIFETIME", 1209600); /** * @defgroup oauth2_section_2 Client Credentials * @{ * * When interacting with the authorization server, the client identifies * itself using a client identifier and authenticates using a set of * client credentials. This specification provides one mechanism for * authenticating the client using password credentials. * * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-2 */ /** * Regex to filter out the client identifier (described in Section 2 of IETF draft). * * IETF draft does not prescribe a format for these, however I've arbitrarily * chosen alphanumeric strings with hyphens and underscores, 3-32 characters * long. * * Feel free to change. */ define("OAUTH2_CLIENT_ID_REGEXP", "/^[a-z0-9-_]{3,32}$/i"); /** * @} */