A boilerplate starter project for building a Java Spring Boot application with JWT authentication and MongoDB. This template includes essential features for implementing secure and scalable authentication systems in your Spring Boot applications.
- JWT authentication for secure token-based access
- RSA keys for signing and verifying JWTs
- Role-based access control
- Configured SecurityFilterChain for managing security
- Password hashing for secure storage
- User Sign Up and Sign In endpoints
- Profile endpoint for accessing user details
- Docker Compose for running MongoDB and MongoDB Express
- MongoDB Integration Including Auditing
- Jakarta Bean Validation for input validation
- Swagger UI for API documentation
- Java 17+
- Maven
- Clone the repository:
git clone https://github.com/boilerlabs/java-spring-auth-jwt-mongo
cd java-spring-auth-jwt-mongo
- Generating
app.key
andapp.pub
:
openssl genrsa -out src/main/resources/app.key 2048
openssl rsa -in src/main/resources/app.key -pubout -out src/main/resources/app.pub
- Running with Maven
mvn spring-boot:run
To sign up, send a POST request to /api/auth/signup
with the following JSON payload:
{
"username": "example_username",
"password": "example_password"
}
To sign in, send a POST request to /api/auth/signin
with the following JSON payload
{
"username": "example_username",
"password": "example_password"
}
The response will contain the JWT token
{
"token": "example_token",
"expiration": "2024-09-19T10:00:00Z"
}
To access a protected route, include the JWT token in the Authorization header
curl -X GET http://localhost:8080/api/profile \
-H "Authorization: Bearer example_token"
The response will return the user details:
{
"id": "user-uuid",
"username": "user",
"roles": ["ROLE_USER"]
}
We welcome contributions! Please see the CONTRIBUTING.md for more details.
This project is licensed under the MIT License - see the LICENSE file for details.