In Authentication with a Database-backed UserDetailsService post, we analyzed one approach to achieve this by implementing the UserDetailService interface ourselves.
In this regard, we will use the instructions of AuthenticationManagerBuilder #jdbcAuthentication to analyze the pros and cons of this more direct approach.
First, we will analyze how we can achieve Authentication using an embedded H2 database. This is easy to accomplish because Spring Boot's autoconfiguration is prepared out-of-the-box for this scenario.
Let's start by following the instructions of Spring Boot With H2 Database post to:
Include the corresponding spring-boot-starter-data-jpa and h2 dependencies
Configure the database connection with application properties
Enable the H2 console
We can see, we're using the autoconfigured DataSource. The withDefaultSchema directive adds a database script that will populate the default schema, allowing users and authorities to be stored. This basic user schema is documented in the Spring Security Appendix.
Finally, we will be creating an entry in the database with a default user programmatically.
In addition, we'll secure this endpoint, whilst permitting access to the H2 console: