spring - 使用 Spring oauth2 使用受 OAuth 保护的 REST Web 服务

标签 spring spring-mvc oauth-2.0 spring-boot jhipster

我想使用来自服务器的 REST Web 服务,该服务器使用 oauth2 保护其资源。

我使用 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.

我用谷歌搜索,发现了这个:

但这对我没有帮助。

谢谢。

最佳答案

您对授权 URL 和 token URL 使用相同的 URL。这是我的第一个线索,然后我看到了你的评论。

即使您正在更改授权类型,当您应该使用“ClientCredentialsResourceDetails”时,您仍在使用“AuthorizationCodeResourceDetails”。这种类型的 ResourceDetails 旨在用于您正在解释的情况。

ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
resource.setAccessTokenUri(TOKEN_URL);
resource.setClientId(CLIENT_ID);
resource.setClientSecret(CLIENT_SECRET);
resource.setClientAuthenticationScheme(AuthenticationScheme.form); //This line isn't always needed
return resource;

关于spring - 使用 Spring oauth2 使用受 OAuth 保护的 REST Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31452729/

相关文章:

java - 使用 enctype ="multipart/form-data"将选定的图像获取到 Controller 类?

java - Spring Web 开发中的通用 DAO

oauth-2.0 - OAuth2 访问 token 中允许使用哪些字符?

mysql - JPQL - 由 10 多个不同客户租用的精选汽车

java - JPA脚本生成重复语句

spring - 如何记录 Spring 事务内容

java - hibernate 保存到数据库 "Duplicate entry ' 1' for key ' UK_2n5xttsxwbyc6x67l0x8phfwn'"

java - 无法 Autowiring org.springframework.mail.javamail.JavaMailSender

asp.net-core - ASP .Net Core 谷歌身份验证

oauth-2.0 - 发送带有对象标签的 http 请求 header