emrtnm / spring-security-jwt

Spring Security JWT

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spring Security JWT

Spring Boot Spring Security JWT License: GPL-3.0

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.

Features

  • 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

How To Run

  1. Clone this repository:
git clone git@github.com:emrtnm/spring-security-jwt.git
  1. Open project settings and select BellSoft Liberica 17 SDK (Recommended).

  2. 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
  1. Change your secret key in application.properties file (Optional):
security.jwt.token.secret-key=d32d53896a4e21855aeb0efaa23c4b5ab0721a6e23702d5eb549332d9e913d14
  1. Open terminal in your project folder

  2. Run this command

mvn spring-boot:run
  1. Go to http://localhost:8080 from your browser.

  2. You should see this message:

{
  "message": "Unauthorized path"
}

How To Test

  1. Open Postman: Open the Postman application.
  2. Import File: Click on the "File" tab in the top menu.
  3. Click on Import: Select the "Import" option.
  4. 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.
  5. Collection Created: After importing the file, a collection named "Spring Security JWT API" will be created.
  6. Enjoy: You can now use the newly created collection in Postman to manage your requests and test your API.

File Structure

├── 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

License

This project is open-sourced software licensed under the GPL-3.0 license.

Make A Contribution!

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

About

Spring Security JWT

License:GNU General Public License v3.0


Languages

Language:Java 100.0%