I have a situation where I would like to create an access token myself (so not through the usual process). I have come up with something like this:
@Inject
private DefaultTokenServices defaultTokenServices;
...
OAuth2Authentication auth = xxx;
OAuth2AccessToken token = defaultTokenServices.createAccessToken(auth);
The only problem is that I am not sure how to create the OAuth2Authentication (in my code the part with xxx). I have the user & client info and I know which Authorities I want to grant this token.
I based my solution on Mop So's answer but instead of using:
I used:
Why? Because if you use
tokenEndpoint.getAccessToken(principal, parameters)
the endpoing will throw you aHttpRequestMethodNotSupportedException
because it has not been called with aGET
method. At least, this is what happened to me withspring-security-oauth2-2.0.13.RELEASE
Problem
I had problems with all the implementations listed here, so I finally managed to get my own with a stateless server, oauth2 and google social. Its just the last part of the tutorial that is missing here
The problem for me is that after executing the google oauth, I need to exchange a 10 second duration token for a long lived token. In order to do that I need to generate a JWT token and exchange it with a real access token generated by myself.
Implementation
JWSTokenService
: its a self implemented class that encodes and decodes the exchanging token between google oauth and mine.ClientDetailsService
: bean declared as as part of the authorization server. Comes from my databaseoverride fun configure(clients: ClientDetailsServiceConfigurer) { clients.jdbc(datasource) }
UserService
: just a user service that extendsUserDetailsService
to obtain my users from the databaseDefaultTokenServices
: implemented as a primary bean as followsOAuth2RequestFactory
: implemented as a bean as followsWith all this dependencies, what I need to do to generate a token that gets stored into the database and follows the same flows as the other ones without providing a password is:
Authentication
using theUsernamePasswordAuthenticationToken
class. This is the key part, callDefaultTokenServices#createAccessToken
to obtain a new token. It needs some arguments to execute the request:OAuth2Request
: it can be created with theOAuth2RequestFactory
Authentication
created previouslyTokenRequest
with the client that is triggering this token request. In my case I have that hardcodedSummary
So to recap how to create a token manually:
Here it is, your use case may differ slightly based on the flow you are using. This is what works for a password grant flow. There are a few custom class like token store, token enhancer ect. but that is really just extended versions of the spring classes modified for our own needs.
Here is how to generate a Token using the TokenEndpoint interface (used to expose REST service) :
This has worked for me:
In the Oauth2Configuration:
The rest of the Oauth2Configuration should look like in the article:
http://stytex.de/blog/2016/02/01/spring-cloud-security-with-oauth2/
Other way, to manually generate an
OAuth2 Accesss Token
we can use an instance ofTokenService