2.Customized Authentication
Now it is time to start customizing the auto-configuration.
As soon as you customize any bit of Spring Security the complete spring boot auto-configuration will back-off.
Custom authentication with persistent users
Before we start let's look into some internal details how spring security works for the servlet web stack.
By using a ServletFilter you can add functionality that is called around each request and response. Spring Security provides several web filter out of the box.
Filter
Description
AuthenticationWebFilter
Performs authentication of a particular request
AuthorizationWebFilter
Determines if an authenticated user has access to a specific object
CorsWebFilter
Handles CORS preflight requests and intercepts
CsrfWebFilter
Applies CSRF protection using a synchronizer token pattern.
Spring Security WebFilter
Security Filter Chain
Spring Security configures security by utilizing a Security Filter Chain
In step 1 we just used the auto configuration of Spring Boot. This configured a default security filter chain.
As part of this lab we will customize several things for authentication:
Connect the existing persistent user data with Spring Security to enable authentication based on these
Encode the password values to secure hashed ones in the database
Ensure a password policy to enforce secure passwords (a common source of hacking authentication are weak passwords)
Encrypting Passwords
We start by replacing the default user/password with our own persistent user storage (already present in DB). To do this we add a new class WebSecurityConfiguration to package com.example.libraryserver.config having the following contents.
WebSecurityConfiguration.java
The WebSecurityConfiguration implementation does two important things:
This adds the SecurityWebFilterChain
Configures a PasswordEncoder. A password encoder is used by spring security to encrypt passwords and to check if a given password matches the encrypted one.
You may recognize as well a legacy password encoder (this will be used later in this lab for password upgrades)
PasswordEncoder interface
Encrypts the given cleartext password
Validates the given cleartext password with the encrypted one (without revealing the unencrypted one)
In spring security 5 creating an instance of the DelegatingPasswordEncoder is much easier by using the class PasswordEncoderFactories. In past years several previously used password encryption algorithms have been broken (like MD4 or MD5). By using PasswordEncoderFactories you always get a configured PasswordEncoder that uses an PasswordEncoder with a state of the art encryption algorithm like BCrypt or Argon2 at the time of creating this workshop.
DelegatingPasswordEncoder
To have encrypted passwords in our database we need to tweak our existing DataInitializer a bit with the PasswordEncoder we just have configured.
DataInitializer.java
Persistent User Storage
Now that we already have configured the encrypting part for passwords of our user storage we need to connect our own user store (the users already stored in the DB) with spring security's authentication manager.
This is done in two steps:
In the first step we need to implement spring security's definition of a user implementing UserDetails. Please create a new class called AuthenticatedUser in package com.example.libraryserver.security.
To make it a bit easier we just extend our existing User data class.
AuthenticatedUser.java
In the second step we need to implement spring security's interface UserDetailsService to integrate our user store with the authentication manager. Please go ahead and create a new class LibraryUserDetailsService in package com.example.libraryserver.security:
LibraryUserDetailsService.java
After completing this part of the workshop we now still have the auto-configured SecurityWebFilterChain but we have replaced the default user with our own users from our DB persistent storage.
If you restart the application now you have to use the following user credentials to log in:
Username
Password
Role
bwayne
bruce.wayne@example.com
wayne
LIBRARY_USER
bbanner
bruce.banner@example.com
banner
LIBRARY_USER
pparker
peter.parker@example.com
parker
LIBRARY_CURATOR
ckent
clark.kent@example.com
kent
LIBRARY_ADMIN
Authenticated Principal
As we now have a persistent authenticated user we can now also use this user to check if the current user is allowed to borrow or return a book. This requires changes in BookService and BookRestController.
First change the class BookService:
BookService.java
Then please adapt the BookRestController accordingly.
BookRestController.java
You will get compilation errors in class BookModelAssembler. Here you just have to adapt the method calls for returnBook and borrowBook.
BookModelAssembler.java
Automatic Password Encryption Updates
We already looked into the DelegatingPasswordEncoder and PasswordEncoderFactories. As these classes have knowledge about all encryption algorithms that are supported in spring security, the framework can detect an outdated encryption algorithm. If you look at the LibraryUserDetailsService class we just have added this also implements the additionally provided interface UserDetailsPasswordService. This way we can now enable an automatic password encryption upgrade mechanism.
The UserDetailsPasswordService interface just defines one more operation.
UserDetailsPasswordService interface
We already have a user using a password that is encrypted using an outdated MD5 algorithm. We achieved this by defining a legacy user doctor.strange@example.com with password strange in the existing DataInitializer class.
Now restart the application and see what happens if we try to get the list of books using this new user (username='doctor.strange@example.com', password='strange').
In the console you should see the log output showing the old MD5 password being updated to bcrypt password.
CAUTION: Never log any sensitive data like passwords, tokens etc., even in encrypted format. Also never put such sensitive data into your version control. And never let error details reach the client (via REST API or web application). Make sure you disable stacktraces in client error messages using property server.error.include-stacktrace=never
Actuator Security
It is also a good idea to restrict the details of the health actuator endpoint to authenticated users. Anonymous users should only see the UP or DOWN status values but no further details.
Just change this entry in application.yml file:
Adding a Password Policy
Usually not the authentication mechanisms are hacked, instead weak passwords are the most critical source for attacking authentication. Therefore strong passwords are really important. It is also not a good practice any more to force users to change their passwords after a period of time.
Now we want to follow the recommendations of the NIST for secure passwords: NIST (section 5.1.1.2 Memorized Secret Verifiers) to implement a password policy.
We will utilize the Passay library for this.
So first add a new dependency to build.gradle:
PasswordValidationService.java
We also need to configure this service as Spring bean by creating new class PasswordValidationConfiguration:
We also check for well-known insecure passwords using a password list. There are plenty of password lists available on the internet (especially useful for performing brute force attacks by hackers). Please download one of these the password list from https://github.com/danielmiessler/SecLists/blob/master/Passwords/darkweb2017-top100.txt and store this file as password-list.txt in folder src/main/resources.
We also need a special error for reporting password policy violations. Please create a new class InvalidPasswordError.
Now we have to add the PasswordValidationService to our UserRestController to check the policy when creating or updating a User.
UserRestController.java
To correctly handle password policy violations with correct http status we need to extend the ErrorHandler class:
This is the end of lab 2 of the workshop.
NOTE: You find the completed code in project lab2/library-server-complete.
In the next lab we will add the Mutual TLS authentication.
Last updated
Was this helpful?