Markasium sheetSheet Note N2 · Sheet 02 TR / EN

Note N2 · Markasium

FROM KEYCLOAK TO OWN JWT

Replacing Keycloak with self-signed JWTs: when it makes sense and how, in Spring Boot

Sheet
MARKASIUM
Topic
Spring Boot · Spring Security · JWT
Source
security/SecurityConfig.java
Date
2026-11-12
Reading
3 min

Short answer: if you use an identity server only as a password store, a token issuer and a Google broker, and all the login screens are already yours, removing it and signing tokens in the backend means fewer moving parts. Markasium removed Keycloak: the access token is a 15-minute ES256 JWT signed by the backend, refresh tokens are opaque and live in Redis, passwords use Argon2id, and Google sign-in is direct OIDC. Spring Security's resource server stayed as it was; none of the 28 controllers changed.

01Why it was removed

  • Depending on a preview feature: code login, the post-verification session and Google sign-in relied on Keycloak's legacy token-exchange "impersonation" feature. It was enabled with preview flags, and the standard version doesn't support impersonation; a candidate to break in an update.
  • Roles written in two places: roles were read from realm roles in the JWT. Every role change went to both the Keycloak Admin API and the database, and the two drifted apart.
  • Identity confusion: the token's sub was Keycloak's user id, not the application's. A controller that treated sub as the user id misbehaved.
  • Operational load: a separate service, a realm file, a theme, four setup scripts and "run this after Keycloak starts" steps.

The success criterion was written first: with the Keycloak process off, sign-up, password, code login, email verification, password reset, admin 2FA, Google sign-in and linking, refresh and logout must work end to end, and no Keycloak-dependent code may remain in the repo.

02Decisions

  • Access token: an ES256 JWT signed by the backend, 15 minutes. sub is now the application's user id.
  • Refresh: an opaque 32-byte random value; only its hash is in Redis, and it rotates on every use (see N3). 7 days on web, 90 on mobile.
  • Password: Argon2id; a per-account attempt lock replaced Keycloak's brute-force protection.
  • Google: authorization code + PKCE, the id_token verified with Google's public keys.
  • No silent account linking: if the email from Google matches an existing account, sign-in is refused; the user signs in with their password first and links Google from their profile. Linking automatically to an unverified account is an account takeover path.

Considered and rejected: Spring Authorization Server (unnecessary OAuth weight for custom login flows) and server-side sessions (28 controllers would change).

03Signing

JWTClaimsSet claims = new JWTClaimsSet.Builder()
        .issuer(cfg.getIssuer())
        .audience(audience(platform))              // markasium-web | markasium-mobile
        .subject(user.getId())
        .jwtID(UUID.randomUUID().toString())
        .claim("sid", UUID.randomUUID().toString())
        .claim("roles", roles(user))
        .issueTime(Date.from(now))
        .expirationTime(Date.from(now.plusSeconds(accessTtl)))
        .build();
jwt.sign(new ECDSASigner(keys.privateKey()));

The role list includes CUSTOMER for everyone, plus the user's actual role: a restaurant owner can order too.

04Validating

The resource server stayed the same; only the decoder changed. Only its own public key and only ES256 are accepted. Even with a valid signature, a wrong issuer or audience is rejected; a token signed for the web can't be used on mobile endpoints and vice versa.

processor.setJWSKeySelector(new SingleKeyJWSKeySelector<>(JWSAlgorithm.ES256, publicKey));
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(
        new JwtTimestampValidator(),
        new JwtIssuerValidator(issuer),
        audienceValidator(List.of(WEB_AUDIENCE, MOBILE_AUDIENCE))));

Roles from the roles claim become Spring authorities with the ROLE_ prefix; endpoints are protected as before with hasRole("CUSTOMER"), hasRole("RESTAURANT_OWNER"), hasAnyRole("ADMIN", "FOUNDER").

05Filter chain

JWT validation sits in the middle of the chain. Request logging, an IP filter for admin endpoints, rate limiting and request signing (HMAC + nonce) run first; single-session and device-trust checks come after validation. The web client carries the token in a cookie; which cookie gets read is decided separately (see N3).

→More notes from this project

—End of notewritten by MUHAMMEDPYZ