Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 39 ยท Testing and Hardening

Production Hardening Checklist for Spring Security

Harden a Spring Security app before launch: HTTPS and HSTS, secrets, actuator exposure, error messages, default deny, dependency updates and a runnable configuration audit.

A secure design can still be undone by a forgotten debug endpoint, a secret in a Git repository or a stack trace shown to visitors. Hardening is the unglamorous pass where you check configuration against a list. This lesson gives you that list, explains why each item matters, and ends with a small program that automates a few of the checks.

Spring Security cannot run on this site, so the configuration appears as read-only snippets, and the audit tool is a plain Java stand-in that you can extend for your own rules.

1. Transport security

  • Serve everything over HTTPS and redirect HTTP to it. Behind a load balancer, make Spring aware of the original scheme with server.forward-headers-strategy=framework so redirects and cookie flags are correct.
  • Send Strict-Transport-Security (HSTS) so browsers refuse plain HTTP in future. Spring Security adds it automatically on secure requests; see security headers.
  • Mark cookies Secure, HttpOnly and a suitable SameSite.
Java
http.requiresChannel(c -> c.anyRequest().requiresSecure());
YAML
server:
  servlet:
    session:
      cookie:
        secure: true
        http-only: true
        same-site: lax

2. Secrets and configuration

  • No passwords, API keys or signing keys in source control or in application.properties. Read them from environment variables, a secrets manager or a vault.
  • Use different secrets per environment, and rotate them. A leaked development key must be useless in production.
  • JWT signing keys: prefer asymmetric keys (RS256 or ES256) so resource servers hold only the public key; see JWT signing.
  • Remove the generated development password (Using generated security password) by defining real users or an identity provider.

3. Default deny

End every authorization rule set with anyRequest().authenticated() or denyAll(), never permitAll(). A new endpoint then starts locked and must be opened deliberately.

Java
http.authorizeHttpRequests(auth -> auth
    .requestMatchers("/", "/login", "/assets/**").permitAll()
    .requestMatchers("/admin/**").hasRole("ADMIN")
    .anyRequest().authenticated());

Enable method security where rules depend on data, and test it as in testing secured endpoints.

4. Actuator and management endpoints

Actuator endpoints such as env, heapdump, beans and mappings expose internals. Expose only what you need, preferably on a separate management port that is not public.

YAML
management:
  endpoints:
    web:
      exposure:
        include: health,info
  endpoint:
    health:
      show-details: never
  server:
    port: 9090

5. Errors and information leaks

  • Disable stack traces in responses: server.error.include-stacktrace=never and include-message=never (Boot's defaults for recent versions).
  • Return generic messages for authentication problems; see login throttling.
  • Do not echo user input unescaped, and keep framework banners and versions out of headers (server.server-header= empty).
  • Make 401 and 403 responses small and identical for resources a user is not allowed to know about; sometimes 404 is the better answer.

6. Dependencies and updates

Most real-world breaches use known vulnerabilities. Run a dependency scanner (OWASP Dependency-Check, your Git host's alerts, or similar) in CI, keep Spring Boot on a supported release line, and subscribe to Spring security advisories. A regular update routine beats a heroic emergency upgrade.

7. Sessions and tokens

  • Keep access tokens short-lived (minutes) and use refresh tokens with rotation; see refresh tokens and revocation.
  • Invalidate the session on logout, and let Spring change the session id after login (it does by default).
  • Limit concurrent sessions where it makes sense: http.sessionManagement(s -> s.maximumSessions(1)).

An automated configuration audit

The program below treats your settings as a map and flags risky values. It is a stand-in for the idea behind tools that lint configuration in CI: write the rules once, run them on every build.

Java Lint application settings (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Hardening review in one picture

Before you go live

HTTPS and HSTS Secure cookies too
Secrets Vault, never Git
Default deny anyRequest().authenticated()
Actuator Expose health only

Four settings that stop most mistakes

1 / 2

A checklist finds known mistakes only. For anything holding sensitive data, add a threat-modelling session and an independent penetration test before launch.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why end authorization rules with anyRequest().authenticated() or denyAll()?

    Default deny means forgetting a rule fails safe.
  2. 2. What is the risk of exposing all Actuator endpoints publicly?

    Management endpoints reveal internals and some allow changes; expose only what you need, ideally on a private port.
  3. 3. Where should a JWT signing key live?

    Keys must stay out of source control and be rotatable per environment.
  4. 4. Which header tells browsers to use HTTPS for future visits?

    HSTS pins the site to HTTPS for the period given in max-age.

Summary

  • Force HTTPS, set HSTS and secure cookie flags.
  • Keep secrets in a vault or environment, rotate them and separate environments.
  • Default deny; open endpoints deliberately.
  • Expose minimal Actuator endpoints and hide stack traces and versions.
  • Scan and update dependencies continuously, keep tokens short-lived and test your rules.

Next: Project: a secure notes API