Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/devrezaur/spring-security-jwt-module
Ready to use JWT implementation with Spring Security
https://github.com/devrezaur/spring-security-jwt-module
jwt-auth spring-boot spring-security
Last synced: 2 days ago
JSON representation
Ready to use JWT implementation with Spring Security
- Host: GitHub
- URL: https://github.com/devrezaur/spring-security-jwt-module
- Owner: DevRezaur
- License: mit
- Created: 2020-10-27T14:03:56.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-11-13T13:19:27.000Z (about 1 year ago)
- Last Synced: 2023-11-13T14:40:00.245Z (about 1 year ago)
- Topics: jwt-auth, spring-boot, spring-security
- Language: Java
- Homepage:
- Size: 317 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JWT Auth with Spring Boot
This is a ready to use template code for implementing `JWT based authentication and authorization` using Spring Boot.
## Documentation and Usage
### Authentication API
`POST` URI: `localhost:8080/auth/authenticate`
![authenticate-api](https://github.com/DevRezaur/spring-security-JWT-module/blob/main/Screenshots/authenticate-api.PNG)
### Access User Controller
`GET` URI: `localhost:8080/user/`
![user-controller](https://github.com/DevRezaur/spring-security-JWT-module/blob/main/Screenshots/user-controller.PNG)
### Access Admin Controller
`GET` URI: `localhost:8080/admin/`
![admin-controller](https://github.com/DevRezaur/spring-security-JWT-module/blob/main/Screenshots/admin-controller.PNG)
### Token Expired / Access Denied Example
`GET` URI: `localhost:8080/admin/`
![access-denied](https://github.com/DevRezaur/spring-security-JWT-module/blob/main/Screenshots/access-denied.PNG)
### User Registration API
`POST` URI: `localhost:8080/auth/registerUser`
![user-registration](https://github.com/DevRezaur/spring-security-JWT-module/blob/main/Screenshots/user-registration.PNG)
### Admin Registration API
Since admin registration is a `admin privileged` feature, we protected it via `/admin/` mapping. So sending authentication header is required with this request.
`POST` URI: `localhost:8080/admin/registerAdmin`
![admin-registration](https://github.com/DevRezaur/spring-security-JWT-module/blob/main/Screenshots/admin-registration.PNG)
## Migrate to MySQL
By default this module uses H2 database. To use MySQL edit the following configuration.
Open `pom.xml` and configure it as below:
```xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.0
dev.rezaur
Spring-Security-JWT
0.0.1-SNAPSHOT
Spring-Security-JWT
Spring security with JWT
15
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
io.jsonwebtoken
jjwt
0.9.1
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
com.fasterxml.jackson.module
jackson-module-kotlin
org.springframework.boot
spring-boot-starter-test
test
org.springframework.security
spring-security-test
test
org.springframework.boot
spring-boot-maven-plugin
```
After that open `src/main/resources/application.properties` and configure like below:
```properties
server.port=8080
spring.jpa.open-in-view=false
spring.h2.console.enabled=true
spring.datasource.url=jdbc:mysql://localhost:3306/database_name
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.defer-datasource-initialization=true# JWT secret
jwt.secret=secret
# JWT token lifespan (5 mins)
jwt.jwtExp=300000
```
## Additional Configuration
`src/main/resources/data.sql` file is used to insert initial data to the database. This file is not mandatory. Feel free to `modify/delete` this `data.sql` file.
```sql
INSERT INTO roles
VALUES(1, 'ROLE_ADMIN');INSERT INTO roles
VALUES(2, 'ROLE_USER');INSERT INTO users (user_id, fullname, username, password)
VALUES('101', 'Rezaur Rahman', 'DevRezaur', 'iamadmin');INSERT INTO users (user_id, fullname, username, password)
VALUES('102', 'Sanzida Sultana', 'SanzidaSultana', 'iamuser');INSERT INTO user_role
VALUES('101', 1);INSERT INTO user_role
VALUES('101', 2);INSERT INTO user_role
VALUES('102', 2);
```
## Run the Project
* `Clone the repository`
* `Update maven build (Project > Maven > Update Project)`
* `Run (Project > Run As > Spring Boot App)`
## Relevant Projects
> [JWT Auth with Refresh Token using Spring Boot](https://github.com/DevRezaur/JWT-refresh-token-spring-boot)
Feel free to leave a star if you find this helpful :smile: