<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>

                [TOC] # 自定義uaa-client-spring-boot-starter uaa-client-spring-boot-starter深度擴展spring cloud oauth2/ spring security封裝平臺通用資源服務能力,進行token 權限校驗。其作用是包裝資源服務器,作為獨立單元為api-gateway,user-center等提供安全保障 ## pom依賴 ![](https://img.kancloud.cn/d4/3b/d43ba51edb494003fbf44e35c5792578_1496x924.png) ## Spring Security Oauth 認證授權介紹 ### 基本設計 ![](https://img.kancloud.cn/28/18/281868d3d6f64df665978bf3229327d1_1101x517.png) ### 常用過濾器 ![](https://img.kancloud.cn/74/de/74de7856c4d3e3029b52eeb10941a822_1793x901.png) * 訪問者(Accessor)需要訪問某個資源(Resource)是這個場景最原始的需求,但并不是誰都可以訪問資源,也不是任何資源都允許任何人來訪問,所以中間我們要加入一些檢查和防護 * 在訪問資源的所經之路上,可能遇到細菌,病毒,不管怎么樣,對于要防護的資源來說最好的方法就是設關卡點,對于上圖的FilterSecurityInvation,MethodIncation,Jointpoint,這些在spring security oauth中統稱SecuredObjects * 我們知道在哪里設置關卡點最合適,下一步就是設置關卡,對應FileSecurityInterceptor,MethodSecurityInterceptor,AspectSecurityInterceptor, 這些關卡統一的抽象類是AbstractSecurityInterceptor * 有關卡點,關卡了以后,到底誰該攔截誰不應該呢,spring security oauth中由 AccessDecisionManager控制 * 最后一個問題,這個誰怎么定義,我們總得知道當前這個訪問者是誰才能告訴AccessDecisionManager攔截還是放行,在spring security oauth框架中AuthenticationManager將解決訪問者身份認證問題,只有確定你在冊了,才可以給授權訪問。AuthenticationManager,AccessDecisionManager,AbstractSecurityInterceptor屬于spring security框架的基礎鐵三角。 * 有了以上骨架,真正執行防護任務的其實是SecurityFilterChain中定于的一系列Filter,其中ExceptionTranslationFilter,它負責接待或者送客,如果訪問者來訪,對方沒有報上名來,那么,它就會讓訪客去登記認證(找AuthenticationManager做認證),如果對方報上名了,但認證失敗,那么請重新認證送客,送客的方式是拋出相應的Exception,所以名字叫做ExceptionTranslationFilter。 * 最后,這個filter序列中可能不滿足我們的需求,比如增加驗證碼,所以我們需要在其中穿插自己的Filter實現類,為定制和擴展Spring Security Oauth的防護體系。 * spring security內置的filter序列 ### 執行過濾鏈 ![](https://img.kancloud.cn/18/fe/18fe52e694ccccf09a6d9594d6ea788a_1270x348.png) ## 認證授權服務器處理流程 ![](https://img.kancloud.cn/e1/6a/e16a2ecf59a3b2c9b94a6b2f1c0de2ef_1111x632.png) ## 授權類處理流程圖 ![](https://img.kancloud.cn/3e/23/3e234e7710733533e20624b8ad577f52_2025x719.png) ## 功能 * 通用資源服務器校驗 * 通用的token校驗機制(TokenStore) * 通用的訪問控制(OpenAuthorizeConfigManager) * 通用的異常配置(SecurityHandlerConfig,ExceptionHandlerAdvice) ### 資源服務器 * 要訪問資源服務器受保護的資源需要攜帶令牌(從授權服務器獲得) * 客戶端往往同時也是一個資源服務器,各個服務之間的通信(訪問需要權限的資源)時需攜帶訪問令牌 * @EnableResourceServer引入OAuth2AuthenticationProcessingFilter過濾器 ![](https://img.kancloud.cn/8f/d8/8fd81c3e5e8ac3fda50aa7201684de39_2475x1096.png) * 通過繼承 ResourceServerConfigurerAdapter 類來配置資源服務器 ![](https://img.kancloud.cn/f6/75/f6757b5772312b3e9f411f95f09cb461_2276x1104.png) ### ResourceServerSecurityConfigurer 可配置屬性 * tokenServices:ResourceServerTokenServices 類的實例,用來實現令牌業務邏輯服務 * resourceId:這個資源服務的ID,這個屬性是可選的,但是推薦設置并在授權服務中進行驗證 * tokenExtractor 令牌提取器用來提取請求中的令牌 * 請求匹配器,用來設置需要進行保護的資源路徑,默認的情況下是受保護資源服務的全部路徑 * 受保護資源的訪問規則,默認的規則是簡單的身份驗證(plain authenticated) * 其他的自定義權限保護規則通過 HttpSecurity 來進行配置 * 使用 DefaultTokenServices 在資源服務器本地配置令牌存儲、解碼、解析方式 ## 核心代碼處理邏輯 認證授權核心的邏輯在于票據的生成和驗證,認證服務器負責生成票據,資源服務器負載驗證票據,二者統一調用DefaultTokenServices服務。 ![](https://img.kancloud.cn/6d/59/6d5984fccaca417aded4e24e4d483301_1402x589.png) ### DefaultTokenServices票據服務 DefaultTokenServices核心服務在于調用TokenStore生成驗證token ![](https://img.kancloud.cn/eb/91/eb911f3fb4880686730ef6f31d126507_2134x891.png) #### 票據token redis持久化 ![](https://img.kancloud.cn/db/d0/dbd0ebe9dac4d5e17b168bd36adcf1f0_2097x854.png) #### redis token 結構 ~~~ 排除refresh_token,主要key如下: * access:token值,value為string,這個主要是通過token值來獲取OAuth2AccessToken * auth:token值,value為string結構,這個主要用來獲取token的OAuth2Authentication,用來獲取相應的權限信息 * auth_to_access:OAuth2Authentication相關信息加密后的值,value為string結構 這個主要是通過OAuth2Authentication來獲取OAuth2AccessToken * client_id_to_access:clientId,value為list結構 這個主要是存儲了每個clientId申請的OAuth2AccessToken的集合方便用來審計和應急處理跟clientId相關的token * uname_to_access:clientId:userId,value的結構是list,存儲OAuth2AccessToken的集合 主要是為了通過clientId,userId來獲取OAuth2AccessToken集合,方便用來獲取及revoke approval ~~~ #### 票據token jwt存儲 ![](https://img.kancloud.cn/0f/35/0f3586968b9525f4239685031d776a6f_2470x1086.png) ## com.open.capacity.uaa.common.UAAClientAutoConfig的作用 * 提供OAuth2AuthenticationProcessingFilter保護我們的API接口 * 提供白名單免除OAuth2AuthenticationProcessingFilter校驗API接口 * 提供方法級權限校驗 ### 資源服務器核心配置 ![](https://img.kancloud.cn/b4/45/b445f86a859fa26cedfa5aedb4a9fe70_2303x1198.png) 通過以上配置可以達到以下效果 * 不帶token http://127.0.0.1:7000/users?page=1&limit=10 ![](https://img.kancloud.cn/87/63/8763013095dc8276f4a829ae662ba8b8_2082x437.png) * 帶不合法token http://127.0.0.1:7000/users?access_token=sds&page=1&limit=10 ![](https://img.kancloud.cn/cf/27/cf2767eeaa4b69893390039a6fa3dd93_1535x182.png) * 帶合法token http://127.0.0.1:7000/users?access_token=a3e3b8b3-5014-4eb5-b62e-6261f8afe4b5&page=1&limit=10 ![](https://img.kancloud.cn/bb/2d/bb2dac215543ca02b2a2e6641df24730_2560x383.png) [查看token的生成](10.%E8%87%AA%E5%AE%9A%E4%B9%89uaa-server-spring-boot-starter.md) ### 資源服務器核心權限過濾 ![](https://img.kancloud.cn/28/cf/28cfe2a0b8673ec889a8076a21351036_2445x946.png) ### 自定義安全門面設計 ![](https://img.kancloud.cn/1f/6a/1f6aa5c31edc185578ff6faf2956f7d7_2462x1031.png) ### 自定義安全策略 ![](https://img.kancloud.cn/7a/d3/7ad37669f8a7c0eb99590f19d0babbde_2484x1187.png) ### 白名單配置類 ![](https://img.kancloud.cn/4f/21/4f2129dfc88717d199b02faa9c85e586_2085x1212.png) ### 白名單配置文件 ![](https://img.kancloud.cn/c3/6e/c36e7ab5a4a35f86afb578931af7446c_2404x1129.png) ### websocket安全攔截 ![](https://img.kancloud.cn/e8/37/e8376e5c66db82cd64dc6376eab5e66d_2439x1208.png) ### 獲取當前登錄人 ![](https://img.kancloud.cn/8f/78/8f787452a7ffa21f68f238c2916d86e3_2131x896.png) ``` @GetMapping("/users/current") @ApiOperation(value = "根據access_token當前登錄用戶") public ResponseEntity<LoginAppUser> getLoginAppUser(@LoginUser(isFull = true) SysUser user) { return ResponseEntity.succeed(sysUserService.getLoginAppUser(user)); } ``` * 傳入token方式可以配合@LoginUser方式獲取當前登錄人 * 傳入userId username role的方式也可以配合@LoginUser方式獲取當前登錄人
                  <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>

                              哎呀哎呀视频在线观看