79578350

Date: 2025-04-17 02:17:08
Score: 0.5
Natty:
Report link

Needed to add @Configuration annotation then matching started to hit to custom filterchain

@EnableWebSecurity
@ComponentScan("com.xxxx.nes.**")
@EntityScan("com.xxxx.nes.model.**")
@EnableJpaRepositories("com.xxxx.nes.model.**")
@Configuration <---
public class ResourceServeConfig {

    @Bean
    @Order(1)
    public SecurityFilterChain actuatorEndpoints(HttpSecurity http) throws Exception {
        http
                .securityMatcher("/actuator/health")
                .authorizeHttpRequests(auth -> auth.anyRequest().permitAll())
                .csrf(AbstractHttpConfigurer::disable);
        return http.build();
    }

    @Bean
    @Order(2)
    public SecurityFilterChain securedEndpoints(HttpSecurity http) throws Exception {
        http
                .csrf(csrf -> csrf.ignoringRequestMatchers("/**/notifications/**"))
                .authorizeHttpRequests(
                        auth ->
                                auth
                                        .requestMatchers("/**/notifications/**")
                                        .hasAuthority("SCOPE_notifications")
                                        .anyRequest()
                                        .authenticated()
                )
                .oauth2ResourceServer(
                        oauth -> oauth
                                .jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthenticationConverter()))
                );
        return http.build();
    }

    private JwtAuthenticationConverter jwtAuthenticationConverter() {
        JwtGrantedAuthoritiesConverter converter = new JwtGrantedAuthoritiesConverter();
        converter.setAuthorityPrefix("SCOPE_");
        converter.setAuthoritiesClaimName("scope");

        JwtAuthenticationConverter jwtConverter = new JwtAuthenticationConverter();
        jwtConverter.setJwtGrantedAuthoritiesConverter(converter);
        return jwtConverter;
    }
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Configuration
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: MOHAMMAD WASEEM