问题分析
Spring Cloud OAuth2项目中访问获取access_token票证的端点/oauth/token时默认是不支持GET请求的,例如
get http://localhost:5000/oauth/token?grant_type=client_credentials&client_id=client2&client_secret=123456
错误如下:
{
"error": "method_not_allowed",
"error_description": "Request method 'GET' not supported"
}
解决方法
1、第1种方式是改为post请求,可以在postman中设置实现,如下:
2、第2种方式是在代码中允许使用GET请求获取票证,如下(这里只列出关键代码)
@Configuration
@EnableAuthorizationServer
public class OAuth2ServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}
}
同样执行成功,如下图: