2017-05-04 23 views

回答

0

已经解决了我的问题 因此,目前很难做到这一点,因为您需要为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); 
    } 

} 
+0

这与认证无关。答案只会在标题中设置信息。 – Piazzolla

+0

@Piazzolla的问题是:如何将Windows用户登录到API请求头?你想要什么? – RocketRaccoon

+0

是的,这是问题的最后一部分,但至少标题和问题包括“Windows身份验证”以及有关哪个用户的信息不是仅用于身份验证的信息。我可以看到,我的答案可能会有点困难。 – Piazzolla

相关问题