🖨️ Version PDF
Sans sécurité :
Objectifs :
Exemple :
Dépendance :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Sans config, Spring bloque tout (401).
@Bean SecurityFilterChain security(HttpSecurity http) throws Exception { return http .csrf(csrf -> csrf.disable()) .authorizeHttpRequests(auth -> auth .requestMatchers("/actuator/health").permitAll() .requestMatchers(HttpMethod.POST, "/api/races/**").hasRole("ADMIN") .requestMatchers(HttpMethod.POST, "/api/concours/**").hasRole("ADMIN") .anyRequest().authenticated() ) .httpBasic(Customizer.withDefaults()) // d'abord on fait simple .build(); }
Pourquoi commencer avec httpBasic ?
httpBasic :
JWT :
(voir module tests avancés pour exemples complets)
csrf.disable()