gpt4 book ai didi

spring - 如何在@WebFluxTest中禁用csrf保护?

转载 作者:行者123 更新时间:2023-12-02 05:27:49 25 4
gpt4 key购买 nike

为什么我在以下测试中收到 403 FORBIDDEN

@RestController
public class MyServlet {
@PostMapping("/")
public Mono<String> accept(Authentication authentication) {}
}


@WebFluxTest(MyServlet.class)
@WithMockUser
public class MyServletTest {
@Autowired
private WebTestClient webClient;

@Test
public void test() {
webClient.post().url("/")
.exchange()
.expectStatus().isOk();
}
}

结果:

java.lang.AssertionError: Status expected:<200 OK> but was:<403 FORBIDDEN>

> POST /
> WebTestClient-Request-Id: [1]
> Content-Type: [application/json]

No content

< 403 FORBIDDEN Forbidden
< Content-Type: [text/plain]
< Cache-Control: [no-cache, no-store, max-age=0, must-revalidate]
< Pragma: [no-cache]
< Expires: [0]
< X-Content-Type-Options: [nosniff]
< X-Frame-Options: [DENY]
< X-XSS-Protection: [1 ; mode=block]
< Referrer-Policy: [no-referrer]

CSRF Token has been associated to this client

据我所知,@WebFluxTest 禁用了 csrf。那么为什么它会提示呢?

最佳答案

webClient.mutateWith(SecurityMockServerConfigurers.csrf()).post()...;

关于spring - 如何在@WebFluxTest中禁用csrf保护?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60651827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com