针对本地主机的放心请求没有有效的面包屑

问题描述 投票:0回答:1

我有这个放心的代码,它针对我在本地主机上运行的应用程序进行测试:

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 security,我的应用程序非常简单,只有一个普通的控制器和服务类。

@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 错误而失败。

这是完整的应用程序并在 Github 上进行测试:

https://github.com/francislainy/coderock-investment-backend-challenge/blob/master/src/test/java/com/francislainy/coderockinvestment/functiontests/FunctionalIT.java

谢谢你。

spring-boot http postman rest-assured insomnia
1个回答
0
投票

问题现已解决,似乎与 Jenkins 以某种方式拦截对 8080 端口的请求有关,我只有在尝试对 Firefox get 请求时才能发现这一点,因为 Chrome、Edge 和 Safari 都是所有人仍然能够绕过这个问题并给我一个正确的回应,类似于邮递员的做法。所以我现在将我的应用程序指向端口 8081

© www.soinside.com 2019 - 2024. All rights reserved.