Java 在io.restassured api测试中使用windows身份验证
我们正在尝试测试一个使用Windows身份验证的api。我正在使用rest-assured包来测试api。有没有关于如何将windows登录用户分配到api请求头的建议?已经重新连接了我的答案 因此,目前很难做到这一点,因为您需要配置HTTPClient以确保安全,但它只支持不推荐的Java 在io.restassured api测试中使用windows身份验证,java,rest-assured,Java,Rest Assured,我们正在尝试测试一个使用Windows身份验证的api。我正在使用rest-assured包来测试api。有没有关于如何将windows登录用户分配到api请求头的建议?已经重新连接了我的答案 因此,目前很难做到这一点,因为您需要配置HTTPClient以确保安全,但它只支持不推荐的AbstractHttpClient。这是我的实现。但我没有能力测试它 import io.restassured.RestAssured; import org.apache.http.annotation.Con
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);
}
}
我把我的答案改写了
因此,目前很难做到这一点,因为您需要配置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问题是:如何将windows登录用户分配到api请求标题?你到底想要什么?是的,这是问题的最后一部分,但至少标题和问题包括“windows身份验证”和关于它是哪个用户的信息不仅仅是身份验证信息。我可以看出我可能对你的答案有点难。将用户和密码作为基本身份验证头传递难道还不够吗?像
given().auth().basic(“用户名”、“密码”)
就可以了!(看起来很有希望)这与身份验证无关。答案只会在标题中设置信息。@Piazzolla问题是:如何将windows登录用户分配到api请求标题?你到底想要什么?是的,这是问题的最后一部分,但至少标题和问题包括“windows身份验证”和关于它是哪个用户的信息不仅仅是身份验证信息。我可以看出我可能对你的答案有点难。将用户和密码作为基本身份验证头传递难道还不够吗?像given().auth().basic(“用户名”、“密码”)
就可以了!(看起来很有希望)