【问题标题】:Unable to set the curl's "User" Parameter in RestAssured无法在 RestAssured 中设置 curl 的“用户”参数
【发布时间】:2019-12-25 22:10:41
【问题描述】:


我正在尝试在 RestAssured 测试中移植以下 curl shell:

curl -X POST http://localhost:8180/auth/realms/demo/protocol/openid-connect/token \
    --user backend-service:secret \
    -H 'content-type: application/x-www-form-urlencoded' \
    -d 'username=alice&password=alice&grant_type=password'

这是我的 RestAssured 版本:

response = given().urlEncodingEnabled(true)
        .auth().basic("backend-service", "secret")
        .param("grant_type", "password")
        .param("client_id", "backend-service")
        .param("username", "alice")
        .param("password", "alice")
        .header("Accept", ContentType.JSON.getAcceptHeader())
        .post("http://localhost:8180/auth/realms/demo/protocol/openid-connect/token")
        .then().statusCode(200).extract()
        .response();

不幸的是,问题在于“--user”参数,它没有通过 auth().basic() 进行翻译。从服务器日志中我可以看到它是唯一为空的参数:

type=LOGIN_ERROR, realmId=demo, clientId=backend-service, userId=null, ipAddress=127.0.0.1, error=invalid_client_credentials, grant_type=password

知道如何配置它吗? 谢谢

【问题讨论】:

  • 你试过.auth().preemptive()吗?
  • 谢谢,auth().preemptive() 成功了!

标签: rest-assured


【解决方案1】:

我认为您应该使用常规身份验证(也称为抢先身份验证 - 例如 Curl 是如何做到的)::

response = given().urlEncodingEnabled(true)
        .auth().preemptive().basic("backend-service", "secret")
        ...

更多信息您可以找到here

【讨论】:

    猜你喜欢
    • 2013-06-12
    • 2016-09-16
    • 2015-06-03
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多