This project is a centralized Identity Provider (IdP). It uses JWT (JSON Web Tokens) to secure multiple applications. It comes with Global CORS enabled, making it ready to serve as a Single Sign-On (SSO) backend for your frontend apps (React, Vue, etc.).
- User Login (
/api/login) → Returns JWT - Protected Endpoints (e.g.,
/api/hello) → Requires JWT - BCrypt Password Encoding
- Stateless Authentication (No Sessions)
- CORS Enabled (Ready for external frontends)
- Database Support: H2 (In-memory) or MySQL/PostgreSQL (Cloud/Aiven)
Before you begin, ensure you have the following installed:
- Java 17 or higher
- Maven (optional, wrapper provided)
- Git
src/main/java/com/example/demo
├── config/
│ └── CorsConfig.java # Global CORS settings (who can access this API)
├── exception/
│ ├── GlobalExceptionHandler.java # Returns nice JSON errors (401, 500)
│ └── InvalidCredentialsException.java
├── AppUser.java # User entity (Database table)
├── AuthController.java # Login endpoints
├── AuthService.java # Business logic for authentication
├── JwtUtil.java # Generates & validates Tokens
└── SecurityConfig.java # Spring Security rules (Authorized vs Public)
git clone https://github.com/ezManish/Auth_API.git
cd Auth_API
mvn clean spring-boot:runThe API will start on http://localhost:8080 (default).
By default, it runs on H2 (In-memory). To connect to a real database (like Aiven), update src/main/resources/application.properties:
For Aiven (MySQL):
spring.datasource.url=jdbc:mysql://YOUR-AIVEN-URL:PORT/defaultdb?ssl-mode=REQUIRED
spring.datasource.username=avnadmin
spring.datasource.password=YOUR_PASSWORD
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update- URL:
POST /api/login - Body:
{ "username": "student1", "password": "pass123" } - Response (200 OK):
eyJhbGciOiJIUzI...(The Token) - Response (401 Unauthorized):
Invalid username or password
- URL:
GET /api/hello - Headers:
Authorization: Bearer <YOUR_JWT_TOKEN> - Response:
Hello, you are authenticated with JWT!
You can use this API as the login backend for multiple other projects (Project A, Project B).
- Login: Call
POST /api/loginto get the token. - Save Token: Store it in
localStorage. - Use Token: Send it in the header for every future request.
Example (Fetch API):
// A. LOGIN
const response = await fetch('http://localhost:8080/api/login', {
method: 'POST',
body: JSON.stringify({ username: 'user', password: 'password' }),
headers: { 'Content-Type': 'application/json' }
});
const token = await response.text();
localStorage.setItem('jwt', token);
// B. ACCESS
await fetch('http://localhost:8080/api/hello', {
headers: { 'Authorization': `Bearer ${token}` }
});If you have other backend services (Project A, Project B) that need to valid users:
| Setting | Auth API (Hub) | Project A / B (You) |
|---|---|---|
| Database Credentials | YES | NO |
| JWT Secret Key | YES (Create Token) | YES (Verify Token) |
Important: Copy the jwt.secret from this project's application.properties to your other projects so they can verify the tokens are valid.
- Cause: Port 8080 is blocked.
- Fix: Open
application.propertiesand addserver.port=8082.
- Cause: Wrong URL or Password in
application.properties. - Fix: Double-check your Aiven credentials. Ensure SSL is enabled (
?ssl-mode=REQUIRED).
- Cause: Frontend is on a different domain.
- Fix: This project allows
*by default. If it fails, checkCorsConfig.javato ensure your specific domain is allowed.