[英]Spring Security OAuth2 Resource Server Always Returning Invalid Token
我正在嘗試使用Spring庫運行基本的內存OAuth2服務器。 我一直在關注sparklr的例子 。
我目前已配置服務器,幾乎一切正常,但我無法從資源服務器訪問我的受限資源。
我的測試工作流程
訪問oauth授權的URI以啟動OAuth2流: http:// localhost:8080 / server / oauth / authorize?response_type = code&client_id = client
重定向到登錄頁面: http:// localhost:8080 / server / login
處理批准並重定向到我配置的重定向頁面,帶有代碼參數: http:// localhost:8080 / client?code = HMJO4K
使用基本身份驗證使用客戶端ID和密碼以及授權類型和代碼構造GET請求: http:// localhost:8080 / server / oauth / token?grant_type = authorization_code&code = HMJO4K
接收一個access_token和刷新令牌對象作為回報
{access_token:“f853bcc5-7801-42d3-9cb8-303fc67b0453”token_type:“bearer”refresh_token:“57100377-dea9-4df0-adab-62e33f2a1b49”expires_in:299范圍:“read write”}
嘗試使用access_token訪問受限資源: http:// localhost:8080 / server / me?access_token = f853bcc5-7801-42d3-9cb8-303fc67b0453
收到無效的令牌回復
{error:“invalid_token”error_description:“無效的訪問令牌:f853bcc5-7801-42d3-9cb8-303fc67b0453”}
再次POST到令牌uri以刷新令牌: http:// localhost:8080 / server / oauth / token?grant_type = refresh_token&refresh_token = 57100377-dea9-4df0-adab-62e33f2a1b49
收到一個新令牌
{access_token:“ed104994-899c-4cd9-8860-43d5689a9420”token_type:“bearer”refresh_token:“57100377-dea9-4df0-adab-62e33f2a1b49”expires_in:300范圍:“read write”}
我真的不確定我做錯了什么,但似乎除了訪問受限制的uri之外的一切都正常。 這是我的配置:
@Configuration
public class Oauth2ServerConfiguration {
private static final String SERVER_RESOURCE_ID = "oauth2-server";
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(SERVER_RESOURCE_ID);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and().requestMatchers()
.antMatchers("/me")
.and().authorizeRequests()
.antMatchers("/me").access("#oauth2.clientHasRole('ROLE_CLIENT')")
;
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthotizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private ClientDetailsService clientDetailsService;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client")
.resourceIds(SERVER_RESOURCE_ID)
.secret("secret")
.authorizedGrantTypes("authorization_code", "refresh_token")
.authorities("ROLE_CLIENT")
.scopes("read","write")
.redirectUris("http://localhost:8080/client")
.accessTokenValiditySeconds(300)
.autoApprove(true)
;
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.tokenStore(tokenStore())
.userApprovalHandler(userApprovalHandler())
.authenticationManager(authenticationManager)
;
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.realm("oauth");
}
@Bean
public ApprovalStore approvalStore() throws Exception {
TokenApprovalStore store = new TokenApprovalStore();
store.setTokenStore(tokenStore());
return store;
}
@Bean
public UserApprovalHandler userApprovalHandler() throws Exception {
TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
handler.setClientDetailsService(clientDetailsService);
handler.setTokenStore(tokenStore());
return handler;
}
}
}
有什么我想念的或者我接近這個錯誤嗎? 任何幫助將不勝感激。
您的步驟#6是錯誤的 - 訪問令牌不應該在URL中發送,因為它以這種方式易受攻擊。 比GET更好,使用POST。
此外,我不明白你的步驟#1 - 為什么你打電話/ oauth / authorize? 當您嘗試獲取受保護資源時,應該隱式執行此操作。 我的意思是,你的流程應該從:
嘗試使用access_token訪問受限資源: http:// localhost:8080 / server / me
然后談判將在“幕后”開始:重定向到“/ oauth / authorize”等。
此外,在步驟#8中,請注意您不是要求“另一個訪問令牌”,而是要求“刷新令牌”。 好像您的訪問令牌已過期。
注意 :身份提供者和資源服務器應該共享tokenStore! 閱讀此處: Spring Security OAuth2純資源服務器
HTH
問題最終是資源服務器和授權服務器沒有獲得相同的令牌存儲引用。 不確定接線是如何工作不正確的,但在配置類中使用固定對象就像一個魅力。 最終,我將轉移到持久性支持的令牌存儲,這可能不會有任何問題。
感謝@OhadR的答案和幫助!
最終,我簡化了配置,通過相同的工作流程,並且成功了
@Configuration
public class Oauth2ServerConfiguration {
private static final String SERVER_RESOURCE_ID = "oauth2-server";
private static InMemoryTokenStore tokenStore = new InMemoryTokenStore();
@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore).resourceId(SERVER_RESOURCE_ID);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("/me").and().authorizeRequests().antMatchers("/me").access("#oauth2.hasScope('read')");
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore).approvalStoreDisabled();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client")
.authorizedGrantTypes("authorization_code","refresh_token")
.authorities("ROLE_CLIENT")
.scopes("read")
.resourceIds(SERVER_RESOURCE_ID)
.secret("secret")
;
}
}
}
任何偶然發現這篇文章的人,我建議更多地考慮單元測試而不是完整的sparklr / tonr示例,因為它有很多額外的配置,不一定需要開始。
這對我有用:
@Configuration
public class Oauth2ServerConfiguration {
private static final String SERVER_RESOURCE_ID = "oauth2-server";
@Autowired
private TokenStore tokenStore;
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore).resourceId(SERVER_RESOURCE_ID);
}
@Override
public void configure(HttpSecurity http) throws Exception {
// ... Not important at this stage
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore).approvalStoreDisabled();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//... Not important at this stage
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.