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

    Production Security

    Production security for TechLearningPRO Orders is layered defence, not a single Spring Security filter.

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

    Introduction

    Production security for TechLearningPRO Orders is layered defence, not a single Spring Security filter. On Boot 3 / Java 21 you still need HTTPS/HSTS, dependency and image scanning, secrets outside git, least-privilege IAM, rate limits at the gateway, and audit trails for money and admin actions.

    Most breaches exploit basics: outdated libraries, leaked credentials, over-exposed Actuator, or containers running as root. The high-impact checklist is boring — and that is the point.

    Security defaults should be the easy path: deny-by-default HTTP security, validated inputs, and CI gates that block known CVEs. Clever crypto does not compensate for a public /actuator/env.

    This lesson turns Orders into a service you can put on the public internet without relying on obscurity.

    Purpose of this lesson

    Apply a production security checklist to Orders: TLS, scanning, secrets, container hardening, gateway controls, and audit.

    Understanding the topic

    Cheap, high-impact layers every Boot 3 app should ship.

    • Transport: HTTPS everywhere; HSTS at the edge.
    • Supply chain: Dependabot/Snyk on PRs; Trivy on images in CI.
    • Secrets: vault / K8s Secrets; rotate on a schedule.
    • Runtime: non-root container; read-only root FS where possible.
    • Edge: WAF + rate limits; lock down Actuator.
    • Identity: least-privilege DB and cloud roles per service.
    • Audit: log login, admin, and payment-adjacent actions with trace ids.

    Step-by-step explanation

    1. Terminate TLS at the gateway/ingress; enable HSTS.
    2. Turn on dependency and image scanning in CI; fail the build on high CVEs.
    3. Move all credentials to a secret store injected as env; delete committed secrets.
    4. Run the Orders image as non-root; restrict Actuator; add gateway rate limits and audit events for place-order and refunds.

    Informative example

    Security defaults sketch for Orders (Boot 3 + container).

    java
    // SecurityFilterChain — deny by default, CSRF off for pure token API
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig {
    @Bean
    SecurityFilterChain api(HttpSecurity http) throws Exception {
    http.csrf(csrf -> csrf.disable())
    .sessionManagement(s -> s.sessionCreationPolicy(STATELESS))
    .authorizeHttpRequests(auth -> auth
    .requestMatchers("/actuator/health", "/actuator/info").permitAll()
    .requestMatchers("/actuator/**").hasRole("OPS")
    .requestMatchers("/api/orders/**").authenticated()
    .anyRequest().denyAll());
    return http.build();
    }
    }
    # Dockerfile excerpt — non-root
    FROM eclipse-temurin:21-jre
    RUN useradd -r -u 10001 orders
    USER 10001
    COPY target/orders.jar /app/orders.jar
    ENTRYPOINT ["java","-jar","/app/orders.jar"]

    Real-world use

    An Orders Actuator path was briefly exposed without auth during a misconfigured ingress. Attackers enumerated env property names before the hole closed. TechLearningPRO now requires network policies plus role-gated Actuator and treats public management endpoints as a sev-1.

    Security implications

    This lesson is the security posture — enforce it in code and pipeline.

    • Never commit real passwords, API tokens, or private keys in samples.
    • Rotate credentials after any suspected leak; assume screenshots are public.
    • Keep jakarta validation and authz on every write path — including internal admin APIs.

    Best practices

    • Make secure defaults the path of least resistance in starters and templates.
    • Separate IAM roles per service; never share a cluster-admin-like DB user.
    • Pair rate limits with auth — anonymous routes get stricter quotas.

    Common mistakes

    • Shipping with default or example credentials still in config.
    • Running containers as root 'just for local parity' in prod.
    • Ignoring transitive CVEs because 'it still compiles'.

    Advanced interview questions

    Interview Prep

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

    3 questions
    1BeginnerQuestionWhat is defence in depth for a Spring Boot API?+

    Answer

    TLS, authn/authz, input validation, secret hygiene, scanning, edge rate limits, and least privilege — layers that still protect if one fails.
    2IntermediateQuestionWhy run containers as non-root?+

    Answer

    Limits blast radius if the process is compromised; many cluster policies require it.
    3IntermediateQuestionWhy audit money and admin actions?+

    Answer

    Forensics and compliance need who did what; metrics alone do not reconstruct a refund dispute.

    Hands-on exercise

    Lab. Harden Orders: non-root image, Actuator locked except health, CI Trivy/Snyk gate, and an audit log line on place that includes principal and order id — never card PANs or raw tokens.

    Summary

    Production security makes Orders safe to expose. Next: engineer performance with SLOs so hardening does not become guesswork.

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