├── .gitignore ├── LICENSE ├── README.md ├── oauth2.0_docs ├── OAuth 2.0协议.pptx ├── OAuth2 Client Credentials授权模式.md ├── OAuth2 Implicit Grant授权模式.md └── imgs │ └── OAuth2.ClientCredentials.jpg └── 权限设计 ├── imgs └── RBAC.png └── 权限设计方案简要概括.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | OpenAPI 2 | ======= 3 | 4 | 有关开放平台项目的一些个人总结 5 | -------------------------------------------------------------------------------- /oauth2.0_docs/OAuth 2.0协议.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxqlovejava/OpenAPI/0fd497b6f4a567ff65876a2891a7cec0f4997e3f/oauth2.0_docs/OAuth 2.0协议.pptx -------------------------------------------------------------------------------- /oauth2.0_docs/OAuth2 Client Credentials授权模式.md: -------------------------------------------------------------------------------- 1 | # OAuth2 Client Credentials(客户端证书授权)开放授权 2 | 3 | ## 适用范围 4 | 5 | 适合用于访问一些**公开资源**(不适合于访问类似用户私人数据这样的资源),比如微博广场首页的微博数据。第三方应用以**应用本身身份**而不是用户身份来读取和修改这些资源。 6 | 7 | 所以这种授权模式比较适合**平台合作方式**的授权。 8 | 9 | ## 工作流程 10 | 11 | ![OAuth2客户端证书授权示意图](./imgs/OAuth2.ClientCredentials.jpg) 12 | 13 | ### A. 用Client Credentials换Access Token(访问令牌) 14 | 假如example.com是OAuth2服务端,则先用如下Shell脚本请求获取Access Token: 15 | 16 | ```shell 17 | curl -X POST -d "grant_type=client_credentials\ 18 | &client_id=c596bd6f3d994df18ed7df90656980bf\ 19 | &client_secret=14decc9f4298477db2c35a951ddbc368\ 20 | &scope=user_r" \ 21 | https://open.example.com/oauth/access_token 22 | ``` 23 | 24 | 几点说明: 25 | * 建议用HTTP POST方式请求 26 | * grant_type、client_id、client_secret都是必选参数,scope是可选参数 27 | * grant_type必须为固定值client_credentials 28 | * client_id和client_secret分别为引用的appKey和appSecret,这两个参数可以放在HTTP头中(OAuth2服务端可以提供两种扩展HTTP请求头,比如X-OAuth2-clientID和X-OAuth2-clientSecret),也可以放在查询参数中 29 | 30 | 如果认证成功,则返回Access Token: 31 | 32 | ```json 33 | { 34 | "access_token":"e4cf99facd9948489301da932494c9b1", 35 | "expires_in": 86400 36 | } 37 | ``` 38 | 39 | ### B. 使用Access Token访问资源 40 | 将Access Token放在HTTP Authorization头或者查询参数中,请求访问资源: 41 | 42 | ```shell 43 | curl https://open.example.com/hot_topics?access_token=e4cf99facd9948489301da932494c9b1 44 | ``` 45 | 46 | ### C. 其他扩展 47 | 48 | 因为严格来说,这种模式不属于OAuth要解决的问题范畴,所以我们可以扩展这种模式,只要**认证服务器能够以某种身份验证客户端身份即可**。 49 | 50 | 比如我们可以扩展一种**临时授权模式**,分配一个临时key(过一段时间会失效)给应用,应用通过临时key获取到一个临时访问令牌,通过这种方式获取的访问令牌有下面特点: 51 | 52 | * 有效期比通过服务端授权码授权模式得到的访问令牌短得多 53 | * 通过这种临时访问令牌能访问的资源比其他方式要少得多 -------------------------------------------------------------------------------- /oauth2.0_docs/OAuth2 Implicit Grant授权模式.md: -------------------------------------------------------------------------------- 1 | # OAuth2 Implicit Grant(简化授权)授权模式 2 | 3 | ## 适用范围 4 | 主要用于无服务器端的应用。涉及的角色包括:用户(Resource Owner)、UA(User Agent)、授权服务器、资源服务器。 5 | 6 | 7 | ## 工作流程 8 | 9 | * A. Client(第三方应用)将用户导向认证服务器 10 | * B. 用户决定是否给客户端授权 11 | * C. 如果用户同意给客户端授权,则认证服务器将用户重定向到Client指定的redirect_uri,并在URI的Hash部分包含访问令牌 12 | * D. UA通过JavaScript解析URI的Hash中的访问令牌信息 13 | * E. UA将解析得到的访问令牌发给Client 14 | * F. UA或Client利用访问令牌去资源服务器取资源 15 | 16 | 注意令牌对用户和浏览器是可见的。 17 | 18 | 下面是API请求过程(example.com是OAuth服务端): 19 | 20 | ```shell 21 | # authorize 22 | curl https://open.example.com/authorize?\ 23 | client_id=98b698eb7bc14259b35fc337a1c6a0eb\ 24 | &response_type=token\ 25 | &redirect_uri=http://client.com/cb\ 26 | &scope=user_r,user_w 27 | &state=abc 28 | ``` 29 | 注意几条: 30 | * client_id、response_type和redirect_uri是必选参数 31 | * scope和state都是可选参数 32 | * response_type为固定值token 33 | * 如果传了state参数,则下一步返回必须原封不动带上state参数 34 | 35 | 成功授权后,跳回redirect_uri所定义的网址: 36 | 37 | ``` 38 | HTTP/1.1 302 Found 39 | Location: http://client.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA 40 | &state=abc&token_type=example&expires_in=3600 41 | ``` 42 | 43 | 然后浏览器可以提取上面Location地址中的Hash值得到访问令牌,再通过访问令牌访问资源服务器上的资源。当然还可以把访问令牌发回给Client Server然后存起来,这样避免后续再去授权。不过这没有什么意义,因为通过简化授权模式得到的访问令牌的有效期一般都很短,比如30分钟。 44 | 45 | 相比授权码模式,少了一个获取Authorization Code(即授权码)的流程,而且不会返回refresh_token。所以这种模式的安全性比较低。安全性低,所以不应该给访问令牌设置过长的有效期。 46 | 47 | 另外为了安全性考虑,应用从授权回调地址接收到访问令牌后,可以调用下get_token_info接口,然后确认是否对应自身的appKey。 -------------------------------------------------------------------------------- /oauth2.0_docs/imgs/OAuth2.ClientCredentials.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxqlovejava/OpenAPI/0fd497b6f4a567ff65876a2891a7cec0f4997e3f/oauth2.0_docs/imgs/OAuth2.ClientCredentials.jpg -------------------------------------------------------------------------------- /权限设计/imgs/RBAC.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxqlovejava/OpenAPI/0fd497b6f4a567ff65876a2891a7cec0f4997e3f/权限设计/imgs/RBAC.png -------------------------------------------------------------------------------- /权限设计/权限设计方案简要概括.md: -------------------------------------------------------------------------------- 1 | # 权限设计简要概括 2 | 3 | ## 1、权限设计的方案选择 4 | 5 | ### 1.1、RBAC权限模型 6 | RBAC(Role Base Access Control,基于角色的访问控制协议)是企业应用管理系统(比如CRM、OA等系统)用得最多的一种访问权限控制协议。其示意图如下(这是最全面也最复杂的情况): 7 | 8 | ![RBAC示意图](./imgs/RBAC.png) 9 | 10 | RBAC的优点是比较通用,可以细粒度控制每个用户的权限。缺点是层级太多,实现起来笨重,使用起来也笨重。更多内容请参考这篇博客:RBAC权限系统设计之我见 11 | 12 | ### 1.2、WBAC权限模型 13 | WBAC(WBAC,Weight Based Access Control)是一种基于权值的权限控制协议,是我在设计开放平台项目中的权限系统时想出来的,这个名词也是我自己起的。思路大概如下: 14 | 15 | > 保留了RBAC的用户、角色和权限。每个用户唯一对应一种角色,每种角色包含一个weight属性,表示这种角色的权限等级值。权限表中的每一个权限也包含一个weight属性,表示该权限的权限等级值。 16 | > 17 | > 假如张三对应的角色R1的weight值为5,那么张三就获得了权限表中所有weight值小于等于5的权限。 18 | > 19 | > 这种方案的优势在于不用显式维护权限与用户的映射关系,判断权限清晰明了。 20 | 21 | > 但缺点也很明显,那就是没法细粒度控制某个用户拥有的权限。比如有一个权限A且weight为3,有角色R1且weight为5,张三的角色为R1,那么按照上面这种模型张三就一定拥有权限A。 22 | 23 | 最开始我按照RBAC协议设计了开放平台的权限模型,可后面与产品沟通后认为这种方案也满足要求,于是最终采用这种方案。 24 | 25 | 但是这种方案只对普通的第三方应用开发者适用,对于平台合作类型开发者,不采用这种权限模型,因为平台合作类型业务,一般都得由商务、技术和对方一起洽谈开放什么接口,并且对于不同的平台合作方开放的接口都可能不一样,这些接口(包括文档)不大可能公开给普通的第三方应用开发者。 26 | 27 | ## 2、其他 28 | 29 | ### 2.1、2的N次幂与位运算运用 30 | 有时候,我们可以给每个权限依次赋予权值2^0、2^1、2^2...,而某个用户可能拥有多个权限,那么就让这个用户也带上一个权值属性,并且让权值等于它拥有的所有权限的权值进行或运算,而要判断某个用户是否拥有某个权限时只需要让该用户的权值与那个权限的权值进行与运算,如果结果等于权限的权值,那么说明有该权限,反之则没有。 31 | 32 | 比如有三种权限P1:1,P2:2,P3:4,假如有用户U1拥有P1和P3两种权限,那么U1的权值为1 | 4 = 5。现在要判断U1是否有P1权限,5 & 1 = 1,所以有P1权限;再判断U1是否有P2权限,5 & 2 = 0,所以没有P1权限。 33 | 34 | ### 2.2、分层权限 35 | 36 | 在设计权限时有时候可以采用层次结构,一般的语法结构如下(支持通配符): 37 | 38 | ```html 39 | 资源:子资源:操作 40 | ``` 41 | 比如user、comment都是资源,comment是user的子资源,而操作可以抽象出一套通用操作比如create、read、update、delete。 42 | 43 | * 如果权限为user或者user:*,那么表示拥有对用户资源以及用户资源的子资源comment的所有权限 44 | * 如果权限为user:comment,则表示拥有对用户子资源comment的所有操作的权限,它等价于user:comment:* 45 | * 如果权限为user:comment:read,则表示拥有对用户子资源comment的读权限 46 | 47 | 另外也可以扩展通配符,比如支持非、或、与等,举个栗子:"user:comment:!read"可以表示没有对用户子资源comment的读权限。 48 | 49 | 还有MySQL的数据权限系统也是一种层次结构(MySQL也包含用户组、角色的概念,这里不涉及),在INFORMATION_SCHEMA这个数据库里,包含这么几张与权限相关的表: 50 | 51 | * USER_PRIVILEGES 52 | * SCHEMA_PRIVILEGES 53 | * TABLE_PRIVILEGES 54 | * COLUMN_PRIVILEGES 55 | 56 | #### 问: 57 | 如何判断某个用户root是否拥有对某个字段的UPDATE权限? 58 | 59 | #### 答: 60 | 先去USER_PRIVIELGES表中查找某个用户的权限,如果查到并拥有UPDATE权限,那么该用户就拥有对所有库、表、字段的该种类型的权限。 61 | 62 | 反之,如果没有,就再去SCHEMA_PRIVILEGES库权限表查用户是否拥有对那个字段所属数据库的UPDATE权限,如果有则用户root拥有那个字段的UPDATE权限。 63 | 64 | 反之,如果没有,就再去TABLE_PRIVILEGES表权限表查用户是否拥有对那个字段所属表的UPDATE权限,如果有责用户root拥有那个字段的UPDATE权限。 65 | 66 | ... 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | --------------------------------------------------------------------------------