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

                > 本博文翻譯自[官方文檔](https://projects.spring.io/spring-security-oauth/docs/oauth2.html)。 Spring Security 官方已經棄用OAuth2項目了,你知道嗎?Spring Security5.2.x版本以上提供了最新的方式。 ## 一、關于授權服務器 ### AuthorizationServerConfigurer的配置 使用注解`@EnableAuthorizationServer`啟用授權服務器。授權服務器可以對ClientDetailsService、AuthorizationServerSecurity、AuthorizationServerEndpoints進行配置。 分別對應三個配置類: - ClientDetailsServiceConfigurer: a configurer that defines the client details service. Client details can be initialized, or you can just refer to an existing store. - `AuthorizationServerSecurityConfigurer`: tolen獲取的安全性設置。 - `AuthorizationServerEndpointsConfigurer`: 定義了授權、token獲取的相關服務。 ### 配置Client Details `ClientDetailsServiceConfigurer`會被`AuthorizationServerConfigurer`回調。Client Details Service可以被設置為基于內存或者是JDBC的實現,關鍵屬性包括: - clientId: (非空)Client編號. - secret: client secret. - scope: 客戶端訪問范圍限制。如果scope設置為空,則不進行限制。 - authorizedGrantTypes: Grant types that are authorized for the client to use. Default value is empty. - authorities: Authorities that are granted to the client (regular Spring Security authorities). ### 管理token `AuthorizationServerTokenServices`用來管理token存儲的實現,默認存儲是在內存中的。 - InMemoryTokenStore:適合單節點開發環境; - JdbcTokenStore :各個服務之間可以共享token存儲; - JWTTokenStore: ### 配置EndPoint URLS > 什么是EndPoint URLS?作為小白的我,經常感到水土不服。 框架自帶的路徑: - `/oauth/authorize`:認證地址; - `/oauth/token`:token地址; - `/oauth/confirm_access`:用戶授權的時候打開的地址; - `/oauth/error`:授權錯誤時打開的地址; - `/oauth/check_token`:資源服務器解密驗證token; - `/oauth/token_key`:使用JWT token時暴露公有key進行token驗證; ## 二、資源(Resource)服務器配置 資源服務器是基于token保護資源服務的。Spring Oauth協議提供Security認證Filter(`OAuth2AuthenticationProcessingFilter`的實現)來實現這個功能。 開啟方法:在配置類中添加注解:`@EnableResourceServer`。可用配置有: - tokenServices: 定義token services的一個bean (ResourceServerTokenServices的實例). - resourceId: 資源id (可選,但建議使用,如果存在auth服務,將由auth服務器驗證). - other extension points for the resourecs server (e.g. tokenExtractor for extracting the tokens from incoming requests) - request matchers for protected resources (defaults to all) - access rules for protected resources (defaults to plain "authenticated") - other customizations for the protected resources permitted by the HttpSecurity configurer in Spring Security ## 三、OAuth2.0 Client > 基本概念: - `OAuth2ProtectedResourceDetails`:被保護的資源bean; ### 被保護資源配置 - `id`: 資源的ID。客戶端可以用來查找資源;這個ID在OAuth protocol中是無用的,它也用作bean的ID。 - `clientId`: OAuth client的id. This is the id by which the OAuth provider identifies your client. - `clientSecret`: The secret associated with the resource. By default, no secret is empty. - `accessTokenUri`: The URI of the provider OAuth endpoint that provides the access token. - `scope`: 多個scope之間使用','連接的字符串配置,可以訪問資源資源。默認不指定任何的scope。 - `clientAuthenticationScheme`: The scheme used by your client to authenticate to the access token endpoint. Suggested values: "http_basic" and "form". Default: "http_basic". See section 2.1 of the OAuth 2 spec. > Different grant types have different concrete implementations of OAuth2ProtectedResourceDetails (e.g. ClientCredentialsResource for "client_credentials" grant type). For grant types that require user authorization there is a further property: - `userAuthorizationUri`: The uri to which the user will be redirected if the user is ever needed to authorize access to the resource. Note that this is not always required, depending on which OAuth 2 profiles are supported. ### 客戶端的配置 使用注解:`@EnableOAuth2Client`,注解的背后做了兩件事情: - 創建了一個ID為`oauth2ClientContextFilter`的filter,用來存儲當前請求和上下文。在認證請求過程中用于管理OAuth認證url的重定向。 - 請求過程中,創建了一個類型為`AccessTokenRequest`的bean,授權碼可以用這個bean來防止客戶端與單個用戶的狀態沖突。 `AccessTokenRequest`在OAuth2RestTemplate是這樣的使用的: ```java @Autowired private OAuth2ClientContext oauth2Context; @Bean public OAuth2RestTemplate sparklrRestTemplate() { return new OAuth2RestTemplate(sparklr(), oauth2Context); } ``` ### 資源訪問 在Spring3中RestTemplate是訪問資源時推薦的方式,Spring Security對其進行了拓展,只需要實例化`OAuth2ProtectedResourceDetails`就好了。 ### 客戶端token的保持 客戶端不需要持久化token,但是客戶端重啟時用戶不需要獲取一個新的token。 `ClientTokenServices` 接口定義了必要的功能用來持久化 OAuth 2.0 tokens以區分用戶。系統提供了一種JDBC的實現。想要使用這個特性,需要向OAUthRestTemplate提供額外的`TokenProvider`配置。 ```java @Bean @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) public OAuth2RestOperations restTemplate() { OAuth2RestTemplate template = new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(accessTokenRequest)); AccessTokenProviderChain provider = new AccessTokenProviderChain(Arrays.asList(new AuthorizationCodeAccessTokenProvider())); provider.setClientTokenServices(clientTokenServices()); return template; } ``` ## 感慨一下 > 之前在網絡上搜了不少關于Spring Security Oauth2相關的資料,理解一直不甚透徹。今日官網文檔一看,發現Spring Security Oauth2已經是Spring4.x時代的產物了。說明自己學習新知識的習慣還是不太成熟。另外說一點,看英文文檔的表述比谷歌的自動翻譯要好太多了。
                  <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>

                              哎呀哎呀视频在线观看