[英]Refresh token is not returned in oauth/token response of spring
我正在尝试使用 Spring Boot 和 Spring Security 创建一个 Rest API。 以下是我为获取授权令牌所做的代码更改的详细信息:-
1]授权服务器配置
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private TokenStore tokenStore;
@Autowired
private UserApprovalHandler userApprovalHandler;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("my-trusted-client")
.authorizedGrantTypes("client_credentials", "password", "refresh_token" )
.authorities("ROLE_CLIENT").scopes("read","write","trust")
.secret("secret")
.accessTokenValiditySeconds(5000)
.refreshTokenValiditySeconds(6000).autoApprove(true);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("isAuthenticated()");
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Bean
@Autowired
public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
handler.setTokenStore(tokenStore);
handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
handler.setClientDetailsService(clientDetailsService);
return handler;
}
@Bean
@Autowired
public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
TokenApprovalStore store = new TokenApprovalStore();
store.setTokenStore(tokenStore);
return store;
}
}
2]资源服务器配置
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
private static final String RESOURCE_ID = "my_rest_api";
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(RESOURCE_ID).stateless(false);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable().and()
.authorizeRequests()
.antMatchers("/register").permitAll()
.antMatchers("/ex/**").authenticated();
}
}
3]方法安全配置
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@SuppressWarnings("unused")
@Autowired
private OAuth2SecurityConfiguration securityConfig;
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
当我通过邮递员提出请求时,会返回以下响应:-
请求网址:-
http://localhost:8090/oauth/token?grant_type=client_credentials&username=sr7&password=aA$gm12
收到回复:-
{
"access_token": "6e55f38f-4aad-4e84-97d2-24b30d39bf5e",
"token_type": "bearer",
"expires_in": 4999,
"scope": "read write trust"
}
请帮我弄清楚我在这里做错了什么,这使我无法获得刷新令牌和响应。
提前致谢。
根据规范,您通常(不应使用规范术语)在“客户端凭据”授予类型的情况下没有刷新令牌。 引用@chenrui 的回答:
client_credentials OAuth 授予服务器机器对机器身份验证的需要,因此无需刷新令牌。
结果,在 Spring Security OAuth 的
ClientCredentialsAccessTokenProvider
,supportsRefresh
返回false
,refreshToken
方法返回null
。
在“客户端凭据”中,裸客户端凭据用于获取访问令牌。
推荐阅读:
除了使用密码授予类型,尝试设置 TokenServices 对刷新令牌的支持。 这只是一个例子,我不知道默认令牌服务是否适用于您的情况。
@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
var tokenService = new DefaultTokenServices();
tokenService.setSupportRefreshToken(true);
endpoints
.accessTokenConverter(accessTokenConverter())
.tokenServices(tokenService)
.userDetailsService(userDetailsService)
.authenticationManager(authenticationManager);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.