我們正試圖測試使用Windows身份驗證的api。我正在使用休息保證軟件包來測試api的。任何建議如何將Windows用戶登錄到API請求標頭?在io.restassured api測試中使用Windows身份驗證
回答
已經解決了我的問題 因此,目前很難做到這一點,因爲您需要爲REST Assured配置HTTPClient,但它僅支持已棄用的AbstractHttpClient
。這是我的實現。但我沒有能力測試它...
import io.restassured.RestAssured;
import org.apache.http.annotation.Contract;
import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthSchemeRegistry;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.auth.win.WindowsCredentialsProvider;
import org.apache.http.impl.auth.win.WindowsNegotiateScheme;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.SystemDefaultCredentialsProvider;
import org.apache.http.impl.client.SystemDefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.config.HttpClientConfig.httpClientConfig;
import static java.net.HttpURLConnection.HTTP_OK;
public class WinHttpClientTest {
@Test
public void test() {
@Contract(threading = ThreadingBehavior.IMMUTABLE)
class WindowsNTLMSchemeFactory implements AuthSchemeProvider, AuthSchemeFactory {
private final String servicePrincipalName;
public WindowsNTLMSchemeFactory(final String servicePrincipalName) {
super();
this.servicePrincipalName = servicePrincipalName;
}
@Override
public AuthScheme create(final HttpContext context) {
return new WindowsNegotiateScheme(AuthSchemes.NTLM, servicePrincipalName);
}
@Override
public AuthScheme newInstance(HttpParams params) {
return new WindowsNegotiateScheme(AuthSchemes.NTLM, null) ;
}
}
@Contract(threading = ThreadingBehavior.IMMUTABLE)
class WindowsNegotiateSchemeFactory implements AuthSchemeProvider, AuthSchemeFactory {
private final String servicePrincipalName;
public WindowsNegotiateSchemeFactory(final String servicePrincipalName) {
super();
this.servicePrincipalName = servicePrincipalName;
}
@Override
public AuthScheme create(final HttpContext context) {
return new WindowsNegotiateScheme(AuthSchemes.SPNEGO, servicePrincipalName);
}
@Override
public AuthScheme newInstance(HttpParams params) {
return new WindowsNegotiateScheme(AuthSchemes.SPNEGO, null);
}
}
AuthSchemeRegistry authSceme = new AuthSchemeRegistry();
authSceme.register(AuthSchemes.BASIC, new BasicSchemeFactory());
authSceme.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
authSceme.register(AuthSchemes.NTLM, new WindowsNTLMSchemeFactory(null));
authSceme.register(AuthSchemes.SPNEGO, new WindowsNegotiateSchemeFactory(null));
final CredentialsProvider credsProvider = new WindowsCredentialsProvider(new SystemDefaultCredentialsProvider());
AbstractHttpClient httpClient = new SystemDefaultHttpClient();
httpClient.setAuthSchemes(authSceme);
httpClient.setCredentialsProvider(credsProvider);
RestAssured.config = RestAssured.config().httpClient(httpClientConfig().httpClientFactory(() -> httpClient));
given()
.log().all()
.when()
.get("http://httpbin.org/get")
.then()
.log().all()
.statusCode(HTTP_OK);
}
}
這與認證無關。答案只會在標題中設置信息。 – Piazzolla
@Piazzolla的問題是:如何將Windows用戶登錄到API請求頭?你想要什麼? – RocketRaccoon
是的,這是問題的最後一部分,但至少標題和問題包括「Windows身份驗證」以及有關哪個用戶的信息不是僅用於身份驗證的信息。我可以看到,我的答案可能會有點困難。 – Piazzolla
- 1. 測試HTTPS身份驗證
- 2. 在IIS中使用Windows身份驗證
- 3. 在Windows身份驗證中使用SimpleMembershiProvider
- 4. 使用Perforce API測試用戶身份驗證?
- 5. web api中的Windows身份驗證2
- 6. 如何使用Windows身份驗證加載測試網頁
- 7. 測試使用Windows身份驗證的Intranet站點
- 8. 使用Windows身份驗證的簡單身份驗證代理
- 9. 使用SoapUI和Windows身份驗證測試Web服務
- 10. Sitecore的身份驗證使用API身份驗證/登錄
- 11. 測試與Jasmine使用會話身份驗證的AJAX API?
- 12. 使用Laravel5身份驗證的Codeception功能API測試
- 13. 在ASP .NET Web Api中打開身份驗證身份驗證
- 14. 在Web API中使用Windows身份驗證的自定義RoleProvider
- 15. 如何在API控制器中測試身份驗證
- 16. Windows身份驗證和Asp.Net Web API
- 17. Windows Azure表REST Api身份驗證
- 18. 在IIS中啓用Windows身份驗證
- 19. Windows API清除身份驗證令牌
- 20. 在Django Rest Framework中使用身份驗證測試POST
- 21. 在Symfony2中使用WebTestCase測試oAuth/Facebook身份驗證
- 22. 在REST WCF Web API中使用Windows身份驗證對用戶進行身份驗證
- 23. Outlook REST API:嘗試使用AZURE AD身份驗證調用API
- 24. Windows身份驗證 - 針對不同用戶進行測試
- 25. Rails Api身份驗證與用戶身份驗證
- 26. 使用的MVC API使用Windows身份驗證從VBA
- 27. 使用窗體身份驗證cookie中緩存角色 - (Windows身份驗證)
- 28. 在Windows上使用OpenSSL通過身份驗證訪問REST API
- 29. 從使用Windows身份驗證
- 30. 使用asp.net進行Windows身份驗證
我也想找到這個。運氣好的話? – Piazzolla