【发布时间】:2015-10-05 19:28:46
【问题描述】:
我想使用来自使用 oauth2 保护他的资源的服务器的 REST Web 服务。
我使用 Spring boot (JHipster)。
要做到这一点,我在SecurityConfiguration 类中有这个:
@Value("${oauth.resource:http://sercverUsingOAuth2}")
private String baseUrl;
@Value("${oauth.authorize:http://sercverUsingOAuth2/rest/oauth/token}")
private String authorizeUrl;
@Value("${oauth.token:http://sercverUsingOAuth2/rest/oauth/token}")
private String tokenUrl;
@Bean
public OAuth2RestOperations oauth2RestTemplate() {
AccessTokenRequest atr = new DefaultAccessTokenRequest();
return new OAuth2RestTemplate(resource(),
new DefaultOAuth2ClientContext(atr));
}
@Bean
protected OAuth2ProtectedResourceDetails resource() {
AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
resource.setAccessTokenUri(tokenUrl);
resource.setUserAuthorizationUri(authorizeUrl);
resource.setClientId("client_id");
resource.setClientSecret("client_secret");
resource.setGrantType("grant_type");
return resource;
}
此类 (SecurityConfiguration) 使用以下注释:
@Configuration
@EnableWebSecurity
@EnableOAuth2Client
这是我的controller(Spring MVC):
@RestController
@RequestMapping("/consume")
public class MyContrtoller {
@Inject
private OAuth2RestOperations oauth2RestTemplate;
@RequestMapping(value = "/oauth2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<DataModel> getProducts() {
ResponseEntity<MyModel> forEntity = oauth2RestTemplate
.getForEntity("http://sercverUsingOAuth2/rest/resourceToConsume",
MyModel.class);
return forEntity.getBody().getData();
}
}
但是,当我想使用我的网络服务 (http://myHost/consume/oauth2) 时,我得到了这个异常:
org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException:
Unable to obtain a new access token for resource 'null'. The provider manager
is not configured to support it.
我用谷歌搜索过,我发现了这个:
但这对我没有帮助。
谢谢。
【问题讨论】:
-
你会发现这个话题很有用:stackoverflow.com/a/28278293/604218
-
感谢@Reberto 的回复。
-
我不需要用户名和用户密码来使用 Web 服务,因为托管此 ws 的服务器允许我使用以下方式使用此 ws:client_id、client_secret 和 grant_type。 PS:当我使用其他客户端应用程序时,我得到了令牌表单服务器,但使用我的 java 代码却不能。
-
这个问题你解决了吗?
-
不,我没有找到任何解决方案!
标签: spring spring-mvc oauth-2.0 spring-boot jhipster