Search code examples
spring-securitykeycloakspring-webfluxspring-cloud-gateway

NoClassDefFoundError : BearerTokenServerAuthenticationEntryPoint


I am getting error NoClassDefFoundError : BearerTokenServerAuthenticationEntryPoint while instantiating SecurityWebFilterChain at .oauth2ResourceServer((oauth2) -> oauth2.jwt(Customizer.withDefaults()));

I intend to verify the token with oauth2resourceServer(keycloak).

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

application.yml

spring:
security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:9090/realms/whatsapp-clone

note : http://localhost:9090/realms/whatsapp-clone is working fine.

Now here is my bean config

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http.
                csrf(ServerHttpSecurity.CsrfSpec ::disable)
                .authorizeExchange(auth -> auth.
                        pathMatchers("/eureka/**")
                        .permitAll()
                        .anyExchange()
                        .authenticated()
                )

                .oauth2ResourceServer((oauth2) -> oauth2.jwt(Customizer.withDefaults()));

        return http.build();
    }
}

still i am getting the error mentioned in the title.


Solution

  • Try to replace the spring-boot-starter-oauth2-client with:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
    </dependency>
    

    In the existing dependencies, since as far as I can see, you are using the Resource Server mode (the request come with the jwt token in the headers) and not the Client mode (the application needs to call Keycloak to get the jwt token using user credentials, or secrets).