- Application running on
http://localhost:8081 - Postman installed
- Basic understanding of HTTP headers
- Spring Boot 2.7.18 with Spring Security 5.7.x
Method 1: Authorization Tab
- Open new request in Postman
- Go to Authorization tab
- Select Basic Auth from dropdown
- Enter credentials:
- Username:
admin - Password:
admin
- Username:
Method 2: Manual Header
- Go to Headers tab
- Add header:
- Key:
Authorization - Value:
Basic YWRtaW46YWRtaW4=(base64 of admin:admin)
- Key:
GET http://localhost:8081/api/basic/admin
Authorization: Basic Auth
Username: admin
Password: admin
Expected Response: 200 OK
{
"message": "Basic Auth - Admin endpoint",
"user": "admin"
}
GET http://localhost:8081/api/basic/user
Authorization: Basic Auth
Username: user
Password: password
Expected Response: 200 OK
{
"message": "Basic Auth - User endpoint",
"user": "user",
"authorities": ["ROLE_USER"]
}
GET http://localhost:8081/api/basic/user
Authorization: Basic Auth
Username: user
Password: wrongpassword
Expected Response: 401 Unauthorized
GET http://localhost:8081/api/basic/admin
Authorization: Basic Auth
Username: user
Password: password
Expected Response: 403 Forbidden
POST http://localhost:8081/api/auth/login
Content-Type: application/json
Body (raw JSON):
{
"username": "admin",
"password": "admin"
}
Expected Response: 200 OK
{
"token": "eyJhbGciOiJIUzI1NiJ9...",
"role": "ADMIN"
}
Copy the token from response for next requests
- Go to Authorization tab
- Select Bearer Token
- Paste the JWT token
OR manually add header:
- Key:
Authorization - Value:
Bearer eyJhbGciOiJIUzI1NiJ9...
GET http://localhost:8081/api/jwt/user/profile
Authorization: Bearer Token
Token: <your-jwt-token>
Expected Response: 200 OK
{
"message": "JWT - User profile",
"user": "admin",
"authorities": ["ROLE_ADMIN"]
}
GET http://localhost:8081/api/jwt/admin/dashboard
Authorization: Bearer Token
Token: <your-jwt-token>
Expected Response: 200 OK
{
"message": "JWT - Admin dashboard",
"user": "admin"
}
GET http://localhost:8081/api/jwt/user/profile
Authorization: Bearer Token
Token: invalid-token-here
Expected Response: 403 Forbidden
GET http://localhost:8081/api/jwt/user/profile
Authorization: Bearer Token
Token: eyJhbGciOiJIUzI1NiJ9.malformed.signature
Expected Response: 403 Forbidden
GET http://localhost:8081/api/jwt/user/profile
Authorization: Bearer Token
Token: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInJvbGUiOiJBRE1JTiIsImlhdCI6MTYwOTQ1OTIwMCwiZXhwIjoxNjA5NDU5MjYwfQ.invalid_signature_for_expired_token
Expected Response: 403 Forbidden
GET http://localhost:8081/api/jwt/user/profile
Authorization: Bearer Token
Token: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInJvbGUiOiJBRE1JTiIsImlhdCI6MTcwNjc4NDAwMCwiZXhwIjoxNzA2ODcwNDAwfQ.wrong_signature_here_for_testing
Expected Response: 403 Forbidden
- Go to Headers tab
- Add header:
- Key:
X-API-Key - Value:
admin-key-123oruser-key-456
- Key:
GET http://localhost:8081/api/key/data
Headers:
X-API-Key: admin-key-123
Expected Response: 200 OK
{
"message": "API Key - Protected data",
"user": "api-admin",
"authorities": ["ROLE_ADMIN"]
}
GET http://localhost:8081/api/key/data
Headers:
X-API-Key: user-key-456
Expected Response: 200 OK
{
"message": "API Key - Protected data",
"user": "api-user",
"authorities": ["ROLE_USER"]
}
GET http://localhost:8081/api/key/data
Headers:
X-API-Key: wrong-key-123
Expected Response: 401 Unauthorized
Reason: Wrong API key → No authentication set
GET http://localhost:8081/api/key/data
(No X-API-Key header)
Expected Response: 401 Unauthorized
Reason: No API key → No authentication set
GET http://localhost:8081/api/role/user/info
Authorization: Basic Auth
Username: user
Password: password
Expected Response: 200 OK
{
"message": "Role-based - User info",
"user": "user"
}
GET http://localhost:8081/api/role/admin/settings
Authorization: Basic Auth
Username: admin
Password: admin
Expected Response: 200 OK
{
"message": "Role-based - Admin settings",
"user": "admin"
}
GET http://localhost:8081/api/role/admin/settings
Authorization: Basic Auth
Username: user
Password: password
Expected Response: 403 Forbidden
GET http://localhost:8081/api/method/sensitive
Authorization: Basic Auth
Username: admin
Password: admin
Expected Response: 200 OK
{
"message": "Method-level security - Sensitive data",
"user": "admin"
}
GET http://localhost:8081/api/method/sensitive
Authorization: Basic Auth
Username: jane (ADMIN role but wrong username)
Password: jane123
Expected Response: 403 Forbidden
GET http://localhost:8081/api/method/sensitive
Authorization: Basic Auth
Username: user
Password: password
Expected Response: 403 Forbidden
OAuth2 testing in Postman requires browser-based authentication flow. API testing is limited to endpoints that accept session cookies after web login.
- Open browser and go to:
http://localhost:8081/login - Click "GitHub" or "Google" button
- Complete OAuth2 authentication flow
- You'll be redirected to dashboard with session established
- In browser, open Developer Tools (F12)
- Go to Application/Storage → Cookies
- Copy
JSESSIONIDvalue
GET http://localhost:8081/api/oauth2/user
Headers:
Cookie: JSESSIONID=your-session-id-here
Expected Response: 200 OK
{
"message": "OAuth2 - User profile",
"name": "John Doe",
"email": "john@example.com",
"provider": "github",
"attributes": {...}
}
GET http://localhost:8081/api/oauth2/profile
Headers:
Cookie: JSESSIONID=your-session-id-here
Expected Response: 200 OK
{
"message": "OAuth2 - User profile",
"id": 12345,
"name": "John Doe",
"email": "john@example.com",
"avatar": "https://avatars.githubusercontent.com/u/12345",
"provider": "github"
}
GET http://localhost:8081/api/oauth2/user
(No Cookie header)
Expected Response: 401 Unauthorized
{
"error": "Not authenticated"
}
GET http://localhost:8081/api/oauth2/user
Headers:
Cookie: JSESSIONID=invalid-session-id
Expected Response: 401 Unauthorized
For easier OAuth2 testing, use browser directly:
- Login via OAuth2 at:
http://localhost:8081/login - Test endpoints directly in browser:
http://localhost:8081/api/oauth2/userhttp://localhost:8081/api/oauth2/profile
GET http://localhost:8081/api/public/info
(No authorization headers needed)
Expected Response: 200 OK
{
"message": "Public endpoint - No authentication required"
}
POST http://localhost:8081/api/auth/register
Content-Type: application/json
Body (raw JSON):
{
"username": "testuser",
"password": "testpass123",
"email": "test@example.com",
"role": "USER"
}
Expected Response: 200 OK
{
"message": "User registered successfully"
}
POST http://localhost:8081/api/auth/register
Content-Type: application/json
Body (raw JSON):
{
"username": "admin", (already exists)
"password": "newpass",
"email": "new@example.com",
"role": "USER"
}
Expected Response: 400 Bad Request
{
"error": "Username already exists"
}
GET http://localhost:8081/api/rate-limit/public
(No authentication required)
Expected Response: 200 OK (first 10 requests)
{
"message": "Public rate limiting test endpoint",
"timestamp": 1706784000000
}
Expected Response: 429 Too Many Requests (after 10 requests)
{
"error": "Rate limit exceeded. Max 10 requests per minute."
}
GET http://localhost:8081/api/rate-limit/secure
Authorization: Basic Auth
Username: admin
Password: admin
Expected Response: 200 OK (first 10 requests)
{
"message": "Rate limited secure endpoint",
"user": "admin",
"timestamp": 1706784000000
}
Expected Response: 429 Too Many Requests (after 10 requests)
{
"error": "Rate limit exceeded. Max 10 requests per minute."
}
1. Make 10 requests quickly → Get 429 error
2. Wait 1 minute
3. Make request again → Should work (200 OK)
POST http://localhost:8081/api/auth/logout
(No authentication required)
Expected Response: 200 OK
{
"message": "Logged out successfully"
}
- Click New → Collection
- Name: "Spring Boot Security Tests"
- Add folders for each auth method
- Click Environments → New Environment
- Name: "Spring Security API"
- Add variables:
baseUrl:http://localhost:8081jwtToken: (leave empty initially)adminKey:admin-key-123userKey:user-key-456sessionId: (leave empty initially)
- Click Save
- Select environment from dropdown (top right)
Add this script to your Login request:
In Login Request → Tests tab:
// Save JWT token to environment
if (pm.response.code === 200) {
const response = pm.response.json();
pm.environment.set("jwtToken", response.token);
console.log("JWT Token saved:", response.token);
}For OAuth2 endpoints, manually set session ID:
In OAuth2 Request → Pre-request Script:
// Set session cookie for OAuth2 requests
const sessionId = pm.environment.get("sessionId");
if (sessionId) {
pm.request.headers.add({
key: "Cookie",
value: `JSESSIONID=${sessionId}`
});
}| Method | Header/Auth Type | Example Value |
|---|---|---|
| Basic Auth | Authorization | Basic YWRtaW46YWRtaW4= |
| JWT | Authorization | Bearer eyJhbGciOiJIUzI1NiJ9... |
| API Key | X-API-Key | admin-key-123 |
| OAuth2 | Cookie | JSESSIONID=session-id-here |
- Start with Public Endpoints - No auth required
- Test User Registration - Create test users
- Test Basic Authentication - Username/password
- Get JWT Token - Login to get token
- Test JWT Endpoints - Use Bearer token
- Test API Key Endpoints - Use X-API-Key header
- Test OAuth2 - Browser login + session cookie
- Test Rate Limiting - Multiple rapid requests
- Test Error Scenarios - Invalid credentials, expired tokens
This comprehensive guide covers all 7 authentication methods implemented in your Spring Boot Security project.