Languages
[Edit]
EN

Spring Boot 2 - simple custom authentication example using JPA and MySQL database

3 points
Created by:
Creg
9600

In this article, we would like to show how to create a simple web application with custom authentication using Spring Boot 2, JPA, and MySQL database.

Notes:

In the below example:

  1. to generate construstors, getters and setters automatically Lombok pre-procesor was used
    (check request, response and entity classes),
  2. to generate query methods automatically and query database Spring Data JPA was used
    (check UsersRepository interface).

Final effect:

Simple authentication example using Spring Boot 2, JPA, and MySQL database.
Simple authentication example using Spring Boot 2, JPA, and MySQL database.

Project structure:

 

DemoApplication.java file:

 

AuthenticationController.java file:

 

AuthenticationService.java file:

 

repositories/UsersRepository.java file:

entities/UserEntity.java file:

 

requests/UserLoginRequest.java file:

requests/UserRegisterRequest.java file:

 

responses/UserActionResponse.java file:

responses/UserCheckResponse.java file:

 

resources/static/index.html file:

 

resources/application.properties file:

Hint: to run database in the computer memory go to this snippet to see how to use H2 database.

 

pom.xml file:

Hint: to run database in the computer memory go to this snippet to see how to use H2 database (in that case mysql-connector-java dependency can be removed from pom.xml file).

 

Database preparation

Edit

1. Database should be created using:

2. users table will be created or updated automatically by Spring Boot 2 Application
(configured by spring.jpa.hibernate.ddl-auto=update property).

 

See also

Edit
  1. Spring Boot 2 - using Lombok plugin with Maven project (in pom.xml file)

  2. Spring Boot 2 - CRUD example using JPA and MySQL database

Alternative titles

  1. Spring Boot 2 - simple own authentication example using JPA and MySQL database
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join