<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # **認證中心* auth-server* auth-server在項目中的位置 ![](https://img.kancloud.cn/43/3a/433a1aa4c85d69f6cf32671c8c6fbf54_1055x734.png) auth-server在項目用的作用 ![](https://box.kancloud.cn/67e561ea8cb7eeab5f62d74f4956a5ab_1064x532.png) ## 認證服務器頒發token核心流程 * /oauth/token?grant_type=password #請求授權token * /oauth/token?grant_type=refresh_token #刷新token * /oauth/check_token #校驗token * /oauth/authorize #授權碼模式 ## OAUTH 核心處理調用關系 ![](https://box.kancloud.cn/153c89b5bdf9cef58e6bf51005624413_857x461.png) ![](https://box.kancloud.cn/61db9c23a4af534a31da34b273fedb9b_927x531.png) 根據以上流程重寫了密碼模式,(僅用來學習流程,并沒有真正調用) ![](https://box.kancloud.cn/a3683f7beb1c62446e5c517b26696ce1_1751x595.png) 根據以上流程重寫了客戶端模式(僅用來學習流程,并沒有真正調用) ![](https://box.kancloud.cn/65c0a102ac6e419b4695ae0e70c45426_1648x622.png) # 代碼結構 認證服務器@EnableAuthorizationServer ![](https://box.kancloud.cn/4327a1d87e4e8e79b06dbaac9ce51b65_1738x607.png) ### AuthorizationServer 認證服務器 AuthorizationServer配置主要是覆寫如下的三個方法,分別針對endpoints、clients、security配置。 認證服務同時也是一個資源服務器@EnableResourceServer ### ResourceServer資源服務器配置 ![](https://box.kancloud.cn/0868208721326fb41dffc032a9e18f07_1786x638.png) # auth-server核心表 oauth_client_details --> JdbcClientDetailsService 處理 ![](https://box.kancloud.cn/35a46621f0574c3e6165dda495e416c1_1916x543.png) 為了性能,重構JdbcClientDetailsService,oauth_client_details內容緩存redis中map中 ![](https://box.kancloud.cn/58c840241be2a8062af4f9e50351e71d_1901x844.png) ## 授權碼存取策略 授權碼緩存redis里面,RedisAuthorizationCodeServices,解決啟動多個認證中心授權碼問題 ![](https://box.kancloud.cn/da07dc8a5bfb4f0eef8a17213c214d60_1577x654.png) ## 異常通用處理 ![](https://box.kancloud.cn/125f255b3f58f070949623342f963a8e_1776x801.png) ## token票據服務 ![](https://box.kancloud.cn/55cafd11e73a34a76914b4c3ca61ecd2_1771x702.png) ## spring security 自定義用戶驗證邏輯,取sys_user表 ![](https://box.kancloud.cn/73507e4063206e159c8d088fdc2663dc_1756x668.png) ## 服務間鑒權處理,feigin調用解決token傳遞問題 ![](https://box.kancloud.cn/b8ef91cfaad8bd86632cf3e46977d68a_1744x686.png) **注意,基于密碼模式的方式需要先啟動用戶中心,通過feigin調用** ![](https://img.kancloud.cn/81/c3/81c3fb329d5cca970c6f6c094bb6cb12_921x556.png) ### feign原理 ![](https://img.kancloud.cn/18/4d/184dd90c81a44e8e46fb34f6cc279600_1211x585.png) ### feign聲明 ![](https://box.kancloud.cn/8b0f61a737fa07e771f61d6678b8c65a_1752x726.png) ### 服務調用 ![](https://img.kancloud.cn/39/f8/39f8174b9e577392f2a0ee98211f069c_739x762.png) 啟動user-center后測試auth-server的密碼模式 ![](https://box.kancloud.cn/e0c1166c9435ca32629ff625343ced1c_1907x944.png) **如果是客戶端模式,可以通過swagger測試** 訪問認證中心地址http://130.75.131.208:8000/document.html ![](https://box.kancloud.cn/1cbf53f07d1d7d32d9af307225aa066e_1911x904.png) 需要帶上token,否則無法訪問auth-server的資源 ![](https://box.kancloud.cn/61ba5a06eea91e2a16dab4cb0cc48c92_1174x528.png) ## 權限訪問白名單 ### 配置文件 ![](https://box.kancloud.cn/141d93cff0be2656f6310faa239a43cd_1742x673.png) ### 處理類 ![](https://box.kancloud.cn/cf95e9daa6644104fa4c78bf5b96d829_1738x715.png) ### 處理/oauth/authorize 核心類AuthorizationEndpoint ![](https://box.kancloud.cn/d970243ea0fac42f0784d4e7a2895dec_1118x678.png) #### 授權碼模式 獲取授權碼 http://127.0.0.1:8000/oauth/authorize?client_id=owen&redirect_uri=http://127.0.0.1:9997/dashboard/login&state=abc&scope=app&response_type=code 用授權碼換token ![](https://box.kancloud.cn/fbfa8acafaf99603a1169d96fbb4d39b_1637x352.png) #### 密碼模式 >基本流程就是: 通過ClientDetailsSevice根據ClientId查找對應的Client,由Oauth2RequestFactory生成TokenRequest。檢查是否能對應的找到Client,沒有就拋出異常。 檢查參數,都沒問題了最后就調用Ganter的grant方法來生成Token并返回。 >密碼模式,即用戶提供username,password,clientId,clientSecret,grantType=password等信息,請求/oauth/token,獲得access_token,用戶即可通過access_token訪問資源 訪問/oauth/token >對應代碼處理流程 此時FilterChainProxy的filter順序如下。重要的Filter有ClientCredentialsTokenEndpointFilter和BasicAuthenticationFilter,前者從request parameters中抽取client信息,后者從header Authorization Basic XXXX中抽取client信息。 ![](https://box.kancloud.cn/c2da6efd9ac36097796aafc414f93246_1648x843.png) **ClientCredentialsTokenEndpointFilter**會從parameter中抽取client_id,client_secret信息,并進行client的身份驗證。 ![](https://box.kancloud.cn/ba4b7bf3c47c4dc5459d4192259e9fc8_833x494.png) ![](https://box.kancloud.cn/9963569242229cd877dd1abb6a522b2a_912x739.png) 訪問/oauth/token,都事先驗證了client信息,并作為authentication存儲在SecurityContextHolder中。傳遞到TokenEndPoint的principal是client,paramters包含了user的信息和grantType。 ![](https://box.kancloud.cn/62a46e5d5b3feb8ec9ca87f47a679c87_842x490.png) ![](https://box.kancloud.cn/4e1fdb001e75b7d07dfd98d88812099b_961x153.png) ### 處理/oauth/token核心類TokenEndpoint ![](https://box.kancloud.cn/95a0c28a24999a030093a8ca832980d8_1206x532.png) ![](https://box.kancloud.cn/cd129821b9eb34b2145923e976062188_961x683.png) #### security認證原理 >認證的工作是交給AuthenticationManager去做,AuthenticationManager下有多個認證器 AuthenticationProvider 只要其中一個AuthenticationProvider通過認證就算登陸成功,而且在認證器中拋出異常,無法終止認證流程只是算該認證器未通過。 第一個config就算配置了一個AuthenticationManagerBuilder 這個類會生成一個 AuthenticationManager和DaoAuthenticationProvider認證器,認證調用userdetailservice 的loadUserByUsername方法來和你傳入的username passworde做比較,password 是通過BCryptPasswordEncoder來做編碼后比較的,這樣做是為了提高安全性。 ## 通過arthas理解OAUTH密碼模式 ![](https://box.kancloud.cn/c166a2246d88ca8cc31b64f7d32a77f6_1563x394.png) ![](https://box.kancloud.cn/b73ab6847f26a3fc8e545ac8d8360ab0_1547x314.png) ![](https://box.kancloud.cn/a2e57bed6f98c43750f8b9d782ceb134_1548x127.png) ## 代碼分析 ![](https://img.kancloud.cn/b7/67/b767e7d66c6d0c16c6b78465553cd87a_663x420.png) ### 認證授權效果圖 ![](https://box.kancloud.cn/abab658b5e65084b2aead66130dc90f6_1922x950.gif) ### redis token 結構 ``` 排除refresh_token,主要key如下: * auth_to_access:OAuth2Authentication相關信息加密后的值,value為string結構 這個主要是通過OAuth2Authentication來獲取OAuth2AccessToken * auth:token值,value為string結構 這個主要用來獲取token的OAuth2Authentication,用來獲取相應的權限信息 * client_id_to_access:clientId,value為list結構 這個主要是存儲了每個clientId申請的OAuth2AccessToken的集合 方便用來審計和應急處理跟clientId相關的token * access:token值,value為string 這個主要是通過token值來獲取OAuth2AccessToken * uname_to_access:clientId:userId,value的結構是list 存儲OAuth2AccessToken的集合 主要是為了通過clientId,userId來獲取OAuth2AccessToken集合,方便用來獲取及revoke approval ``` ## 項目pom版本依賴 ![](https://box.kancloud.cn/f61d7429e13a2592f0e3b4721500663a_1768x667.png) 左側2.0.x版本 左側1.5.9版本差異 ![](https://box.kancloud.cn/6c10f0c1ae40e1bdbf5ba48f35fb1f4b_920x816.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看