2

我正在尝试对我的一个应用程序使用放心,但问题是我无法进行身份验证。

以下是我正在尝试的代码

baseURI = "http://localhost:8080/api";
given()
        .auth().form("admin", "admin", new FormAuthConfig("/authentication/", "j_username", "j_password"))
.when()
        .get("/formAuth")
        .then()
        .log().all()
        .statusCode(200);

我也尝试了以下选项, 以下代码和以上代码都没有解决 401 错误

form("admin", "admin", new FormAuthConfig("/", "j_username", "j_password"))

以下选项不起作用,显示无法解析登录页面。检查登录页面上的错误或指定 FormAuthConfig。错误

.auth().form("admin", "admin", formAuthConfig().withAutoDetectionOfCsrf())

以下是表格的代码

<form class="form ng-pristine ng-valid" role="form" ng-submit="login($event)">
                <div class="form-group">
                    <label for="username" translate="global.form.username" class="ng-scope">Login</label>
                    <input type="text" class="form-control ng-pristine ng-valid ng-touched" id="username" placeholder="Your login" ng-model="username" autocomplete="off">
                </div>
                <div class="form-group">
                    <label for="password" translate="login.form.password" class="ng-scope">Password</label>
                    <input type="password" class="form-control ng-pristine ng-untouched ng-valid" id="password" placeholder="Your password" ng-model="password">
                </div>
                <div class="form-group">
                    <label for="rememberMe">
                        <input type="checkbox" id="rememberMe" ng-model="rememberMe" checked="" class="ng-pristine ng-untouched ng-valid">
                        <span translate="login.form.rememberme" class="ng-scope">Automatic Login</span>
                    </label>
                </div>
                <button type="submit" class="btn btn-primary ng-scope" translate="login.form.button">Authenticate</button>
            </form>

以下是使用PostMan的捕获输出

URL: http://localhost:8080/api/authentication?cacheBuster=1485280599118
parameter:  j_username:admin
j_password:admin
4

2 回答 2

0

我相信您需要提供有关如何执行身份验证调用的更多信息。

当您定义 时new FormAuthConfig("/authentication/", "j_username", "j_password"),您是在告诉 Rest Assured 它应该/authentication使用最后两个参数作为表单参数对端点进行 POST 调用。

通常,HTML<form>元素有一个action属性指示它将向其发出 POST 请求的端点,并且标签中的name属性<input>指示应该使用的表单参数的键。

在您的情况下,HTML 表单没有提供有关如何进行调用的太多信息,因为它可能是用 Javascript 处理的。

顺便说一句,在定义FormAuthConfig如下时启用日志记录通常很有用:

given().auth()
  .form(
    "admin",
    "admin",
    new FormAuthConfig("/authentication", "j_username", "j_password")
      .withLoggingEnabled())
  // ...
于 2019-04-14T13:33:29.957 回答
0

v3 中似乎有些问题。我今天从 com.jayway.restassured 2.8.0 迁移到 io.restassured 3.0.5 时遇到了同样的问题。我刚刚回滚了升级,它又可以工作了:/

于 2017-10-24T21:19:54.383 回答