An open API service indexing awesome lists of open source software.

https://github.com/gbzarelli/spring-security-oauth2-resttemplate

Sample RestTemplate Client with Spring Security Oauth2 with client_credentials flow
https://github.com/gbzarelli/spring-security-oauth2-resttemplate

client-credentials java oauth2 spring spring-boot spring-boot-security-oauth2

Last synced: about 1 year ago
JSON representation

Sample RestTemplate Client with Spring Security Oauth2 with client_credentials flow

Awesome Lists containing this project

README

          

# Sample RestTemplate Client with Spring Security Oauth2 with client_credentials flow

This sample works with `Spring Security Oauth2 5` integrated in Spring Boot RestTemplate to make
client requests with Oauth2 client credentials flow. The goal is manage request tokens and expirations time.

## Caution!!

This lib inject the Spring Security context configurations, but, you can remove-it just
add the exclusions in `spring-boot-starter-oauth2-client` like that:

```xml

org.springframework.boot
spring-boot-starter-oauth2-client


org.springframework.boot
spring-boot-starter


org.springframework.security
spring-security-config

```

After that, just remove the [SecurityConfiguration.java](/src/main/java/br/com/helpdev/security/SecurityConfiguration.java).

## Oh no! I can't use Spring Security Oaut2 5, just 2.x versions, how to implement the client credentials flow?

If you can't upgrade, you can use the **DEPRECATED** way:
(its implementation is easier to build a RestTemplate class)
```
// Build o OAuth2RestTemplate:
final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setAccessTokenUri("https://your.api.url.token.com/....");
resourceDetails.setClientId("clientID");
resourceDetails.setClientSecret("clientSecret");
resourceDetails.setGrantType("client_credentials");

final RestTemplate oauthClient = new OAuth2RestTemplate(resourceDetails);
```

## My project uses Spring Boot version previous **2.4.x** version

Change the `spring-boot-starter-oauth2-client` dependency to:

```xml

org.springframework.security
spring-security-oauth2-client
5.7.7

org.springframework.security
spring-security-oauth2-core
5.7.7

```

# References

- https://docs.spring.io/spring-security/site/docs/5.2.0.RELEASE/reference/html/oauth2.html
- https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Migration-Guide
- https://github.com/oktadev/okta-spring-boot-client-credentials-example/tree/main/client-webclient