如何使用Yii2编程:Google身份验证
如果您問“ Yii是什么?” 查閱 Yii Framework簡介 ,其中 介紹了Yii 的優點,并概述了Yii 2.0。
在本使用Yii2編程系列中 ,我指導讀者使用PHP的Yii2框架。 在本教程中,我將繼續指導您使用Google Accounts API和OAuth 2.0集成內置AuthClient 。
回顧使用Yii2進行編程:集成用戶注冊 ,它會介紹如何集成Yii2-User庫以進行用戶注冊和身份驗證,以及使用Yii2進行編程:AuthClient與Twitter,Google和其他網絡的集成,這可能對您有所幫助。帶有Twitter身份驗證, 并且現已棄用對Google的OpenID的支持 。 本教程將向您展示如何從早期的Google OpenID集成過渡到使用Google Accounts API的OAuth 2.0集成。
對于這些示例,我們將繼續構建該系列的框架,即我們的hello代碼庫。 使用此頁面上的GitHub鏈接獲取代碼存儲庫的下載。
在開始之前,請記住,我確實會嘗試參加以下討論。 如果您有任何問題或建議,請在下面發表評論,或通過Twitter @reifman與我聯系 。 您也可以直接給我發電子郵件 。
如果您注意到本系列中的內容有所延遲,那是因為我最近剛從腦外科手術回來。 感謝您的耐心配合和支持,很高興能再次定期寫信,我期待繼續關注Yii2。
更新Yii2用戶
自從我上一次寫這些主題以來, Yii2 User有了很大的改進。 我們需要做的第一件事是更新其庫。 您可以在命令行使用composer進行此操作:
composer require "dektrium/yii2-user:0.9.*@dev"這將自動更新composer.json:
"minimum-stability": "stable","require": {"php": ">=5.4.0","yiisoft/yii2": "*","yiisoft/yii2-bootstrap": "*","yiisoft/yii2-swiftmailer": "*","dektrium/yii2-user": "0.9.*@dev","stichoza/google-translate-php": "~2.0","yiidoc/yii2-redactor": "2.0.0","yiisoft/yii2-authclient": "*",},然后,為Yii2 User運行最新的數據庫遷移-這將使您的數據庫與最新的庫保持同步:
php yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations您可能會看到以下內容:
$ php yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations Yii Migration Tool (based on Yii v2.0.6)Total 4 new migrations to be applied:m141222_110026_update_ip_fieldm141222_135246_alter_username_lengthm150614_103145_update_social_account_tablem150623_212711_fix_username_notnullApply the above migrations? (yes|no) [no]:yes *** applying m141222_110026_update_ip_field> alter column registration_ip in table {{%user}} to string(45) ... done (time: 0.009s) *** applied m141222_110026_update_ip_field (time: 0.030s)*** applying m141222_135246_alter_username_length> alter column username in table {{%user}} to string(255) ... done (time: 0.010s) *** applied m141222_135246_alter_username_length (time: 0.012s)*** applying m150614_103145_update_social_account_table> add column code string(32) to table {{%social_account}} ... done (time: 0.008s)> add column created_at integer to table {{%social_account}} ... done (time: 0.009s)> add column email string to table {{%social_account}} ... done (time: 0.008s)> add column username string to table {{%social_account}} ... done (time: 0.009s)> create unique index account_unique_code on {{%social_account}} (code) ... done (time: 0.016s) *** applied m150614_103145_update_social_account_table (time: 0.059s)*** applying m150623_212711_fix_username_notnull> alter column username in table {{%user}} to string(255) NOT NULL ... done (time: 0.009s) *** applied m150623_212711_fix_username_notnull (time: 0.011s)Migrated up successfully.您還必須將yii2-user的組件定義移動到\ hello \ config \ web.php中的模塊:
'modules' => ['redactor' => 'yii\redactor\RedactorModule','class' => 'yii\redactor\RedactorModule','uploadDir' => '@webroot/uploads','uploadUrl' => '/hello/uploads','user' => ['class' => 'dektrium\user\Module','enableUnconfirmedLogin' => TRUE,'confirmWithin' => 21600,'cost' => 12,'admins' => ['admin']], ],注冊您的Google Project
為了讓您的Yii2應用程序用戶注冊并使用他們的Google帳戶登錄,您需要在開發者控制臺注冊Google項目:
讓我們創建一個名為yii2hello 的項目 :
您將使用左側邊欄菜單中的各種選項進入項目主頁:
請務必與我們正在使用的Google請求訪問任何API組的訪問權限,這一點很重要。 對于Yii2用戶,我們需要啟用Google+ API。 點擊API和身份驗證> API,然后搜索google + :
然后,點擊啟用API :
您會看到已啟用的API列表,其中現在包括Google+ API :
接下來,我們需要添加憑據,以便我們可以向Google驗證針對該項目活動的API請求。 點擊憑據 :
讓我們使用上面的OAuth 2.0客戶端ID選項。 然后,將要求您創建OAuth同意屏幕,并填寫有關您的應用程序的信息,這些信息將呈現給嘗試注冊或登錄您的應用程序的用戶:
在“ 憑據”頁面上,我們將選擇Web application ,并且由于我們在本地進行測試,因此我們將為身份驗證回調提供兩個設置。 我將端口8888用于本地開發。 因此,我的授權JavaScript起源將為http:// localhost:8888 ,Yii2用戶要求的授權重定向URI路徑為http:// localhost:8888 / hello / user / security / auth?authclient = google 。
將為您提供API密鑰,也稱為客戶端ID和客戶端密鑰 :
我將在下面查看將這些鍵放置在何處。
配置AuthClient支持
在“ 從GitHub保護您的密鑰”中 ,我詳細描述了如何使用配置文件存儲我的所有密鑰(與GitHub存儲庫分開)。 然后,將此文件包含在我的Yii配置文件的開頭。 這樣可以避免我不小心簽入我的存儲庫密鑰并破壞我的帳戶。
在我的/var/secure/hello.ini文件中,我存儲了應用程序的所有密鑰-將Google API密鑰也放在此處:
oauth_google_clientId="41xxxxxxxxxxxxxeusercontent.com" oauth_google_clientSecret="LmxxxxxxxxxxxxxxxxxxxxxxFJ4" oauth_twitter_key ="rxkxxxxxxxxxxxxxxxxxopjU" oauth_twitter_secret="b7gU4twxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxkLy0z2e" smtp_host = "mailtrap.io" smtp_username = "2xxxxxxxxxxxxxxxxx2" smtp_password = "2xxxxxxxxxxxxxxxxx5" mysql_host="localhost" mysql_un="xxxxxxxxxxxx" mysql_db="hello" mysql_pwd="xxxxxxxxxxxx"我們需要在\config\web.php中的Web配置文件的AuthClient配置設置中添加對這些鍵的引用。 為您希望支持的所有第三方服務添加數組元素:
<?php $config = parse_ini_file('/var/secure/hello.ini', true); $params = require(__DIR__ . '/params.php'); $config = ['id' => 'basic','basePath' => dirname(__DIR__),'bootstrap' => ['log'],'language'=>'en', // back to English'components' => ['view' => ['theme' => ['pathMap' => ['@dektrium/user/views' => '@app/views/user'],],], 'authClientCollection' => ['class' => 'yii\authclient\Collection','clients' => ['google' => ['class' => 'dektrium\user\clients\Google','clientId' => $config['oauth_google_clientId'],'clientSecret' => $config['oauth_google_clientSecret'],],'twitter' => ['class' => 'yii\authclient\clients\Twitter','consumerKey' => $config['oauth_twitter_key'] ,'consumerSecret' => $config['oauth_twitter_secret'] ,],],],另外,在MailTrap簡介:用于應用程序電子郵件的生產前測試的偽SMTP服務器中 ,我開始將MailTrap的自定義SMTP設置集成到我的Yii SwiftMailer配置中以進行測試。 這將確保我們在本地開發平臺上注冊后會收到注冊電子郵件。
對于本教程,我發現我必須將\config\web.php SwiftMailer的端口信息更新為2525:
'mailer' => ['class' => 'yii\swiftmailer\Mailer','viewPath' => '@app/mailer','useFileTransport' => false,'transport' => ['class' => 'Swift_SmtpTransport','host' => $config['smtp_host'],'username' => $config['smtp_username'],'password' => $config['smtp_password'],'port' => '2525','encryption' => 'tls',],],用您的Google帳戶注冊
現在,我們終于準備好使用Yii2 User的前端界面進行探索。 讓我們從注冊開始。 點擊菜單中的注冊選項,您將看到以下表格:
要注冊我們今天創建的所有內容,請點擊Google+圖標。 由于我僅登錄了一個Google帳戶,因此將出現一個透明的彈出窗口并重定向到完成我的注冊:
您應該在右上角看到顯示已通過身份驗證的主頁:
使用您的Google帳戶登錄
點擊注銷 ,然后我們可以嘗試使用Google+圖標登錄。 點擊標題菜單中的登錄 :
然后,再次單擊Google+圖標 。 您將直接進入主屏幕,顯示您已再次登錄。 太酷了吧? 但是,如果您登錄了兩個Google帳戶怎么辦?
選擇用于登錄的Google帳戶后,您將以身份驗證模式返回首頁。
用戶配置文件和連接區域
盡管尚未在Bootstrap標題菜單中對其進行配置,但是如果您訪問http://localhost:8888/hello/user/settings/networks ,則會在Yii2 User中看到第三方帳戶的新連接界面:
登錄后,您的用戶可以從此處連接和斷開其社交帳戶。 還有一個帳戶管理和用戶個人資料區域。 做得很好。
您可能需要查看我的“ 用PHP構建您的啟動”系列 ,該系列使用Yii2的高級模板和第三方集成,目前沒有Yii2 User。 但是,現在有了將Yii2 User與高級模板集成的指南 ,因此我可能會盡快進行更新。
下一步是什么?
當我繼續深入研究框架的不同方面時,請觀看“ 使用Yii2編程”系列中即將發布的教程。
我歡迎功能和主題要求。 您可以在下面的評論中發布它們,或在我的Lookahead Consulting網站上給我發送電子郵件 。
如果您想知道下一個Yii2教程何時到達, 請在Twitter上關注我@reifman或查看我的講師頁面 。 發布后,我的講師頁面將包含本系列中的所有文章。
- 使用Yii2編程:集成用戶注冊(Tuts +)
- 使用Yii2編程:與Twitter,Google和其他網絡(Tuts +)的AuthClient集成
- Yii 2 AuthClient擴展指南
- Yii2 OAuth2庫文檔
- Yii2開發者交流 ,我的Yii2資源網站
翻譯自: https://code.tutsplus.com/tutorials/how-to-program-with-yii2-google-authentication--cms-24987
總結
以上是生活随笔為你收集整理的如何使用Yii2编程:Google身份验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 郑轻oj1033
- 下一篇: vue中svg转png下载