0

在响应式并使用 jwt 的 Spring Boot 应用程序中,在我的 spring-cloud-gateway 中,我有这段代码。

@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;
    }
}
  • [主] onfigReactiveWebServerApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件[/home/mac/Development/project/reactive-cloud/中定义的名称为“springSecurityWebFluxConfig”的bean时出错gateway-service/build/classes/java/main/com/example/gatewayservice/config/SpringSecurityWebFluxConfig.class]:通过构造函数参数1表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件 [/home/mac/Development/project/reactive-cloud/gateway-service/build/classes/java/main/ 中定义的名称为“userServiceImpl”的 bean 时出错com/example/gatewayservice/service/UserServiceImpl.class]:通过构造函数参数 0 表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.example.gatewayservice.repository.UserRepository”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释:{} 2019-06-27 11:47:23.336 INFO 53073 --- [
    主要] ConditionEvaluationReportLoggingListener :

试图把自动连线


SpringSecurityWebFluxConfig 中的 UserServiceImpl 类UserServiceImpl 中的 UserRepository 类JWTReactiveAuthenticationManager.class 中的 UserServiceImpl

但得到同样的错误

编辑

如果我使用

@EnableR2dbcRepositories 到 GatewayServiceApplication,我没有这个错误,但它搜索 DatabaseClient

4

1 回答 1

0

请在构成构造函数或变量级别的 UserRepository 变量的 UserServiceImpl 类中添加 @Autowired 注解。

于 2019-06-27T18:42:27.527 回答