Generating OAuth Tokens Part 1
Join the DZone community and get the full member experience.Join For Free
We will talk about how to generate OAuth tokens. When using OAuth tokens, passwords are not shared between services. Instead, tokens are used for authentication. Here, we will create a basic authorization server that creates tokens given the username and password.
Let us create a new class that extends
AuthorizationServerConfigurerAdapter. We can annotate it with @Configuration to tell it is a configuration class and has one or more @Bean methods. To enable the authorization server, we will use @EnableAuthorizationServer.
Now, let us create a bean for the password encoder. We can use the
BcryptPasswordEncoder for encoding the passwords.
We will override the configure methods as below. There are three configure methods. We will do it as below. Here, we can configure grant types, passwords, refresh token validity, access token validity, scopes
- Authorization code grant.
- Implicit grant.
- Resource owner credentials grant.
- Client credentials grant.
- Refresh token grant.
Scopes limits the application's access to user's accounts. It can have one or more scopes.
Let us now create on more class WebSecurity, which extends
WebSecurityConfigurerAdapter and annotate it with @Configuration and @EnableWebSecurity
Let us override the configure method
Here, I am having username as "user" and password as "user", and roles I've specified as "USER".
Now, let me create a bean. This is required in the newer versions.
Now, a very minimal authorization server is ready. Please see the classes below:
Please find the source code at https://github.com/gudpick/oauth-demo/tree/oauth-starter
Please find video tutorials at:
Opinions expressed by DZone contributors are their own.