paulwong

          使用Spring Cloud Security OAuth2搭建授權服務

          Spring Cloud Security OAuth2 是 Spring 對 OAuth2 的開源實現,優點是能與Spring Cloud技術線無縫集成,如果全部使用默認配置,開發者只需要添加注解就能完成 OAuth2 授權服務的搭建。

          1. 添加依賴

          授權服務是基于Spring Security的,因此需要在項目中引入兩個依賴:

          <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-security</artifactId>
          </dependency>

          <dependency>
                   <groupId>org.springframework.cloud</groupId>
                   <artifactId>spring-cloud-starter-oauth2</artifactId>
           </dependency>


          前者為 Security,后者為Security的OAuth2擴展。

          2. 添加注解和配置

          在啟動類中添加@EnableAuthorizationServer注解:

          @SpringBootApplication
          @EnableAuthorizationServer
          public class AlanOAuthApplication {
              public static void main(String[] args) {
                  SpringApplication.run(AlanOAuthApplication.class, args);
              }
          }


          完成這些我們的授權服務最基本的骨架就已經搭建完成了。但是要想跑通整個流程,我們必須分配 client_idclient_secret才行。Spring Security OAuth2的配置方法是編寫@Configuration類繼承AuthorizationServerConfigurerAdapter,然后重寫void configure(ClientDetailsServiceConfigurer clients)方法,如:

          @Override
              public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                  clients.inMemory() // 使用in-memory存儲
                          .withClient("client") // client_id
                          .secret("secret") // client_secret
                          .authorizedGrantTypes("authorization_code") // 該client允許的授權類型
                          .scopes("app"); // 允許的授權范圍
              }


          3. 授權流程

          訪問授權頁面:

          localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com


          此時瀏覽器會讓你輸入用戶名密碼,這是因為 Spring Security 在默認情況下會對所有URL添加Basic Auth認證。默認的用戶名為user, 密碼是隨機生成的,在控制臺日志中可以看到。

          oauth2

          畫風雖然很簡陋,但是基本功能都具備了。點擊Authorize后,瀏覽器就會重定向到百度,并帶上code參數:

          這里寫圖片描述

          拿到code以后,就可以調用

          POST/GET http://client:secret@localhost:8080/oauth/token
          • 1

          來換取access_token了:

          curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=authorization_code&code=Li4NZo&redirect_uri=http://www.baidu.com' "http://client:secret@localhost:8080/oauth/token"

          返回如下:

          {
            "access_token": "32a1ca28-bc7a-4147-88a1-c95abcc30556",
            "token_type": "bearer",
            "expires_in": 2591999,
            "scope": "app"
          }

          到此我們最最基本的授權服務就搭建完成了。然而,這僅僅是個demo,如果要在生產環境中使用,還需要做更多的工作。

          4. 使用MySQL存儲access_token和client信息

          把授權服務器中的數據存儲到數據庫中并不難,因為 Spring Cloud Security OAuth 已經為我們設計好了一套Schema和對應的DAO對象。但在使用之前,我們需要先對相關的類有一定的了解。

          4.1 相關接口

          Spring Cloud Security OAuth2通過DefaultTokenServices類來完成token生成、過期等 OAuth2 標準規定的業務邏輯,而DefaultTokenServices又是通過TokenStore接口完成對生成數據的持久化。在上面的demo中,TokenStore的默認實現為InMemoryTokenStore,即內存存儲。 對于Client信息,ClientDetailsService接口負責從存儲倉庫中讀取數據,在上面的demo中默認使用的也是InMemoryClientDetialsService實現類。說到這里就能看出,要想使用數據庫存儲,只需要提供這些接口的實現類即可。慶幸的是,框架已經為我們寫好JDBC實現了,即JdbcTokenStoreJdbcClientDetailsService

          4.2 建表

          要想使用這些JDBC實現,首先要建表。框架為我們提前設計好了schema, 在github上:https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql

          在使用這套表結構之前要注意的是,對于MySQL來說,默認建表語句中主鍵是varchar(255)類型,在mysql中執行會報錯,原因是mysql對varchar主鍵長度有限制。所以這里改成128即可。其次,語句中會有某些字段為LONGVARBINARY類型,它對應mysql的blob類型,也需要修改一下。

          4.3 配置

          數據庫建好后,下一步就是配置框架使用JDBC實現。方法還是編寫@Configuration類繼承AuthorizationServerConfigurerAdapter

          @Autowired
              private AuthenticationManager authenticationManager;

              @Autowired
              private DataSource dataSource;
              @Bean // 聲明TokenStore實現
              public TokenStore tokenStore() {
                  return new JdbcTokenStore(dataSource);
              }
              @Bean // 聲明 ClientDetails實現
              public ClientDetailsService clientDetails() {
                  return new JdbcClientDetailsService(dataSource);
              }
              @Override // 配置框架應用上述實現
              public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
                  endpoints.authenticationManager(authenticationManager);
                  endpoints.tokenStore(tokenStore());

                  // 配置TokenServices參數
                  DefaultTokenServices tokenServices = new DefaultTokenServices();
                  tokenServices.setTokenStore(endpoints.getTokenStore());
                  tokenServices.setSupportRefreshToken(false);
                  tokenServices.setClientDetailsService(endpoints.getClientDetailsService());
                  tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());
                  tokenServices.setAccessTokenValiditySeconds( (int) TimeUnit.DAYS.toSeconds(30)); // 30天
                  endpoints.tokenServices(tokenServices);
              }

          完成這些后,框架就會將中間產生的數據寫到mysql中了。oauth_client_details是client表,可以直接在該表中添加記錄來添加client: 
          這里寫圖片描述

          4.4 需要注意的地方

          這里不得不說 Spring 設計有一個奇葩地的方。注意看oauth_access_token表是存放訪問令牌的,但是并沒有直接在字段中存放token。Spring 使用OAuth2AccessToken來抽象與令牌有關的所有屬性,在寫入到數據庫時,Spring將該對象通過JDK自帶的序列化機制序列成字節直接保存到了該表的token字段中。也就是說,如果只看數據表你是看不出access_token的值是多少,過期時間等信息的。這就給資源服務器的實現帶來了麻煩。我們的資源提供方并沒有使用Spring Security,也不想引入 Spring Security 的任何依賴,這時候就只能將 DefaultOAuth2AccessToken的源碼copy到資源提供方的項目中,然后讀取token字段并反序列化還原對象來獲取token信息。但是如果這樣做還會遇到反序列化兼容性的問題,具體解決方法參考我另一篇博文:http://blog.csdn.net/neosmith/article/details/52539614

          5. 總結

          至此一個能在生產環境下使用的授權服務就搭建好了。其實我們在實際使用時應該適當定制JdbcTokenStoreClientDetailsService來實適應業務需要,甚至可以直接從0開始實現接口,完全不用框架提供的實現。另外,Spring 直接將DefaultOAuth2AccessToken序列化成字節保存到數據庫中的設計,我認為是非常不合理的。或許設計者的初衷是保密access_token,但是通過加密的方法也可以實現,完全不應該直接扔字節。不過通過定制TokenStore接口,我們可以使用自己的表結構而不拘泥于默認實現。

          http://blog.csdn.net/tracker_w/article/category/6360121

          http://blog.csdn.net/neosmith/article/details/52539927

          posted on 2016-09-16 18:22 paulwong 閱讀(8752) 評論(0)  編輯  收藏 所屬分類: MICROSERVICESPRING CLOUD

          主站蜘蛛池模板: 永城市| 绍兴县| 天台县| 永川市| 武功县| 乌海市| 伊宁县| 河北省| 武夷山市| 得荣县| 丘北县| 施秉县| 乐业县| 麻栗坡县| 浪卡子县| 尖扎县| 哈密市| 定结县| 内黄县| 凤台县| 保亭| 普兰县| 遂昌县| 靖州| 扶沟县| 临汾市| 忻城县| 广西| 襄城县| 凤翔县| 永嘉县| 荃湾区| 岳西县| 泌阳县| 洛川县| 五家渠市| 灵石县| 洪泽县| 谢通门县| 民丰县| 那曲县|