Spring Boot 2 OAuth2#
Overview#
Roles#
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.
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 – Resource Server#
@Configuration
@EnableResourceServer
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/api/**").authenticated();
}
}
Above config enable protection on all endpoints starting /api
. All other endpoints can be accessed freely.
@Configuration
@Order(1)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests()
.antMatchers("/oauth/authorize**", "/login**", "/error**")
.permitAll().and().authorizeRequests().anyRequest()
.authenticated().and().formLogin().permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("T5750")
.password(passwordEncoder().encode("123456")).roles("USER");
}
@Bean
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
Demo#
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.
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"