0

In a spring boot application who are reactive and use jwt, in my spring-cloud-gateway, I have this code.

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServiceApplication.class, args);
    }

}

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SpringSecurityWebFluxConfig {

    private final UserServiceImpl userService;
    private final JwtTokenUtil tokenUtil;

    private static final String[] AUTH_WHITELIST = {
        "/resources/**",
        "/webjars/**",
        "/authorize/**",
        "/favicon.ico"};


    public SpringSecurityWebFluxConfig(JwtTokenUtil tokenUtil, UserServiceImpl userService) {
        this.tokenUtil = tokenUtil;
        this.userService = userService;
    }
    ..
}

@Service
public class UserServiceImpl implements ReactiveUserDetailsService, UserService {

    private final UserRepository userRepository;

    public UserServiceImpl(final UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    ...

}

@Repository
public interface UserRepository extends ReactiveCrudRepository<User, Integer>{
}

public class JWTHeadersExchangeMatcher implements ServerWebExchangeMatcher {
    @Override
    public Mono<MatchResult> matches(final ServerWebExchange exchange) {
    }
}

public class JWTReactiveAuthenticationManager implements ReactiveAuthenticationManager {
    ...

    public JWTReactiveAuthenticationManager(final PasswordEncoder passwordEncoder, final UserServiceImpl userService) {
        this.passwordEncoder = passwordEncoder;
        this.userService = userService;
    }
}


public class JwtTokenUtil {
    ...
}

public class TokenAuthenticationConverter implements Function<ServerWebExchange, Mono<Authentication>> {
     private final JwtTokenUtil tokenProvider;

    public TokenAuthenticationConverter(JwtTokenUtil tokenProvider) {
        this.tokenProvider = tokenProvider;
    }
}

public class TokenAuthenticationConverter implements Function<ServerWebExchange, Mono<Authentication>> {
     private final JwtTokenUtil tokenProvider;

    public TokenAuthenticationConverter(JwtTokenUtil tokenProvider) {
        this.tokenProvider = tokenProvider;
    }
}
  • [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSecurityWebFluxConfig' defined in file [/home/mac/Development/project/reactive-cloud/gateway-service/build/classes/java/main/com/example/gatewayservice/config/SpringSecurityWebFluxConfig.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl' defined in file [/home/mac/Development/project/reactive-cloud/gateway-service/build/classes/java/main/com/example/gatewayservice/service/UserServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.gatewayservice.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 2019-06-27 11:47:23.336 INFO 53073 --- [
    main] ConditionEvaluationReportLoggingListener :

Tried to put autowired

on UserServiceImpl in SpringSecurityWebFluxConfig class
on UserRepository in UserServiceImpl class
on UserServiceImpl in JWTReactiveAuthenticationManager.class

but get same error

Edit

if i use

@EnableR2dbcRepositories to GatewayServiceApplication, I dont't have this error but it search about DatabaseClient

robert trudel
  • 5,283
  • 17
  • 72
  • 124

1 Answers1

0

Please add @Autowired annotation in UserServiceImpl class for UserRepository variable in constitute constructor or variable level.

GnanaJeyam
  • 2,780
  • 16
  • 27
  • "If a bean has one constructor, you can omit the @Autowired", from [Spring Beans and Dependency Injection](https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/using-boot-spring-beans-and-dependency-injection.html). – Chris Goldman Jun 27 '19 at 20:24