Rest-Assured表单身份验证不起作用

问题描述 投票:2回答: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);

我也尝试过以下选项,对于以下代码和上面的代码没有任何问题

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
java rest rest-assured web-api-testing
2个回答
0
投票

在v3中似乎有些东西被破坏了。今天我从com.jayway.restassured 2.8.0转到io.restassured 3.0.5时遇到同样的问题。我刚刚回滚升级,它再次起作用:/


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())
  // ...
© www.soinside.com 2019 - 2024. All rights reserved.