首页 > 解决方案 > 将标头添加到请求时的Webflux Http 404响应

问题描述

我目前正在尝试在反应堆栈上将我的应用程序从 spring boot 1.5.x 转移到 2.xx。我正面临一个我无法弄清楚的奇怪问题。希望有人知道这个问题的解决方案。

我实现了一个 api 来接收用户 jwt 令牌作为标题上的“授权”字段。api是一个POST方法,在body中接收来自用户的某个json数据,到后端进行处理。不幸的是,当我添加标题时,我不断收到 http 404 错误,当我在邮递员中删除它时,正常的 200 错误。这是我的控制器。

@RestController
@RequestMapping("/user")
@Slf4j
public class UserHandler {

    @Autowired
    private UserService service;

    @Autowired
    private Utility utility;

    @PostMapping("/updateLink")
    public Mono<ServerResponse> addNewAccountLinkAPI(@RequestHeader(name="Authorization") String id, @RequestBody UpdateAccountLink request){
        return Mono.just(request)
                .flatMap(s -> service.addNewAccountLink(s))
                .flatMap(s -> ok().body(BodyInserters.fromObject(new RespWrap("Success", new Date(), null, s))))
                .switchIfEmpty(badRequest().body(BodyInserters.fromObject(new RespWrap("Failed", new Date(), "Failed to create new link", null))));
    }
}

这是我的简单安全配置

@Configuration
@EnableWebFluxSecurity
@EnableWebFlux
public class ResourceServerConfig implements WebFluxConfigurer {

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http, FirebaseAuthenticationManager manager) {
        http
                .authorizeExchange().anyExchange().permitAll()
                .and().csrf().disable();
        return http.build();
    }
}

谁能指出我的问题。这肯定看起来像缺少配置问题。

标签: javaspring-bootspring-webflux

解决方案


我可以看到您的代码片段有两个问题。

首先,您不应该添加@EnableWebFlux,因为它完全禁用了 Spring Boot 完成的自动配置。@EnableWebMvc在 Spring MVC 应用程序中也是如此。

其次,您正在混合使用 WebFlux 注释和 WebFlux 功能。您使用的注解很好,但该ServerResponse类型只应在编写功能处理程序时使用。您应该尝试在这里使用ResponseEntity.


推荐阅读