A Spring Boot-based authentication service that provides JWT-based authentication with refresh token support. This service handles user signup, login, and token refresh operations.
- π User Registration (Signup)
- π User Login with JWT Authentication
- π Refresh Token Mechanism
- π‘οΈ Spring Security Integration
- πΎ MySQL Database Integration
- π JWT Token Generation and Validation
- Java 21
- Spring Boot 3.3.0
- Spring Security
- Spring Data JPA
- MySQL 8.0
- JWT (JSON Web Tokens)
- Lombok
- Gradle
- Java 21 or higher
- MySQL 8.0 or higher
- Gradle 9.2.1 or higher
Update the database configuration in app/src/main/resources/application.properties:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:127.0.0.1}:${MYSQL_PORT:3306}/${MYSQL_DB:authservice}
spring.datasource.username=root
spring.datasource.password=root123MYSQL_HOST- MySQL host (default: 127.0.0.1)MYSQL_PORT- MySQL port (default: 3306)MYSQL_DB- Database name (default: authservice)
The application runs on port 9898 by default. You can change it in application.properties:
server.port=9898./gradlew build./gradlew bootRunOr:
java -jar app/build/libs/app.jarBase URL: http://localhost:9898
Register a new user account.
Endpoint: POST /auth/v1/signup
Request Body:
{
"user_id": "user123",
"username": "john_doe",
"password": "securePassword123",
"first_name": "John",
"last_name": "Doe",
"phone_number": 1234567890,
"email": "john.doe@example.com"
}Success Response (200 OK):
{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token": "abc123-def456-ghi789"
}Error Responses:
400 Bad Request- User already exists:"Already Exist"500 Internal Server Error- Server error:"Exception in User Service"
Authenticate an existing user and receive JWT tokens.
Endpoint: POST /auth/v1/login
Request Body:
{
"username": "john_doe",
"password": "securePassword123"
}Success Response (200 OK):
{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token": "abc123-def456-ghi789"
}Error Responses:
401 Unauthorized- Invalid credentials500 Internal Server Error- Server error:"Exception in User Service"
Get a new access token using a valid refresh token.
Endpoint: POST /auth/v1/refreshToken
Request Body:
{
"token": "abc123-def456-ghi789"
}Success Response (200 OK):
{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token": "abc123-def456-ghi789"
}Error Responses:
401 Unauthorized- Token not found:"Refresh Token is not in DB"401 Unauthorized- Token expired:"<token> Refresh token is expired. Please make a new login..!"500 Internal Server Error- Server error:"Exception in Refresh Token Service"
Postman:
- Method:
POST - URL:
http://localhost:9898/auth/v1/signup - Headers:
Content-Type: application/json
- Body (raw JSON):
{ "user_id": "user123", "username": "john_doe", "password": "securePassword123", "first_name": "John", "last_name": "Doe", "phone_number": 1234567890, "email": "john.doe@example.com" }
cURL:
curl -X POST http://localhost:9898/auth/v1/signup \
-H "Content-Type: application/json" \
-d '{
"user_id": "user123",
"username": "john_doe",
"password": "securePassword123",
"first_name": "John",
"last_name": "Doe",
"phone_number": 1234567890,
"email": "john.doe@example.com"
}'Postman:
- Method:
POST - URL:
http://localhost:9898/auth/v1/login - Headers:
Content-Type: application/json
- Body (raw JSON):
{ "username": "john_doe", "password": "securePassword123" }
cURL:
curl -X POST http://localhost:9898/auth/v1/login \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"password": "securePassword123"
}'Postman:
- Method:
POST - URL:
http://localhost:9898/auth/v1/refreshToken - Headers:
Content-Type: application/json
- Body (raw JSON):
{ "token": "abc123-def456-ghi789" }
cURL:
curl -X POST http://localhost:9898/auth/v1/refreshToken \
-H "Content-Type: application/json" \
-d '{
"token": "abc123-def456-ghi789"
}'- All endpoints (
/auth/v1/signup,/auth/v1/login,/auth/v1/refreshToken) are publicly accessible (no authentication required) - Other endpoints require JWT authentication via
Authorization: Bearer <token>header - Passwords are encrypted using BCrypt
- JWT tokens are used for authenticated requests
- Refresh tokens expire after 7 days
app/src/main/java/org/example/authservice
βββ auth/
β βββ JwtAuthFilter.java # JWT authentication filter
β βββ SecurityConfig.java # Spring Security configuration
βββ controller/
β βββ AuthController.java # Signup endpoint
β βββ TokenController.java # Login and refresh token endpoints
βββ entities/
β βββ RefreshToken.java # Refresh token entity
β βββ UserInfo.java # User entity
β βββ UserRole.java # User role entity
βββ model/
β βββ UserInfoDto.java # User DTO for signup
βββ repository/
β βββ RefreshTokenRepository.java # Refresh token repository
β βββ UserRepository.java # User repository
βββ request/
β βββ AuthRequestDto.java # Login request DTO
β βββ RefreshTokenRequestDto.java # Refresh token request DTO
βββ response/
β βββ JwtResponseDto.java # JWT response DTO
βββ service/
βββ JwtService.java # JWT token service
βββ RefreshTokenService.java # Refresh token service
βββ UserDetailsServiceImpl.java # User details service
The application uses JPA/Hibernate with ddl-auto=update, which automatically creates/updates the database schema. Main tables:
users- User informationtokens- Refresh tokensusers_roles- User roles mapping
Security debug logging is enabled. To adjust logging levels, modify application.properties:
logging.level.org.springframework.security=DEBUGThis project is part of an example authentication service implementation.