我想獲得一個使用Spring庫運行的基本內存OAuth2服務器。我一直在關注sparklr example。春季安全OAuth2資源服務器總是返回無效令牌
我目前已經配置了服務器,幾乎所有的工作都正常,但是我無法從資源服務器訪問我的受限資源。
我的測試流程:
訪問授權URI的OAuth的開始OAuth2流程:http://localhost:8080/server/oauth/authorize?response_type=code&client_id=client
重定向到登錄頁面:http://localhost:8080/server/login
辦理有關報批和重定向到我配置的重定向頁面w /代碼參數: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: 「承載」 refresh_token: 「57100377-dea9-4df0-ADAB-62e33f2a1b49」 expires_in:299 範圍: 「讀寫」 }
嘗試使用的access_token訪問受限制的資源:http://localhost:8080/server/me?access_token=f853bcc5-7801-42d3-9cb8-303fc67b0453
,接收無效令牌回覆
{ 錯誤: 「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: 「承載」 refresh_token: 「57100377-dea9-4df0-ADAB-62e33f2a1b49」 expires_in:300 範圍: 「讀寫」 }
我真的不知道我做錯了,但現在看來,比訪問受限制的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;
}
}
}
有什麼我失蹤或我接近這個不正確嗎?任何幫助將不勝感激。
一些測試後,我仍然無法得到它的工作。不知何故,似乎ResourceServer沒有加載正確的令牌存儲或其他東西。我有一個令牌存儲bean並將它自動裝配到一個控制器中,該控制器將打印我的客戶端的令牌,這很好。我將同一令牌存儲bean(使用唯一限定符)自動裝入自定義身份驗證管理器,並且無法在存儲中找到任何託管。我真的不知道這是如何可能的,除非有隱含的會話範圍? – jyore