How to Secure Third‑Party APIs with AK/SK, Signatures, and Token Strategies

This guide presents a comprehensive design for securing third‑party API calls, covering Access Key/Secret Key generation, permission granularity, timestamp and nonce based replay protection, signature creation and verification, token handling, TLS encryption, rate limiting, logging, and practical Java code examples.

Architect
Architect
Architect
How to Secure Third‑Party APIs with AK/SK, Signatures, and Token Strategies

Design Overview

Generate a unique Access Key/Secret Key (AK/SK) pair for each third‑party application, use AK for identification and SK for signing/encryption. Clients create a signature from request parameters, timestamp and nonce, and include it in headers for authentication.

Permission Division

Define appId as the global identifier, appKey (public) and appSecret (private) as a pair, and token for temporary access. Multiple appKey+appSecret pairs can be bound to the same appId to achieve fine‑grained permission control (read‑only, read‑write, delete, etc.).

Signature Process

Clients send timestamp (ms), nonce (unique random string) and sign (MD5 of sorted parameters + secret). Server validates timestamp freshness (e.g., within 60 s), checks nonce uniqueness in Redis, recomputes the signature and rejects replayed or tampered requests.

Signature Rules

Assign appId and appSecret to each caller.

Add timestamp (server time, 5‑minute validity) to mitigate DoS.

Add nonce (≥10 bytes) to prevent duplicate submissions.

Include sign calculated from all parameters and secret.

API Design Example

Typical REST endpoints (GET/POST/PUT/DELETE) with pagination, request/response formats, HTTP status codes, and a unified JSON response structure {code, message, data}. Use POST for all operations to avoid URL parameter exposure.

Security Considerations

Enforce HTTPS.

Validate AK and signature on every request.

Encrypt sensitive data with TLS.

Implement IP whitelist or Sentinel for additional protection.

Rate‑limit per IP using Redis counters.

Log requests for audit.

Mask sensitive fields in logs.

AK/SK Generation

Provide a management service to create, store (DB/Redis), rotate and distribute AK/SK securely. Store credentials in a table api_credentials with fields id, app_id, access_key, secret_key, valid_from, valid_to, enabled, allowed_endpoints, created_at.

Token Mechanism

Issue UUID tokens after user login; store token‑to‑user mapping in Redis with expiration. Use token together with signature for stateful APIs.

Implementation Samples

Java Spring interceptor SignAuthInterceptor validates timestamp, nonce and signature, stores nonce in Redis for 60 s, and rejects invalid requests. TLS setup example shows creating SSLContext and using HttpsURLConnection. Enum CodeEnum and class Result define unified response codes.

Best Practices

Separate API key management from business logic.

Rotate keys regularly.

Use RSA for encrypting sensitive payloads.

Version APIs (e.g., /v1/, /v2/).

Document APIs with Swagger.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaspringTokenAPI SecuritysignatureReplay attackAccess KeySecret Key
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.