You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rest Assured请求本地服务报403:No valid crumb was included in the request

解决Rest Assured请求返回403 "No valid crumb was included in the request"问题

测试代码

public class FunctionalIT {
@Test
    void createInvestment() {
        Investment investment = Investment.builder()
                        .ownerId(UUID.randomUUID())
                        .amount(BigDecimal.valueOf(1000))
                        .creationDate(LocalDate.now())
                        .build();

        RestAssured.given()
                .contentType(ContentType.JSON)
                .body(investment)
                .baseUri("http://localhost:8080")
                .when()
                .post("/api/investments")
                .then()
                .statusCode(201);
    }
}

错误信息

执行测试后收到403错误,完整错误日志如下:

francislainycampos/.m2/repository/org/slf4j/slf4j-api/2.0.12/slf4j-api-2.0.12.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.francislainy.coderockinvestment.functionaltests.FunctionalIT,createInvestment
Request method: POST
Request URI:    http://localhost:8080/api/investments
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
                Content-Type=application/json
Cookies:        <none>
Multiparts:     <none>
Body:
{
    "id": null,
    "ownerId": "5d63e45a-5bf6-45cb-9bd8-c803b8d12c90",
    "creationDate": [
        2024,
        4,
        9
    ],
    "amount": 1000,
    "expectedBalance": null
}
HTTP/1.1 403 Forbidden
X-Content-Type-Options: nosniff
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Content-Length: 555
Server: Jetty(10.0.15)

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
    <title>Error 403 No valid crumb was included in the request</title>
  </head>
  <body>
    <h2>HTTP ERROR 403 No valid crumb was included in the request</h2>
    <table>
      <tr>
        <th colspan="1" rowspan="1">URI:</th>
        <td colspan="1" rowspan="1">/api/investments</td>
      </tr>
      <tr>
        <th colspan="1" rowspan="1">STATUS:</th>
        <td colspan="1" rowspan="1">403</td>
      </tr>
      <tr>
        <th colspan="1" rowspan="1">MESSAGE:</th>
        <td colspan="1" rowspan="1">No valid crumb was included in the request</td>
      </tr>
      <tr>
        <th colspan="1" rowspan="1">SERVLET:</th>
        <td colspan="1" rowspan="1">Stapler</td>
      </tr>
    </table>
    <hr/>
    <a shape="rect" href="https://eclipse.org/jetty">Powered by Jetty:// 10.0.15</a>
    <hr/>
  </body>
</html>

应用代码

我的Spring Boot应用未使用Spring Security,仅包含常规Controller和Service:

@RestController
@RequestMapping("/api/investments")
@RequiredArgsConstructor
public class AppController {

    private final AppServiceImpl appService;

    @PostMapping
    public ResponseEntity<Object> createInvestment(@Valid @RequestBody Investment investment) {
        return new ResponseEntity<>(appService.createInvestment(investment), HttpStatus.CREATED);
    }
}

问题说明

该请求在Postman和Mac终端中可正常执行,但在Insomnia和Rest Assured中均返回相同的403错误。


解决方案

1. 排查端口占用

错误日志中的Stapler是Jenkins的核心Servlet,且服务器为Jetty,说明你的Rest Assured请求实际发送到了本地运行的Jenkins服务(默认端口8080),而非自己的Spring Boot应用。

在Mac终端执行以下命令查看8080端口的占用进程:

lsof -i :8080

若输出包含Jenkins相关进程,即可确认端口冲突。

2. 解决端口冲突

有两种处理方式:

  • 停止Jenkins服务:若无需Jenkins运行,直接停止它,让Spring Boot应用占用8080端口。
  • 修改应用端口:在application.properties或application.yml中设置自定义端口,例如:
    server.port=8081
    
    同时更新Rest Assured测试代码中的baseUri为http://localhost:8081。

3. 验证修复

重新启动应用并执行测试,确认请求能正常返回201状态码。


内容的提问来源于stack exchange,提问作者Francislainy Campos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 05:22:48