Spring Security
HttpSecurity.securityMatcher
Select which requests enter this chain. Boot 4.0.10 / Security 7.0.9 companion project; source-reviewed, not executed.
Formal Syntax & Structure
java syntaxjava
http.securityMatcher("/api/**")Working Example
Example Snippetjava
package example.tenant;import java.util.List;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import org.springframework.security.config.Customizer;import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.http.SessionCreationPolicy;import org.springframework.security.web.SecurityFilterChain;import org.springframework.http.HttpMethod;import org.springframework.web.cors.*;@Configuration@EnableMethodSecuritypublic class ApiSecurity { @Bean @Order(1) SecurityFilterChain api(HttpSecurity http) throws Exception { return http.securityMatcher("/api/**") .sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .requestCache(c -> c.disable()) .cors(c -> c.configurationSource(apiCors())) .authorizeHttpRequests(a -> a .requestMatchers(HttpMethod.GET,"/api/tenants/*/report").hasAuthority("SCOPE_report.read") .anyRequest().denyAll()) .oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults())) .build(); // GET-only API; leave CSRF protection enabled. } private CorsConfigurationSource apiCors() { var config = new CorsConfiguration(); config.setAllowedOrigins(List.of("https://ui.example.invalid")); config.setAllowedMethods(List.of("GET")); config.setAllowedHeaders(List.of("Authorization")); config.setAllowCredentials(false); var source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/api/**",config); return source; }}