Skip to main content
PreparationZone Logo
PrepZone

CORS, CSRF, and Production Checklist

Browser security, CSRF protection and the checklist before you deploy.

Pass checkpoint below

Why this matters

  • Misconfigured CORS blocks legitimate frontend clients or accidentally allows any origin to call your API.
  • CSRF attacks trick authenticated users into performing unwanted actions — dangerous for cookie-based sessions.
  • A pre-deployment checklist catches the security gaps that code review misses.

CORS for browsers

L1 · Fresher

Browsers block cross-origin AJAX unless BookStore API returns Access-Control-Allow-Origin for trusted frontends.

CSRF and sessions

L2 · Intermediate

Cookie-based sessions need CSRF tokens; pure JWT APIs often disable CSRF because browsers do not auto-send Bearer tokens.

Security headers

L3 · Advanced

HSTS, X-Content-Type-Options, and CSP reduce browser-side attacks — configure via Security or reverse proxy.

Production checklist

L4 · Expert

Lock down actuator, disable dev endpoints, rotate secrets, and run dependency scanning on every Boot upgrade.

CorsFilter
JwtAuthFilter
AuthorizationFilter
Controller
Each filter handles one concern. Order matters — authentication before authorization.

CORS configuration

When the BookStore React frontend (https://app.bookstore.example.com) calls the API (https://api.bookstore.example.com), the browser enforces CORS:

Java
// learn:fragment
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
            .allowedOrigins("https://app.bookstore.example.com")
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .allowedHeaders("Authorization", "Content-Type", "X-Correlation-Id")
            .exposedHeaders("X-Correlation-Id")
            .maxAge(3600);
    }
}

Or via Spring Security:

Java
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.cors(cors -> cors.configurationSource(request -> {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowedOrigins(List.of("https://app.bookstore.example.com"));
        config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
        config.setAllowedHeaders(List.of("Authorization", "Content-Type"));
        return config;
    }));
    // ... authorization rules
    return http.build();
}

CORS rules for BookStore

  • allowedOrigins — Explicit origin list. Never use * with credentials.
  • allowedMethods — HTTP verbs the frontend may use.
  • allowedHeaders — Request headers the frontend may send.
  • exposedHeaders — Response headers the frontend JavaScript can read.
  • maxAge — How long the browser caches preflight results.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 5 sections

  • CSRF protection
  • Security headers
  • Production security checklist
  • Rate limiting example
  • Error response hygiene

Test yourself

Answer these before moving on — recall is what makes it stick.