In the Stripe Java SDK, the Webhook class (com.stripe.net.Webhook) verifies that an incoming webhook request really came from Stripe and parses it into an Event object. Its main method, Webhook.constructEvent, checks the Stripe-Signature header against the raw request body using your endpoint secret, and throws an exception if the check fails.
You should never trust a webhook payload without this check, because anyone who knows your endpoint URL can post fake JSON to it. Verification is what lets you safely mark an order paid or provision an account.
Stripe signs each event it sends. The Stripe-Signature header contains a timestamp and one or more signatures computed with HMAC-SHA256 over the timestamp and the exact raw body, using the signing secret of that endpoint (it starts with whsec_). The SDK recomputes the signature and compares it in constant time. It also rejects events whose timestamp is outside a tolerance window, 300 seconds by default, which protects against replay of old requests.
Two practical consequences follow. You must pass the raw body, byte for byte, as received, because any re-serialisation changes the signature. And the secret is per endpoint, so the one from the Dashboard (or from the Stripe CLI for local testing) must match the endpoint that receives the call.
Here is a Spring Boot controller that reads the raw body as a String and verifies it.
Declaring the body as String keeps the raw text. If you accept a parsed object instead, Spring has already consumed and transformed the body, and verification will fail. A bad signature raises SignatureVerificationException; malformed JSON raises a runtime exception from the SDK's JSON handling, so catch both and return a 400.
Once verified, switch on the event type and deserialise the embedded object.
getObject() returns an empty Optional when the event was created with an API version different from the one the SDK was built for. In that case either align the endpoint's API version with the SDK, or use deserializeUnsafe() knowing the shape might differ. Keep a default branch so new event types do not break the handler.
Verification is only part of a production webhook endpoint. Stripe retries failed deliveries for days, so design for repeats and disorder.
event.getId() and skip events you already processed, because the same event can arrive more than once.With the Stripe CLI you can forward events to your machine and get a temporary signing secret.
The CLI prints a whsec_ secret for that session; use it as stripe.webhook-secret while testing. In unit tests you can build a valid header yourself with the SDK's Webhook.Util.computeHmacSha256 over timestamp + "." + payload, then call your controller with it.
Usually the body was modified before verification, or the secret belongs to a different endpoint. Read the raw body and double-check the whsec_ value for that exact endpoint, in the right mode (test or live).
You can, but you should not. Use the Stripe CLI to get a real secret so the code path you test matches production.
The SDK allows 300 seconds. You can pass a different tolerance as a fourth argument to constructEvent if your servers have clock drift, but keep it small.
No. Stripe's servers send them. The class only verifies and parses what your endpoint receives.
The Stripe Java Webhook class authenticates incoming events with Webhook.constructEvent, using the raw body, the Stripe-Signature header and your endpoint secret. Verify first, then deserialise the event data, respond with 2xx quickly, and make processing idempotent so retries and duplicates are harmless.