gpt4 book ai didi

java - 无法使用 spring security 登录 Rest API

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:33 24 4
gpt4 key购买 nike

我需要使用 REST Assured API 自动化 REST API,但遇到问题。使用 REST Assured API,我需要使用 Spring Security 配置登录。

请看下面的代码:

@Before
public void configure() {
RestAssured.baseURI = "https://example.com";
RestAssured.basePath = "/my-portal";
}

@Test
public void apiLogin() {
given().auth().form("username", "password", springSecurity().withLoggingEnabled(new LogConfig())).when().get();
}

我面临的问题是:

实际请求 URI:https://example.com/j_spring_security_check
预期请求 URI:https://example.com/my-portal/j_spring_security_check

以下是获取的响应:

Request method: POST
Request URI: https://example.com/j_spring_security_check
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: j_username={username}
j_password={password}
Path params: <none>
Multiparts: <none>
Headers: Accept=*/*
Content-Type=application/x-www-form-urlencoded; charset=ISO-8859-1
Cookies: <none>
Body: <none>
HTTP/1.1 404 Not Found
Date: Mon, 06 Feb 2017 10:51:41 GMT
Content-Length: 0
Connection: close
Content-Type: text/plain

最佳答案

我成功了。只需几个错误就可以了。

下面是工作代码:

final StringWriter writer = new StringWriter();
final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
Response s = given().auth().
form(userName, password,
FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true)))
.post("/home/myapp.html");

System.out.println(s.getBody().asString());

它提供 HTTP 状态代码:302(即重定向到页面/home/myapp.html)。

关于java - 无法使用 spring security 登录 Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42071197/

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