This repository contains an example of authentication and access control using Spring Security and JWT (JSON Web Token). It provides customized authentication using a MySQL database instead of Spring Security's default user management.
- Secure API access using Spring Security
- Authentication and access control with JWT
- User management using MySQL database
- Example of user login and token-based authorization
- Clone this repository:
git clone git@github.com:emrtnm/spring-security-jwt.git
-
Open project settings and select BellSoft Liberica 17 SDK (Recommended).
-
Make your MySQL database connection settings by opening the
application.properties
file:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
- Change your secret key in
application.properties
file (Optional):
security.jwt.token.secret-key=d32d53896a4e21855aeb0efaa23c4b5ab0721a6e23702d5eb549332d9e913d14
-
Open terminal in your project folder
-
Run this command
mvn spring-boot:run
-
Go to http://localhost:8080 from your browser.
-
You should see this message:
{
"message": "Unauthorized path"
}
- Open Postman: Open the Postman application.
- Import File: Click on the "File" tab in the top menu.
- Click on Import: Select the "Import" option.
- Choose File to Import: Click on "Upload Files" and select the
collection.json
file located in the main directory. Then click "Open" or "Choose" to import the file. - Collection Created: After importing the file, a collection named "Spring Security JWT API" will be created.
- Enjoy: You can now use the newly created collection in Postman to manage your requests and test your API.
├── README.md
├── collection.json
├── pom.xml
── src
├── main
│ ├── java
│ │ └── com
│ │ └── emrtnm
│ │ └── jwt
│ │ ├── Application.java
│ │ ├── config
│ │ │ ├── JwtAuthFilter.java
│ │ │ ├── PasswordConfig.java
│ │ │ ├── RestExceptionHandler.java
│ │ │ ├── SecurityConfig.java
│ │ │ └── UserAuthenticationEntryPoint.java
│ │ ├── controller
│ │ │ ├── AuthController.java
│ │ │ └── HomeController.java
│ │ ├── dto
│ │ │ ├── CredentialsDto.java
│ │ │ ├── ErrorDto.java
│ │ │ ├── SignUpDto.java
│ │ │ ├── UserDto.java
│ │ │ └── UserResponse.java
│ │ ├── entity
│ │ │ └── User.java
│ │ ├── enums
│ │ │ └── Role.java
│ │ ├── exception
│ │ │ └── AppException.java
│ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ ├── repository
│ │ │ └── UserRepository.java
│ │ └── service
│ │ └── UserService.java
│ └── resources
│ └── application.properties
└── test
└── java
└── com
└── emrtnm
└── jwt
└── ApplicationTests.java
This project is open-sourced software licensed under the GPL-3.0 license.
If you think that anything here could be improved, please don't hesitate to contribute. Anyone who wants to, regardless of their ability, skill, or background, can contribute to this project. All contributions are made for the benefit of the community. Thanks very much for helping this repository become much more suitable for its purpose and more useful to a larger number of people.
#java #spring #jwt #security #springsecurity #springboot #springframework #auth #jsonwebtoken #springsecurityjwt