Once you clone this project, from the root folder (where you can see the gradlew binary), do the following:
If you don't have docker-compose (which should be included with docker), go download and install it now for your OS .
First copy the docker-compose.yml into any directory outside the project. I had issues with running it from inside the project folder while the IDE was running.
Next via terminal cd
into the folder where docker-compose.yml is and run the command. If you're running Windows, just exclude the ./
from the commands:
# from the folder where you put your docker-compose.yml create your database container
./docker-compose up
# in a NEW terminal window - list your containers
./docker ps
# find the container name or id and then do the command, to get into bash:
./docker exec -it <container name or id> /bin/bash
# now we have to expose the user to be usable by your application
./mysql -uroot -p
# enter password as "password" and hit enter
Enter password:password
# run sql statements on your user
ALTER USER 'user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
# from the root folder of the project build and start your application
./gradlew clean build
./gradlew bootRun
Just to see if it's working try to run a curl request, or postman, whatever you normally use, copy and paste this request in and you should get a response that the user was created:
curl --location --request POST 'http://localhost:8080/api/auth/signup/' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "user@example.com",
"username": "user_admin",
"role": ["admin"],
"password": "password"
}'
Original readme content is below, note, you do not need to run the insert statements for the role table, as I included an init script already which runs with ApplicationRunner.
Also, the docker-compose mysql setup already matches the configuration in the application.properties, so you won't need to change that.
The diagram shows flow of how we implement User Registration, User Login and Authorization process.
You can have an overview of our Spring Boot Server with the diagram below:
For more detail, please visit:
Secure Spring Boot App with Spring Security & JWT Authentication
For instruction: Spring Boot Refresh Token with JWT example
Run both Back-end & Front-end in one place:
More Practice:
Exception handling: @RestControllerAdvice example in Spring Boot
Deployment:
– If you want to use PostgreSQL:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
– or MySQL:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Open src/main/resources/application.properties
- For PostgreSQL:
spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
spring.datasource.username= postgres
spring.datasource.password= 123
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
# App Properties
bezkoder.app.jwtSecret= bezKoderSecretKey
bezkoder.app.jwtExpirationMs= 86400000
- For MySQL
spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username= root
spring.datasource.password= 123456
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update
# App Properties
bezkoder.app.jwtSecret= bezKoderSecretKey
bezkoder.app.jwtExpirationMs= 86400000
mvn spring-boot:run
INSERT INTO roles(name) VALUES('ROLE_USER');
INSERT INTO roles(name) VALUES('ROLE_MODERATOR');
INSERT INTO roles(name) VALUES('ROLE_ADMIN');