java 放心承载认证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42790021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rest Assured Bearer authentication
提问by Klugsman
I am new to using Rest Assured,Java and Api testing so please be gentle with me. When I use rest assured to test an api that uses Bearer authentication the tests fail resulting in:- java.net.ConnectException: Connection refused: connect
我是使用 Rest Assured、Java 和 Api 测试的新手,所以请对我保持温和。当我使用放心测试使用承载身份验证的 api 时,测试失败导致:- java.net.ConnectException:连接被拒绝:连接
I know the issue is likely to do with the authentication but am unsure on how to use "Bearer". I searched around and believe that somehow I need to make an initial request using my username and password. Then get a token back to be used for bearer authentication. Please can someone help me to do this with a very simple example?
我知道问题可能与身份验证有关,但不确定如何使用“承载”。我四处搜索并相信我需要以某种方式使用我的用户名和密码进行初始请求。然后取回令牌以用于承载身份验证。请有人用一个非常简单的例子帮助我做到这一点吗?
My code is
我的代码是
import com.jayway.restassured.RestAssured;
import static com.jayway.restassured.RestAssured.*;
import static org.hamcrest.Matchers.hasItem;
@BeforeTest
public void setUp() {
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
RestAssured.authentication = preemptive().basic("username","password");
}
@Test
public void successfulTest() {
given()
.contentType("application/json; charset=UTF-8");
when().
get("http://mydomain/testpath/Id=2").
then().
statusCode(200);
}
回答by PTT
Response response =
given()
.headers(
"Authorization",
"Bearer " + bearerToken,
"Content-Type",
ContentType.JSON,
"Accept",
ContentType.JSON)
.when()
.get(url)
.then()
.contentType(ContentType.JSON)
.extract()
.response();
回答by Leandro Santana
In order to get the bearer token you can use this code to authorize your request:
为了获得不记名令牌,您可以使用此代码来授权您的请求:
PreemptiveBasicAuthScheme authScheme = new PreemptiveBasicAuthScheme();
authScheme.setUserName("login");
authScheme.setPassword("password");
RestAssured.authentication = authScheme;
After you get the token, send it in your request this way:
获得令牌后,以这种方式在您的请求中发送它:
response = given().auth().oauth2(token).get("http://mydomain/testpath/Id=2");
回答by MikeJRamsey56
My Cucumber step definition looks like this:
我的 Cucumber 步骤定义如下所示:
// Class variables
private String token_resource = "/yourApp/oauth/token?username=";
private String endpoint_rest="https://your.app.domain.com/";
private String acessToken;
@When("^user gets access token using userId \"(.+)\" and password \"(.+)\"$")
public void getAccessToken(String userName, String password){
RequestSpecification requestSpec = RestAssured.with();
requestSpec.given().contentType("application/json");
requestSpec.headers("Authorization", "Basic your-string-here");
Response response = requestSpec.post(endpoint_rest + token_resource + userName + "&password=" + password + "&client_id=yourApp&grant_type=password");
String responseMsg = response.asString();
System.out.println(">> responseMsg=" + responseMsg);
assertTrue("Missing access token",responseMsg.contains("access_token"));
System.out.println(">> Get Access token RESPONSE: " + responseMsg);
DocumentContext doc = JsonPath.parse(responseMsg);
acessToken= doc.read("access_token");
System.out.println(" >> doc.read access_token= " + acessToken);
}
Much depends on how your endpoint was coded.
在很大程度上取决于您的端点是如何编码的。
When I want to learn this kind of thing I go to the Rest-assured examplesand search.
当我想学习这种东西时,我会去 Rest-assured examples和搜索。
Herefor instance.
以这里为例。
回答by lniniaa
If the error was "Connection refused", it sounds more like a network issue instead of authentication. Basically you haven't reached the point to authenticate your client with the service. You can check to see if your service is running on a different port other than 80. If that's the case, just provide the port before sending out the request:
如果错误是“连接被拒绝”,则听起来更像是网络问题而不是身份验证。基本上,您还没有达到使用服务对您的客户端进行身份验证的程度。您可以检查您的服务是否在 80 以外的其他端口上运行。如果是这种情况,只需在发送请求之前提供端口:
given().port(your_port_number)
You can use a more visualized rest client app to try your request to make sure it actually works before putting it into your code. "Postman" could be a good candidate.
您可以使用更直观的 rest 客户端应用程序来尝试您的请求,以确保它在将其放入您的代码之前确实有效。“邮递员”可能是一个很好的候选人。