Spring Boot 2 OAuth2#



OAuth defines four roles –

  • Resource Owner – The user of the application.

  • Client – the application (user is using) which require access to user data on the resource server.

  • Resource Server – store user’s data and http services which can return user data to authenticated clients.

  • Authorization Server – responsible for authenticating user’s identity and gives an authorization token. This token is accepted by resource server and validate your identity.

Oauth2 Flow

Access Token vs Refresh Token#

An access token is a string representing an authorization issued to the client. Tokens represent specific scopes and duration of access, granted by the resource owner, and enforced by the resource server and authorization server.

Refresh token is issued (along with access token) to the client by the authorization server and is used to obtain a new access token when the current access token becomes invalid or expires, or to obtain additional access tokens with identical or narrower scope (access tokens may have a shorter lifetime and fewer permissions than authorized by the resource owner). Issuing a refresh token is optional at the discretion of the authorization server.

  • The responsibility of access token is to access data before it gets expired.

  • The responsibility of refresh token is to request for a new access token when the existing access token is expired.

Oauth2 – Authorization Server#

  • OAuth2AuthorizationServer

public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {
	private BCryptPasswordEncoder passwordEncoder;

	public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
				.authorizedGrantTypes("password", "authorization_code", "refresh_token")
  • Spring security oauth exposes two endpoints for checking tokens (/oauth/check_token and /oauth/token_key) which are by default protected behind denyAll(). tokenKeyAccess() and checkTokenAccess() methods open these endpoints for use.

  • ClientDetailsServiceConfigurer is used to define an in-memory or JDBC implementation of the client details service. we have used in-memory implementation. It has following important attribute:

    • clientId – (required) the client id.

    • secret – (required for trusted clients) the client secret, if any.

    • scope – The scope to which the client is limited. If scope is undefined or empty (the default) the client is not limited by scope.

    • authorizedGrantTypes – Grant types that are authorized for the client to use. Default value is empty.

    • authorities – Authorities that are granted to the client (regular Spring Security authorities).

    • redirectUris – redirects the user-agent to the client’s redirection endpoint. It must be an absolute URL.

Oauth2 – Resource Server#

public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {
	public void configure(HttpSecurity http) throws Exception {

Above config enable protection on all endpoints starting /api. All other endpoints can be accessed freely.

public class SecurityConfig extends WebSecurityConfigurerAdapter {
	protected void configure(HttpSecurity http) throws Exception {
				.antMatchers("/oauth/authorize**", "/login**", "/error**")

	protected void configure(AuthenticationManagerBuilder auth)
			throws Exception {

	public BCryptPasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();

Above WebSecurityConfigurerAdapter class setup a form based login page and open up the authorization urls with permitAll().

Oauth2 protected REST resources#

  • RestResource

  • UserProfile


We have an API http://localhost:8080/api/users/me which we can access by directly putting username/password in login form, but third party application cannot access the API as we do in browsers. They need oauth2 token.

Get authorization grant code from user#

  • First step is to get authorizarion grant from resource owner from URL: http://localhost:18093/oauth/authorize?client_id=clientapp&response_type=code&scope=read_profile_info

  • T5750, 123456

  • http://localhost:18094/login?code=rfQzEz. Here rfQzEz is authorization code for the third party application.

Get access token from authorization server#

  • http://localhost:18093/oauth/token?grant_type=authorization_code&code=rfQzEz

  • clientapp, 123456

#Access token request from postman


Content-Type: application/x-www-form-urlencoded
authorization: Basic Y2xpZW50YXBwOjEyMzQ1Ng==

Form data - application/x-www-form-urlencoded:

Or make similar request from cURL.

curl -X POST --user clientapp:123456 http://localhost:18093/oauth/token -H "content-type: application/x-www-form-urlencoded" -d "code=rfQzEz&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A18094%2Flogin&scope=read_user_info"

Access user data from resource server#

  • http://localhost:18093/api/users/me?authorization=Bearer%20389b48fc-914b-4ded-a0cb-a80e32b5c512

curl -X GET http://localhost:18093/api/users/me -H "authorization: Bearer 389b48fc-914b-4ded-a0cb-a80e32b5c512"