Client Interaction

To consume the service in a microservices architecture with Spring Security and JWT, clients need to follow a series of steps to authenticate and interact securely with the services. Here's a high-level overview of how a client can consume the service:

Step-by-Step Client Interaction

1. User Login and Token Generation

  • Endpoint: /login

  • Method: POST

  • Description: The client sends a request with the user's credentials (e.g., username and password) to the authentication server's login endpoint. If the credentials are valid, the server generates a JWT token and returns it to the client.

Example Request:

POST /login HTTP/1.1
Host: authentication-server.com
Content-Type: application/json

{
    "username": "user",
    "password": "password"
}

Example Response:

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

2. Store the Token

  • Action: The client securely stores the JWT token, typically in an HTTP-only cookie or local storage.

3. Access Protected Resources

  • Endpoint: /api/protected-resource

  • Method: GET

  • Description: When the client needs to access protected resources, it includes the JWT token in the Authorization header of the request.

Example Request:

GET /api/protected-resource HTTP/1.1
Host: service-provider.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Example Response:

{
    "data": "This is a protected resource."
}

4. Token Validation and User Authorization

  • Action: The service provider validates the JWT token to ensure it is not expired and was issued by a trusted source. If the token is valid, the service processes the request and returns the appropriate response.

5. Token Refresh (Optional)

  • Endpoint: /refresh-token

  • Method: POST

  • Description: If the token is nearing expiration, the client can request a new token by sending the current token to a token refresh endpoint.

Example Request:

bash

POST /refresh-token HTTP/1.1
Host: authentication-server.com
Content-Type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Example Response:

json

{
    "token": "new-jwt-token..."
}

Summary

  1. User Logs In: Client sends credentials to the authentication server.

  2. Token Generated: Authentication server returns a JWT token.

  3. Token Stored: Client securely stores the JWT token.

  4. Access Protected Resources: Client includes JWT in the Authorization header when accessing protected resources.

  5. Token Validation: Service provider validates the JWT.

  6. Token Refresh (Optional): Client can refresh the JWT token when needed.

Last updated