Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 105

    Authentication Challenges

    Authentication mistakes are public and expensive.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Authentication mistakes are public and expensive. These challenges make you implement real Boot 3 / Security 6 flows for a TechLearningPRO-style identity service: signup, login, email verification, password reset, refresh-token rotation with reuse detection, TOTP 2FA, Google OIDC, and device session revoke.

    Use placeholders for client ids and mail credentials — never commit secrets. Prefer jakarta.* validation annotations.

    Build it, break it with replay tests, then harden it.

    Purpose of this lesson

    Implement end-to-end auth flows with JWT/session options, rotation, 2FA, OIDC, and session management on Boot 3.

    Understanding the topic

    Five builds — each has abuse tests.

    • Core auth — signup, login, verify email, reset password.
    • Refresh rotation — reuse detection revokes the family.
    • 2FA TOTP — enroll, challenge, backup codes.
    • Google OIDC — authorization code flow into local user link.
    • Device sessions — list and revoke individual sessions.

    Step-by-step explanation

    1. Model users, refresh_tokens, totp_secrets, sessions tables via Flyway.
    2. Implement password hash with BCrypt; issue access + refresh tokens.
    3. Add rotation: storing token hash only; on reuse, revoke family.
    4. Add TOTP enroll/verify and OIDC login; expose session list/revoke APIs.

    Informative example

    Refresh rotation with reuse detection (sketch).

    java
    @Service
    public class RefreshService {
    public TokenPair rotate(String presentedRaw) {
    String hash = sha256(presentedRaw);
    RefreshToken current = repo.findByHash(hash)
    .orElseThrow(() -> new InvalidRefreshException());
    if (current.isRevoked()) {
    repo.revokeFamily(current.familyId()); // reuse detected
    throw new ReuseDetectedException();
    }
    current.revoke();
    RefreshToken next = RefreshToken.newInFamily(current.familyId());
    repo.save(current);
    repo.save(next);
    return tokens.issue(current.userId(), next.rawOnce());
    }
    }
    // Abuse test
    @Test
    void reusedRefresh_revokesFamily() {
    TokenPair first = login();
    TokenPair second = refresh(first.refresh());
    assertThrows(ReuseDetectedException.class, () -> refresh(first.refresh()));
    }

    Real-world use

    A leaked refresh token once let an attacker mint new access tokens for a TechLearningPRO admin after password change. Rotation with family revoke stopped the session within one reuse attempt — the exact failure mode this challenge encodes in tests.

    Security implications

    Auth code is high-risk surface area.

    • Never commit OAuth client secrets — use env placeholders.
    • Disable dangerous account enumeration where policy requires.
    • Step-up MFA for password change and email change.

    Best practices

    • Short-lived access tokens; rotatable refresh tokens.
    • Hash tokens at rest; encrypt TOTP secrets with KMS.
    • Rate-limit login and reset endpoints.

    Common mistakes

    • Storing raw refresh tokens in the database.
    • Silent reuse (issuing new tokens without revoking the family).
    • Logging TOTP secrets or backup codes.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1AdvancedQuestionWhat is refresh-token reuse detection?+

    Answer

    If a revoked or already-rotated refresh token is presented, revoke the entire token family because theft is likely.
    2IntermediateQuestionWhy hash refresh tokens at rest?+

    Answer

    DB leaks should not yield immediately usable bearer credentials.
    3BeginnerQuestionWhen is session listing valuable?+

    Answer

    Users can spot and revoke unknown devices after laptop loss or shared-computer login.

    Hands-on exercise

    Auth challenge pack. 1) Signup/login/verify/reset with email token (mailer stubbed). 2) Refresh rotation + reuse test that asserts family revoke. 3) TOTP enroll + login challenge. 4) 'Sign in with Google' via OIDC (mock provider in tests). 5) GET /api/sessions + DELETE /api/sessions/{id}. Definition of done: SecurityFilterChain tests green; no secrets in repo; README with threat notes.

    Summary

    Auth challenges harden identity for Orders-adjacent apps. Next: architecture whiteboard challenges.

    Ready to mark this lesson complete?Track your journey across the entire course.