This project demonstrates key Castle workflows in a small Spring Boot app built on the Castle Java SDK. The pages are rendered server-side with Thymeleaf and the JSON endpoints are plain Spring MVC controllers.
The app walks through a full user lifecycle. Every action mints a fresh Castle
request token in the browser (Castle.createRequestToken()) and forwards it to
the backend, which calls Castle and acts on the verdict.
- sign up –
$registrationtofilter(anonymous, so the email goes inparams):$attemptedfor a new email,$failed(resolved viamatching_user_id) for an email that already exists - login –
$loginreusing one request token across two calls:filter$attemptedfirst, thenrisk$succeededon success orfilter$failed(wrong password / unknown user) - account – post-login actions: profile update (
$profile_updatetorisk), a custom event (Castle.custom()), and logout ($logoutvia the non-blockinglogendpoint) - password reset –
$profile_resetvia the non-blockinglogendpoint - lists – the Lists API (
createList,getAllLists) - privacy – the Privacy API (
requestUserDataand a delete call to the privacy endpoint) - webhooks – incoming Castle webhooks are signature-verified with
verifyWebhookSignature(against theX-Castle-Signatureheader) and the most recent payloads are listed
You'll need a Castle account. If you don't have one, start a free trial at
https://castle.io. For local development, use a sandbox environment so demo
traffic from localhost stays separate from production data — from the Castle
dashboard (Settings → API) grab the sandbox keys:
- your publishable key (
castle_pk) – used by the browser SDK - your API secret (
castle_api_secret) – used by the backend SDK
These are the only two values you need to configure.
Requires JDK 17+ and Maven.
git clone https://github.com/castle/castle-java-example.git
cd castle-java-exampleThe Castle browser SDK and the Tailwind stylesheet are built from npm, so install the dependencies and build the CSS:
npm install
npm run build:cssCreate your .env from the example and fill in your two Castle keys:
cp .env_example .envRun the app:
mvn spring-boot:run
# Castle Java demo listening on http://localhost:4009The server listens on :4009 (override with PORT). Open
http://localhost:4009.
The bundled Dockerfile builds the stylesheet and browser SDK, compiles the
Spring Boot jar and serves the app on port 80.
docker build -t castle-java-example .
docker run -d -p 4009:80 \
-e castle_pk=YOUR_PUBLISHABLE_KEY \
-e castle_api_secret=YOUR_API_SECRET \
castle-java-exampleThe app will be available at http://127.0.0.1:4009. Point it at a Castle sandbox environment when running locally.
mvn testWe're sharing this sample app in the hope that other developers find it
valuable. Although it is not an officially supported sample, we welcome
questions and suggestions at support@castle.io.