簡體   English   中英

微服務和Spring Security OAuth2

[英]Microservices and Spring Security OAuth2

我已經在另一個項目中運行了OAuth2授權服務器。 現在我需要使用OAuth2來保護幾個簡單的spring-boot rest-servers。 但是我發現Spring文檔在分離授權和資源服務器時確實非常有限。

我還發現了幾個問題, 答案是“只要它們共享相同的tokenStore數據源,它們就可以是不同的盒子”。 這真的可以嗎? 這怎么可能對微服務有用? 每個休息服務都需要實現它自己的OAuth授權服務器,這似乎是一件非常奇怪的事情。

那么如何為引用遠程oauth授權服務器的spring-boot rest-endpoints設置Oauth2.0安全性(可能甚至不用Spring編寫)?

這個名為RemoteTokenServices的東西似乎很有希望,但根本沒有記錄。

配置你的auh服務器::

ClientDetailsServiceConfigurer為資源服務器創建新的clientDetails。 這將用於配置RemoteTokenService

在資源服務器中配置Spring Security OAuth2:

創建一個使用@EnableWebSecurity@Configuration @EnableWebSecurity @Configuration注釋的類,並擴展WebSecurityConfigurerAdapter

@Configuration
@EnableWebSecurity
protected static class ResourceConfiguration extends WebSecurityConfigurerAdapter {
  // methods        
}

使用@Bean注釋創建一個方法,該方法將返回TokenService實例,該實例將用於創建AuthenticationManager

在此方法中創建RemoteTokenService的實例並設置clientId,client_secret,checkTokenEndpointUrl和DefaultAccessTokenConverterWithClientRoles (此類是我們在OAuth2服務器中驗證accessToken時獲取client_authority的實現。)

@Bean
public ResourceServerTokenServices tokenService() {
    RemoteTokenServices tokenServices = new RemoteTokenServices();
    tokenServices.setClientId("resource_id");
    tokenServices.setClientSecret("resource_secret");
    tokenServices.setCheckTokenEndpointUrl("http://<server-url>: <port>/oauth/check_token");
    return tokenServices;
}

覆蓋authenticationManagerBean()方法並使用@Bean注釋它,並返回注入了OAuth2AuthenticationManagerTokenService實例。

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager();
    authenticationManager.setTokenServices(tokenService());
    return authenticationManager;
}

創建一個使用@EnableResourceServer@Configuration @EnableResourceServer @Configuration注釋的類並擴展ResourceServerConfigurerAdapter

@Configuration
@EnableResourceServer
protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  // Mehotds
}

覆蓋配置方法構成超類以配置資源服務器。 不同的配置器配置資源服務器。

ResourceServerSecurityConfigurer :配置Resource_id。

HttpSecurity :這將配置安全過濾器,告訴用戶需要對受保護的URL(API)進行身份驗證。

@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    resources.resourceId("resource_id");
}

@Override
public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
     .authorizeRequests()
     .antMatchers("/**").authenticated()
     .and()
     .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    // @formatter:on
}

.antMatcher("/**").authenticated()這一行將保護資源服務器的每個api url。 .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)不會創建會話。

PS ::如果有什么不對的話請告訴我。

如果資源服務器有某種方式來驗證訪問令牌是真實的(由授權服務器發布)以及解碼它的某種方式(它可能需要知道什么,那么兩者都必須共享同一個數據庫並不是真的。例如,它授予的范圍。 OAuth2規范沒有說明如何實現這一目標。

如果資源與授權服務器處於同一進程中,則共享數據是一個簡單的選擇。 否則,它需要某種方式來翻譯令牌。 如果令牌只是一個隨機字節的不透明字符串,那么顯然它必須將它交換為真實信息。 這就是RemoteTokenServices功能。 授權服務器公開/check_token端點以允許解碼令牌。

另一種方法是實際編碼令牌中的信息,並讓授權服務器對其進行數字簽名。 然后資源服務器可以解碼並驗證令牌本身,只要它理解格式即可。

我建議您查看Cloudfoundry UAA ,它提供了一個開箱即用的授權服務器,它使用簽名的JWT令牌實現(它還公開了/check_token端點)。 令牌概述API文檔可能是一個很好的起點。

可以在此處找到RemoteTokenService的工作示例。

有關check_token api的更多詳細信息,請參閱org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint.java

在資源服務器端, OAuth2AuthenticationProcessingFilter通過調用OAuth2AuthenticationManager.authenticate()方法驗證OAuth2令牌,該方法調用RemoteTokenServices.loadAuthentication()以驗證來自Auth服務器的令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM