Secure Spring REST With Spring Security and OAuth2 - DZone Security
Secure Spring REST With Spring Security and OAuth2 - DZone Security
In this post, we are going to demonstrate Spring Security + OAuth2 for securing REST API
endpoints on an example Spring Boot project. Clients and user credentials will be stored
in a relational database (example con igurations prepared for H2 and PostgreSQL
database engines). To do it we will have to:
Con igure Spring Security + database.
Create an Authorization Server.
Create a Resource Server.
Get an access token and a refresh token.
Get a secured Resource using an access token.
To simplify the demonstration, we are going to combine the Authorization Server and
Resource Server in the same project. As a grant type, we will use a password (we will use
BCrypt to hash our passwords).
Before you start you should familiarize yourself with OAuth2 fundamentals.
Introduction
The OAuth 2.0 speci ication de ines a delegation protocol that is useful for conveying
authorization decisions across a network of web-enabled applications and APIs. OAuth is
used in a wide variety of applications, including providing mechanisms for user
authentication.
OAuth Roles
OAuth speci ies four roles:
Resource owner (the User) – an entity capable of granting access to a protected
resource (for example end-user).
Resource server (the API server) – the server hosting the protected resources,
capable of accepting responding to protected resource requests using access tokens.
Client – an application making protected resource requests on behalf of the
resource owner and with its authorization.
Authorization server – the server issuing access tokens to the client after
successfully authenticating the resource owner and obtaining authorization.
Grant Types
OAuth 2 provides several "grant types" for different use cases. The grant types de ined
are:
Authorization Code
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 1/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
Password
Client credentials
Implicit
Application
Let's consider the database layer and application layer for our example application.
Business Data
Our main business object is Company :
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 2/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
Based on CRUD operations for Company and Department objects ,we want to de ine
following access rules:
COMPANY_CREATE
COMPANY_READ
COMPANY_UPDATE
COMPANY_DELETE
DEPARTMENT_CREATE
DEPARTMENT_READ
DEPARTMENT_UPDATE
DEPARTMENT_DELETE
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 3/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 4/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
Because we want to come with some pre-loaded data, below is the script that will load all
authorities:
INSERT INTO AUTHORITY(ID, NAME) VALUES (1, 'COMPANY_CREATE');
INSERT INTO AUTHORITY(ID, NAME) VALUES (2, 'COMPANY_READ');
INSERT INTO AUTHORITY(ID, NAME) VALUES (3, 'COMPANY_UPDATE');
INSERT INTO AUTHORITY(ID, NAME) VALUES (4, 'COMPANY_DELETE');
Application Layer
The test application is developed in Spring boot + Hibernate + Flyway with an exposed
REST API. To demonstrate data company operations, the following endpoints were
created:
@RestController
@RequestMapping("/secured/company")
public class CompanyController {
@Autowired
private CompanyService companyService;
PasswordEncoders
Since we are going to use different encryptions for OAuth2 client and user, we will de ine
separate password encoders for encryption:
OAuth2 client password – BCrypt (4 rounds)
User password - BCrypt (8 rounds)
@Configuration
public class Encoders {
@Bean
public PasswordEncoder oauthClientPasswordEncoder() {
return new BCryptPasswordEncoder(4);
}
@Bean
public PasswordEncoder userPasswordEncoder() {
return new BCryptPasswordEncoder(8);
@Autowired
private UserRepository userRepository;
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 7/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
if (user != null) {
return user;
}
To separate the service and repository layers we will create UserRepository with JPA
Repository:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private PasswordEncoder userPasswordEncoder;
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(userPasswordEncoder);
}
}
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 8/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
OAuth2 Configuration
First of all, we have to implement the following components:
Authorization Server
Resource Server
Authorization Server
The authorization server is responsible for the veri ication of user identity and providing
the tokens.
Spring Security handles the Authentication and Spring Security OAuth2 handles the
Authorization. To con igure and enable the OAuth 2.0 Authorization Server we have to
use @EnableAuthorizationServer annotation.
@Configuration
@EnableAuthorizationServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Import(ServerSecurityConfig.class)
public class AuthServerOAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 9/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
@Bean
public OAuth2AccessDeniedHandler oauthAccessDeniedHandler() {
return new OAuth2AccessDeniedHandler();
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").passwordE
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.jdbc(dataSource);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager).userDetai
}
}
Resource Server
A Resource Server serves resources that are protected by the OAuth2 token.
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 10/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(RESOURCE_ID);
}
@Override
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 11/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
public void configure(HttpSecurity http) throws Exception {
http.requestMatchers()
.antMatchers(SECURED_PATTERN).and().authorizeRequests()
.antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
.anyRequest().access(SECURED_READ_SCOPE);
}
}
The configure(HttpSecurity http) method con igures the access rules and request matchers
(path) for protected resources using the HttpSecurity class. We secure the URL
paths /secured/* . It’s worth noting that to invoke any POST method request, the ‘write’
scope is needed.
Let's check if our authentication endpoint is working – invoke:
curl -X POST \
http://localhost:8080/oauth/token \
-H 'authorization: Basic c3ByaW5nLXNlY3VyaXR5LW9hdXRoMi1yZWFkLXdyaXRlLWNsaWVudDpzcHJpbmctc2VjdX
-F grant_type=password \
-F username=admin \
-F password=admin1234 \
-F client_id=spring-security-oauth2-read-write-client
and
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 12/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
@Autowired
private CompanyRepository companyRepository;
@Override
@Transactional(readOnly = true)
@PreAuthorize("hasAuthority('COMPANY_READ') and hasAuthority('DEPARTMENT_READ')")
public Company get(Long id) {
return companyRepository.find(id);
}
@Override
@Transactional(readOnly = true)
@PreAuthorize("hasAuthority('COMPANY_READ') and hasAuthority('DEPARTMENT_READ')")
public Company get(String name) {
return companyRepository.find(name);
}
@Override
@Transactional(readOnly = true)
@PreAuthorize("hasRole('COMPANY_READER')")
public List<Company> getAll() {
return companyRepository.findAll();
}
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 13/15
11/5/2018 Secure Spring REST With Spring Security and OAuth2 - DZone Security
@Override
@Transactional
@PreAuthorize("hasAuthority('COMPANY_CREATE')")
public void create(Company company) {
companyRepository.create(company);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('COMPANY_UPDATE')")
public Company update(Company company) {
return companyRepository.update(company);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('COMPANY_DELETE')")
public void delete(Long id) {
companyRepository.delete(id);
}
@Override
@Transactional
@PreAuthorize("hasAuthority('COMPANY_DELETE')")
public void delete(Company company) {
companyRepository.delete(company);
}
}
Summary
In this blog post, we showed OAuth2 authentication with Spring. Access rights were
de ined straightforward – by establishing a direct connection between User and
Authorities. To enhance this example we can add an additional entity – Role – to improve
the structure of the access rights.
The source code for the above listings can be found in this GitHub project.
https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2 15/15