1. 程式人生 > >api-gateway

api-gateway

包括 詳細介紹 idp ati sdn 命令 rop uri 密碼

#spring-oauth-server


<strong>Spring與OAuth2的整合</strong>

項目用Maven管理, 字符編碼: UTF-8


使用的技術與版本號
<ol>
<li>JDK (1.8.0_40)</li>
<li>Servlet (3.1.0)</li>
<li>Spring (4.1.6.RELEASE)</li>
<li>Spring Security (4.0.4.RELEASE)</li>
<li>spring-security-oauth2 (2.0.7.RELEASE)</li>
<li>Log4j (1.2.14)</li>
<li>MySQL (5.6.23)</li>
<li>EhCache (2.0.4)</li>
</ol>
<hr/>

<strong>Java Config版本請訪問Branch: <a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/config/">config</a></strong>

<strong>MongoDB版本請訪問Branch: <a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/mongodb/">mongodb</a></strong>

<strong>OAuth2下一代身份認證授權協議OIDC實現: <a href="https://git.oschina.net/mkk/MyOIDC">MyOIDC</a></strong>
<hr/>
<h3>
OAuth2客戶端項目請訪問 <a href="http://git.oschina.net/mkk/spring-oauth-client">spring-oauth-client</a>
</h3>
<h3>
在線測試訪問地址 <a href="https://andaily.com/spring-oauth-server/">https://andaily.com/spring-oauth-server/</a>
</h3>
<h4>
Shiro與OLTU整合的OAuth2項目 <a href="http://git.oschina.net/mkk/oauth2-shiro">http://git.oschina.net/mkk/oauth2-shiro</a>
(相比spring-oauth-server, 該項目入門門檻相對較低, 代碼更加透明, 理解更容易,可擴展性更強, 且模塊化開發)
</h4>
<hr/>

<p>
<strong>如何使用?</strong>
<ol>
<li>
項目是Maven管理的, 需要本地安裝maven(開發用的maven版本號為3.1.0), 還有MySql(開發用的mysql版本號為5.6)
</li>
<li>
<a href="http://git.oschina.net/shengzhao/spring-oauth-server/repository/archive?ref=master">下載</a>(或clone)項目到本地
</li>
<li>
創建MySQL數據庫(如數據庫名oauth2), 並運行相應的SQL腳本(腳本文件位於others/database目錄),
<br/>
運行腳本的順序: initial_db.ddl -> oauth.ddl -> initial_data.ddl
</li>
<li>
修改<a href="http://git.oschina.net/shengzhao/spring-oauth-server/blob/master/src/main/resources/spring-oauth-server.properties">spring-oauth-server.properties</a>(位於src/main/resources目錄)中的數據庫連接信息(包括username, password等)
</li>
<li>
將本地項目導入到IDE(如Intellij IDEA)中,配置Tomcat(或類似的servlet運行服務器), 並啟動Tomcat(默認端口為8080);
<br/>
註意將項目的 contextPath(根路徑) 設置為 ‘spring-oauth-server‘.
<br/>
另: 也可通過maven package命令將項目編譯為war文件(spring-oauth-server.war),
將war放在Tomcat中並啟動(註意: 這種方式需要將spring-oauth-server.properties加入到classpath中並正確配置數據庫連接信息).
</li>
<li>
參考<a href="http://git.oschina.net/shengzhao/spring-oauth-server/blob/master/others/oauth_test.txt">oauth_test.txt</a>(位於others目錄)的內容並測試之(也可在瀏覽器中訪問相應的地址,如: http://localhost:8080/spring-oauth-server).
</li>
</ol>
</p>


<hr/>
<strong>grant_type</strong>

說明OAuth2支持的grant_type(授權方式)與功能
<ol>
<li><code>authorization_code</code> -- 授權碼模式(即先登錄獲取code,再獲取token)</li>
<li><code>password</code> -- 密碼模式(將用戶名,密碼傳過去,直接獲取token)</li>
<li><code>refresh_token</code> -- 刷新access_token</li>
<li><code>implicit</code> -- 簡化模式(在redirect_uri 的Hash傳遞token; Auth客戶端運行在瀏覽器中,如JS,Flash)</li>
<li><code>client_credentials</code> -- 客戶端模式(無用戶,用戶向客戶端註冊,然後客戶端以自己的名義向‘服務端‘獲取資源)</li>
</ol>

<hr/>
<strong>幫助與改進</strong>
<ol>
<li>
<p>
與該項目相關的博客請訪問 <a target="_blank" href="http://blog.csdn.net/monkeyking1987/article/details/16828059">http://blog.csdn.net/monkeyking1987/article/details/16828059</a>
</p>
</li>
<li>
<p>
如果在使用過程中遇到特殊的問題(如:如何將oauth_code存入數據庫),請訪問項目的 <a href="http://git.oschina.net/shengzhao/spring-oauth-server/wikis/pages">Wiki</a>
與 <a href="http://git.oschina.net/shengzhao/spring-oauth-server/attach_files">附件</a>.
<br/>
我會把大家反饋的問題解決辦法添加在這裏.
<br/>
若在這兩個地方沒有找到解決辦法的,
歡迎發郵件到<a href="mailto:[email protected]">[email protected]</a>一起討論.
</p>
</li>

<li>
<p>
如果在使用項目的過程中發現任何的BUG或者更好的提議, 建議將其提交到項目的 <a href="http://git.oschina.net/shengzhao/spring-oauth-server/issues">Issues</a> 中,
我會一直關註並不斷改進項目.
</p>
</li>
</ol>

<hr/>
<strong>功能擴展</strong>
<ol>
<li>
<code>oauth_code存入數據庫的配置</code>, 請下載文件 <a href="http://git.oschina.net/shengzhao/spring-oauth-server/attach_files">oauth_code存入數據庫的配置.jpg</a>
</li>
<li>
<code>改變token過期的時間的配置</code>, 請下載文件<a href="http://git.oschina.net/shengzhao/spring-oauth-server/attach_files">改變token過期的時間的配置.jpg</a>
</li>
<li>
<code>自定義 grant_type</code>, 默認情況支持的grant_type包括 [password,authorization_code,refresh_token,implicit], 若不需要其中的某些grant_type,
則可以修改 oauth_client_details 表中的 authorized_grant_types 字段的值;
<br/>
若想把整個Oauth服務修改來只支持某些grant_type, 請修改 <i>security.xml</i>文件中的
<label>oauth2:authorization-server</label> 中的內容,將對應的 grant_type 註釋或刪掉即可
</li>
<li>
<p>
<code>如何刷新access_token(refresh_token)</code>, 在通過客戶端(如移動設備)登錄成功後返回的數據如下
<br/>
<pre>{"access_token":"3420d0e0-ed77-45e1-8370-2b55af0a62e8","token_type":"bearer","refresh_token":"b36f4978-a172-4aa8-af89-60f58abe3ba1","expires_in":43199,"scope":"read write"}
</pre>
<br/>
若需要刷新獲取新的token(一般在 expires_in 有效期時間快到時), 請求的URL類似如下
<br/>
<pre>http://localhost:8080/oauth/token?client_id=mobile-client&client_secret=mobile&grant_type=refresh_token&refresh_token=b36f4978-a172-4aa8-af89-60f58abe3ba1
</pre>
<br/>
註意: refresh_token 參數值必須與登錄成功後獲取的 refresh_token 一致, 且grant_type = refresh_token
<br/>
另: 刷新token 需要 ClientDetails 支持 refresh_token 類型的 grant_type (默認是支持的)
</p>
</li>
</ol>


<hr/>
<h3>開發計劃</h3>
<p>
從 0.3版本開始將項目的所有計劃的開發內容列出來, 方便大家跟進, 也歡迎你加入.
<br/>
項目的開發管理使用開源項目 <a href="http://git.oschina.net/mkk/andaily-developer">andaily-developer</a>.
</p>
<ul>
<li>
<p>
Version: <strong>1.0</strong> [pending]
<br/>
Date: 2017-03-30 / ---
</p>
<ol>
<li><p><del>implicit測試時 取消掉 client secret</del></p></li>
<li><p>---</p></li>
</ol>
<br/>
</li>
<li>
<p>
Version: <strong>0.6</strong> [finished]
<br/>
Date: 2016-07-07 / 2016-10-13
</p>
<ol>
<li><p><del>(150) - 修改OAUTH錯誤時返回JSON數據</del></p></li>
<li><p><del>(151) - 數據添加Ehcache緩存支持</del></p></li>
<li><p><del>(158) - 對配置,代碼必要的地方添加註釋,方便理解</del></p></li>
<li><p><del>添加OIDC協議文檔</del></p></li>
</ol>
<br/>
</li>
<li>
<p>
Version: <strong>0.5</strong> [finished]
<br/>
Date: 2016-02-19 / 2016-06-02
</p>
<ol>
<li><p><del>(118) - Add java-config(零配置) 的支持, 以及啟用 新的註解</del></p></li>
<li><p><del>(138) - OAuth ‘token‘ Restful API</del></p></li>
<li><p><del>(139) - User Overview/ user add/archive</del></p></li>
<li><p><del>(143) - Add project API document</del></p></li>
<li><p><del>(144) - Add MongoDB branch</del></p></li>
</ol>
<br/>
</li>
<li>
<p>
Version: <strong>0.4</strong> [finished]
<br/>
Date: 2015-11-09 / 2015-11-30
</p>
<ol>
<li><p><del>(97) - Fix custom access_token_validity,refresh_token_validity issue(#5)</del></p></li>
<li><p><del>(109) - 升級 spring-security-oauth2 的版本到 2.0.6以上, 目前是1.0.5 </del></p></li>
<li><p><del>(113) - Upgrade spring, spring security version to > 4.0</del></p></li>
<li><p><del>將項目添加到在線測試服務器</del></p></li>
<li><p><del>(115) - Sync update spring-oauth-client version with spring-oauth-server</del></p></li>
<li><p><del>(116) - Remove mybatis dependency</del></p></li>
<li><p><del>Upgrade JAVA to 1.8; Servlet 3.0</del></p></li>
<li><p><del>Oauth table add index </del></p></li>
</ol>
</li>
<li>
<p>
Version: <strong>0.3</strong> [finished]
<br/>
Date: 2015-05-14 / 2015-06-07
</p>
<ol>
<li><p>#73 - Upgrade ‘spring-security-oauth2‘ version to ‘2.0.6.RELEASE‘ (current: 1.0.5.RELEASE) [CANCELED]</p></li>
<li><p><del>#74 - oauth mysql ddl add create_time, default is now() </del></p></li>
<li><p><del>#75 - Add user information API, for <a href="http://git.oschina.net/mkk/spring-oauth-client"><code>spring-oauth-client</code></a> project use
<pre>
URL: /unity/user_info
Login: Yes (ROLE_UNITY)
Data Format: JSON

URL: /m/user_info
Login: Yes (ROLE_MOBILE)
Data Format: JSON
</pre>
</del></p>
</li>
<li><p><del>#77 - User add Privilege domain.
Addition initial two user: unityuser(ROLE_UNITY),mobileuser("ROLE_MOBILE).
If default user, return all privilegs, otherwise return specify privilege(s) </del></p></li>
<li><p><del>#78 - Initial ‘sprint-oauth-client‘ project(maven), add sub-modules</del></p></li>
<li><p><del>#91 - User log4j replace logback dependency </del></p></li>
<li><p><del>#92 - Add database table column description. (添加數據庫表的字段說明) </del></p></li>
<li><p><del>#93 - 將默認的 oauth_code存入數據庫(當前是存入內存) </del></p></li>
<li><p><del> spring-oauth-server project add Bootstrap CSS </del></p></li>
<li><p><del>#95 - Add ‘client-details‘ management; create/delete, show testing links<del></p></li>

</ol>
</li>
</ul>
<br/>

<hr/>
<strong>數據庫表字段說明</strong>
<p>
在0.3版本中添加了<code>db_table_description.html</code>文件(位於/others目錄), 用來說明數據庫腳本文件<code>oauth.ddl</code>中各表,各字段的用途及使用場合.
<br/>
也可在線訪問<a href="http://andaily.com/spring-oauth-server/db_table_description.html">http://andaily.com/spring-oauth-server/db_table_description.html</a>.
</p>


<hr/>
<strong>Project Log</strong>
<p>
<ol>
<li><p><em>2013-11-19</em> Initial project, start push code</p></li>
<li><p><em>2013-11-20</em> 發布<a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/0.1/">0.1</a>版本</p></li>
<li><p><em>2015-05-06</em> 發布<a href="http://www.oschina.net/news/62176/spring-oauth-server-0-2">0.2</a>版本</p></li>
<li><p><em>2015-05-27</em> 創建項目博客,訪問地址 <a href="http://andaily.com/blog/?cat=19">http://andaily.com/blog/?cat=19</a></p></li>
<li><p><em>2015-06-07</em> 發布<a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/0.3/">0.3</a>版本</p></li>
<li><p>
<em>2015-06-16</em> 添加github訪問: <a href="https://github.com/monkeyk/spring-oauth-server">https://github.com/monkeyk/spring-oauth-server</a>,
以後的更新將同步github與gitosc.
</p></li>
<li><p><em>2015-11-09</em> 開始開發 0.4-beta 版本</p></li>
<li><p><em>2015-11-18</em> 發布 <a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/0.4-beta/">0.4-beta</a> 版本</p></li>
<li><p><em>2016-01-02</em> 發布 <a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/0.4/">0.4</a> 版本</p></li>
<li><p><em>2016-02-19</em> Add 0.5 version development planning</p></li>
<li><p><em>2016-04-03</em> Add <a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/config/">config</a> branch</p></li>
<li><p><em>2016-04-14</em> Add <a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/mongodb/">mongodb</a> branch</p></li>
<li><p><em>2016-06-02</em> 發布 <a href="http://git.oschina.net/shengzhao/spring-oauth-server/tree/0.5/">0.5</a> 版本</p></li>
<li><p><em>2016-07-06</em> Add 0.6 version planning</p></li>
<li><p><em>2016-10-13</em> 發布0.6版本</p></li>
</ol>
</p>


<hr/>
<strong>更多資源</strong>
<p>以下是在學習工作中收集的更多關於OAuth2的資源,對深入理解與運用OAuth2有幫助</p>
<ul>
<li>
<p>
<a href="http://tools.ietf.org/html/rfc6749">RFC 6749 - The OAuth 2.0 Authorization Framework</a>, OAuth2.0協議(英文)
</p>
</li>
<li>
<p>
<a href="http://oauth.net/2/">OAuth 2.0 &mdash; OAuth</a>, OAuth2.0官方網站
</p>
</li>
<li>
<p>
<a href="http://netment.iteye.com/blog/945402">OAUTH2核心參數說明</a>, 重點介紹了grant_type 與 response_type 以及示例
</p>
</li>
<li>
<p>
<a href="http://apiwiki.poken.com/authentication/oauth2">OAuth2 flows</a>, 詳細介紹Oauth2的流程,各類錯誤發生時的響應
</p>
</li>
<li>
<p>
<a href="http://www.oschina.net/translate/oauth-2-developers-guide">OAuth 2 開發人員指南(Spring security oauth2)</a>, 翻譯OAuth 2 Developers Guide(spring security oauth2)
</p>
</li>
<li>
<p>
<a href="http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html">理解OAuth 2.0</a>, 介紹Oauth2各類grant_type的使用
</p>
</li>
<li>
<p>
<a href="http://www.dannysite.com/blog/178/">OAuth2:隱式授權(Implicit Grant)類型的開放授權</a>, 介紹grant_type=‘implicit‘模式
</p>
</li>
<li>
<p>
<a href="http://oltu.apache.org/">Apache Oltu</a>, Java版的 OAuth參考實現, 建議去了解了解
</p>
</li>
<li>
<p>
<a href="http://andaily.com/blog/?p=440">OIDC–基於OAuth2的下一代身份認證授權協議</a>
</p>
</li>
<li>
<p>
<a href="https://andaily.com/blog/?p=528">正確處理spring-oauth-server中在驗證失敗或錯誤時的方式</a>
</p>
</li>
</ul>


<hr/>
<h4>
與項目相關的技術文章請訪問 <a href="http://andaily.com/blog/?cat=19">http://andaily.com/blog/?cat=19</a> (不斷更新與OAuth2相關的文章)
</h4>
<p>
<strong>問答與討論</strong>
<br/>
與項目相關的,與OAuth2相關的問題與回答,以及各類討論請訪問<br/>
<a href="http://andaily.com/blog/?dwqa-question_category=oauth">http://andaily.com/blog/?dwqa-question_category=oauth</a>
</p>


<hr/>
<h4>使用案例</h4>
<p>以下是已知的使用(或基於) spring-oauth-server 開源項目的各類商業項目(排名不分先後), 若你有案例希望添加, 請聯系作者.</p>
<ul>
<li><p>Hongkong Parkway Online (在線醫療服務系統)</p></li>
<li><p>海爾日日平臺 (B2B電商平臺)</p></li>
<li><p>wdcy-game (手機遊戲服務端)</p></li>
<li><p>Honyee Management System (企業管理系統)</p></li>
<li><p>AoLin Open Platform (國際物流開發平臺)</p></li>
<li><p>IDS (移動安全產品)</p></li>
<li><p>IDP (統一身份認證平臺)</p></li>
<li><p>......</p></li>
</ul>


<hr/>
<div>
<h3>捐助</h3>
支付寶: [email protected] (**釗)
<ul>
<li><p>快意江湖 -- 100元</p></li>
<li><p>yufan -- 100元</p></li>
<li><p>強鄭 -- 1元 (2016-09-07)</p></li>
<li><p>建化 -- 5元 (2016-12-16)</p></li>
<li><p>南京索特科技 -- 200元 (2016-12-16)</p></li>
<li><p>周廣文 -- 6.66元 (2017-02-17)</p></li>
</ul>
</div>

<hr/>
<p>
關註更多我的開源項目請訪問 <a href="https://andaily.com/my_projects.html">https://andaily.com/my_projects.html</a>
</p>

<p>
若需更多的技術支持請聯系 <a href="mailto:[email protected]">[email protected]</a>
</p>
<h3>
<em>若需商業技術支持或提供技術解決方案, 請聯系 <a href="mailto:[email protected]">[email protected]</a></em>
或訪問 <a href="https://monkeyk.com/kso/" target="_blank">https://monkeyk.com/kso/</a>
</h3>
<p>
<img src="http://77g1is.com1.z0.glb.clouddn.com/wechat_qrcode.jpg"/>
</p>

api-gateway