{{announcement.body}}
{{announcement.title}}

Generating OAuth Tokens Part 1

DZone 's Guide to

Generating OAuth Tokens Part 1

In this article, we'll discuss how to create a basic authorization server that creates tokens given the username and password.

· Security Zone ·
Free Resource

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.

Java


Now, let us create a bean for the password encoder. We can use the BcryptPasswordEncoder for encoding the passwords.

Java


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

Java


Grant Types:

  • Authorization code grant.
  • Implicit grant.
  • Resource owner credentials grant.
  • Client credentials grant.
  • Refresh token grant.

Scope

Scopes limits the application's access to user's accounts. It can have one or more scopes.

Java


Let us now create on more class WebSecurity, which extends WebSecurityConfigurerAdapter and annotate it with @Configuration and @EnableWebSecurity

Java


Let us override the configure method

Java


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. 

Java


Now, a very minimal authorization server is ready.  Please see the classes below:

AuthServer.java

Java


WebSecurity.java

Java


Please find the source code at https://github.com/gudpick/oauth-demo/tree/oauth-starter

Please find video tutorials at:


Topics:
java ,oauth 2 ,oauth 2 access token ,security ,spring secruity ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}