java - 将标头添加到请求时的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();
}
}
谁能指出我的问题。这肯定看起来像缺少配置问题。
解决方案
我可以看到您的代码片段有两个问题。
首先,您不应该添加@EnableWebFlux
,因为它完全禁用了 Spring Boot 完成的自动配置。@EnableWebMvc
在 Spring MVC 应用程序中也是如此。
其次,您正在混合使用 WebFlux 注释和 WebFlux 功能。您使用的注解很好,但该ServerResponse
类型只应在编写功能处理程序时使用。您应该尝试在这里使用ResponseEntity
.
推荐阅读
- laravel - 事件和监听器不起作用?
- c# - 跟踪用户在 FileDialog 中所做的事情
- racket - 如何告诉`make-module-evaluator`使用像#lang这样的自定义阅读器?
- c++ - BoUML 工具已停止在类视图上显示滚动条
- python - 在 SciKit Learn 中使用相同的预处理器缩放数据框中的多列
- r - 在数据框中找到点 (col_1[ i ], col_2[ i ]) = (col_1[ j ], -col_2[ j ])
- python - 如何使用opencv和python找到两个重叠物体的质心
- javascript - SqlLite-WebSQL 选择并添加到传单地图作为 geojson 层
- python-3.x - 如何使用 python 将图像转换为多边形网格 3d 模型
- react-native - 将文本包裹在视图内而不会溢出