I am trying to implement service to service security into spring boot services using spring oauth2. I want a service to access a secured resource of another service without any user action involved.
There are a lot of examples for authorization code grant type, but not very much about the client credentials grant type, which seems to be the right one for this use case.
I can set up the auth server and use a curl request to get a token. The tests I found used Http Objects to check status codes.
How can I use the client credentials grant type in a java client with RestTemplate and spring oauth2?
I would think it must be as simple as adding a dependency, an annotation and a config file, yet I can't make it run.
It's quite simple:
Config
class which is annotated with @Configuration
.OAuth2ProtectedResourceDetails
and create a ClientCredentialsResourceDetails
instance in that method. Add your values to it and return it.OAuth2RestTemplate
in the Configuration
class and create in that method a DefaultOAuth2ClientContext
instance by calling the default constructor. Then create an OAuth2RestTemplate
and add the OAuth2ProtectedResourceDetails
instance and the DefaultOAuth2ClientContext
instance to it. Subsequently return the OAuth2RestTemplate
instance.@Autowired
in both your Controller
and Service
instances to use it.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With