Java 放心表单身份验证不起作用
我正在尝试为我的一个应用程序使用rest-assured,问题是我无法进行身份验证 下面是我正在尝试的代码Java 放心表单身份验证不起作用,java,rest,rest-assured,web-api-testing,Java,Rest,Rest Assured,Web Api Testing,我正在尝试为我的一个应用程序使用rest-assured,问题是我无法进行身份验证 下面是我正在尝试的代码 baseURI = "http://localhost:8080/api"; given() .auth().form("admin", "admin", new FormAuthConfig("/authentication/", "j_username", "j_password")) .when() .get("/formAuth")
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>
v3中似乎有东西坏了。我今天从com.jayway.restassured 2.8.0转到io.restassured 3.0.5时也遇到了同样的问题。我刚刚回滚了升级,它再次起作用:/我认为您需要提供有关如何执行身份验证调用的更多信息 当您定义
新FormAuthConfig(“/authentication/”、“j_用户名”、“j_密码”)
时,您告诉Rest Assured它应该使用最后两个参数作为表单参数对/authentication
端点进行POST调用
通常,HTML
元素有一个action
属性,指示它将向其发出POST请求的端点,
标记中的name
属性指示应该使用的表单参数的键
在您的例子中,HTML表单没有提供关于如何进行调用的很多信息,因为它可能是用Javascript处理的
顺便说一句,在定义FormAuthConfig
时,启用日志记录通常很有用,如下所示:
given().auth()
.form(
"admin",
"admin",
new FormAuthConfig("/authentication", "j_username", "j_password")
.withLoggingEnabled())
// ...
如果from中的字段中没有“j_”,为什么名称以“j_”开头?
given().auth()
.form(
"admin",
"admin",
new FormAuthConfig("/authentication", "j_username", "j_password")
.withLoggingEnabled())
// ...