我想获得一个使用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