Content Security Policy is application-specific, so configure it for the actual scripts and frames rather than assuming default headers cover it.
Spring Security CSP rollout: enforce tested asset rules at the response edge
Defaults do not supply a policy
Spring Security adds several response headers by default, but it cannot infer a safe Content Security Policy for a receipt console. A restrictive policy can reduce the harm of injected markup, yet it does not replace output encoding or input validation. Start with the real asset inventory: scripts, styles, images, fonts and frames. The selected filter chain must add the header to the browser responses that render those assets.
Roll out with evidence
Use a report-only policy first on representative pages. Inspect violations for inline scripts, third-party widgets and browser extensions; not every report indicates a required source. Remove or nonce inline code where practical, then enforce a policy narrow enough for the product. A broad wildcard or unsafe-inline may keep the page working while weakening the rule. Keep any reporting endpoint rate-limited and avoid storing complete user URLs if they carry sensitive query parameters.
Test every page family
Open login, receipt list, receipt detail and error pages with the enforced policy. Check the browser console and actual HTTP header, including reverse-proxy behavior. An API response that returns only JSON may need a different policy than the operator UI. CORS and CSP answer different questions: CORS gates cross-origin response access, while CSP constrains resources a rendered page may load.
Implementation contract
http.headers(headers -> headers
.contentSecurityPolicy(csp -> csp
.policyDirectives("default-src 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'none'")));Cost and verification
The header adds few bytes per response. Report collection, asset refactoring and browser regression testing are the real costs; an overbroad policy offers little protection.
Common Mistakes
- Do not assume Spring Security creates a CSP by default.
- Do not enforce an untested policy that breaks login or checkout scripts.
- Do not use CSP as a replacement for encoding untrusted output.
Read next
Spring Security filter chain: authentication, CSRF and request order, Spring Security multiple filter chains: match the whole request surface, Spring Security CORS preflight: evaluate origin before authentication, Spring Session cookie policy: SameSite, Secure and the reverse proxy, Spring file uploads: discard the supplied filename before writing.
