org.apache.commons.httpclient.NTCredentials Java Examples

The following examples show how to use org.apache.commons.httpclient.NTCredentials. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CredentialsUtils.java    From httpclientAuthHelper with Apache License 2.0 7 votes vote down vote up
public static void setNTLMCredentials(HttpClient httpClient, UsernamePasswordCredentials credentials,
                                      String domain) {
    initNTLMv2();

    String localHostName;
    try {
        localHostName = Inet4Address.getLocalHost().getHostName();
    } catch (Exception e) {
        localHostName = "";
    }

    AuthScope authscope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
    httpClient.getState().setCredentials(
            authscope,
            new NTCredentials(
                    credentials.getUserName(),
                    credentials.getPassword(),
                    localHostName, domain));
}
 
Example #2
Source File: NTLMScheme.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a NTLM authorization string for the given
 * challenge and NT credentials.
 *
 * @param challenge The challenge.
 * @param credentials {@link NTCredentials}
 *
 * @return a ntlm authorization string
 * @throws AuthenticationException is thrown if authentication fails
 * 
 * @deprecated Use non-static {@link #authenticate(Credentials, HttpMethod)}
 */
public static String authenticate(
 final NTCredentials credentials, final String challenge) 
  throws AuthenticationException {

    LOG.trace("enter NTLMScheme.authenticate(NTCredentials, String)");

    if (credentials == null) {
        throw new IllegalArgumentException("Credentials may not be null");
    }
    
    NTLM ntlm = new NTLM();
    String s = ntlm.getResponseFor(challenge,
    credentials.getUserName(), credentials.getPassword(),
    credentials.getHost(), credentials.getDomain());
    return "NTLM " + s;
}
 
Example #3
Source File: NTLMScheme.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a NTLM authorization string for the given
 * challenge and NT credentials.
 *
 * @param challenge The challenge.
 * @param credentials {@link NTCredentials}
 * @param charset The charset to use for encoding the credentials
 *
 * @return a ntlm authorization string
 * @throws AuthenticationException is thrown if authentication fails
 * 
 * @deprecated Use non-static {@link #authenticate(Credentials, HttpMethod)}
 * 
 * @since 3.0
 */
public static String authenticate(
    final NTCredentials credentials, 
    final String challenge,
    String charset
) throws AuthenticationException {

    LOG.trace("enter NTLMScheme.authenticate(NTCredentials, String)");

    if (credentials == null) {
        throw new IllegalArgumentException("Credentials may not be null");
    }
    
    NTLM ntlm = new NTLM();
    ntlm.setCredentialCharset(charset);
    String s = ntlm.getResponseFor(
        challenge,
        credentials.getUserName(), 
        credentials.getPassword(),
        credentials.getHost(), 
        credentials.getDomain());
    return "NTLM " + s;
}
 
Example #4
Source File: TlsTunnelBuilder.java    From jframe with Apache License 2.0 6 votes vote down vote up
private Socket AuthenticateProxy(ConnectMethod method, ProxyClient client, 
        String proxyHost, int proxyPort, 
        String proxyUsername, String proxyPassword) throws IOException {   
    if(method.getProxyAuthState().getAuthScheme().getSchemeName().equalsIgnoreCase("ntlm")) {
        // If Auth scheme is NTLM, set NT credentials with blank host and domain name
        client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), 
                        new NTCredentials(proxyUsername, proxyPassword,"",""));
    } else {
        // If Auth scheme is Basic/Digest, set regular Credentials
        client.getState().setProxyCredentials(new AuthScope(proxyHost, proxyPort), 
                new UsernamePasswordCredentials(proxyUsername, proxyPassword));
    }
    
    ProxyClient.ConnectResponse response = client.connect();
    Socket socket = response.getSocket();
    
    if (socket == null) {
        method = response.getConnectMethod();
        throw new ProtocolException("Proxy Authentication failed. Socket not created: " 
                + method.getStatusLine());
    }
    return socket;
}
 
Example #5
Source File: Manager.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
private void setpUpHttpClient() {
    if (httpclient != null) {
        // add the proxy setting is required
        if (this.isUseProxy()) {
            httpclient
                    .getHostConfiguration()
                    .setProxy(this.getProxyHost(), this.getProxyPort());
            if (this.isUseProxyAuth()) {
                httpclient
                        .getState()
                        .setProxyCredentials(
                                this.getProxyRealm(),
                                this.getProxyHost(),
                                new UsernamePasswordCredentials(
                                        this.getProxyUsername(), this.getProxyPassword()));
            }
        }

        httpclient
                .getHttpConnectionManager()
                .getParams()
                .setConnectionTimeout(Config.connectionTimeout * 1000);
        httpclient.setState(initialState);
        httpclient.getParams().setParameter("http.useragent", Config.userAgent);

        /*
         * Code to deal with http auth
         *
         */

        if (useHTTPauth) {
            // Credentials creds = new Credentials();
            // creds.
            NTCredentials ntCreds =
                    new NTCredentials(this.userName, this.password, "", this.realmDomain);
            httpclient.getState().setCredentials(AuthScope.ANY, ntCreds);
        }

        /*
         * Custom code to add ntlm auth
         */

        // NTCredentials ntCreds = new NTCredentials("username", "password", "", "");
        // httpclient.getState().setCredentials(AuthScope.ANY, ntCreds);
    }
}
 
Example #6
Source File: Http.java    From anthelion with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the HTTP client
 */
private void configureClient() {

  // Set up an HTTPS socket factory that accepts self-signed certs.
  Protocol https = new Protocol("https",
      new DummySSLProtocolSocketFactory(), 443);
  Protocol.registerProtocol("https", https);

  HttpConnectionManagerParams params = connectionManager.getParams();
  params.setConnectionTimeout(timeout);
  params.setSoTimeout(timeout);
  params.setSendBufferSize(BUFFER_SIZE);
  params.setReceiveBufferSize(BUFFER_SIZE);
  params.setMaxTotalConnections(maxThreadsTotal);

  // executeMethod(HttpMethod) seems to ignore the connection timeout on the connection manager.
  // set it explicitly on the HttpClient.
  client.getParams().setConnectionManagerTimeout(timeout);

  HostConfiguration hostConf = client.getHostConfiguration();
  ArrayList headers = new ArrayList();
  // Set the User Agent in the header
  headers.add(new Header("User-Agent", userAgent));
  // prefer English
  headers.add(new Header("Accept-Language", acceptLanguage));
  // prefer UTF-8
  headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
  // prefer understandable formats
  headers.add(new Header("Accept",
          "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
  // accept gzipped content
  headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
  hostConf.getParams().setParameter("http.default-headers", headers);

  // HTTP proxy server details
  if (useProxy) {
    hostConf.setProxy(proxyHost, proxyPort);

    if (proxyUsername.length() > 0) {

      AuthScope proxyAuthScope = getAuthScope(
          this.proxyHost, this.proxyPort, this.proxyRealm);

      NTCredentials proxyCredentials = new NTCredentials(
          this.proxyUsername, this.proxyPassword,
          this.agentHost, this.proxyRealm);

      client.getState().setProxyCredentials(
          proxyAuthScope, proxyCredentials);
    }
  }

}
 
Example #7
Source File: Http.java    From anthelion with Apache License 2.0 4 votes vote down vote up
/**
 * If credentials for the authentication scope determined from the
 * specified <code>url</code> is not already set in the HTTP client,
 * then this method sets the default credentials to fetch the
 * specified <code>url</code>. If credentials are found for the
 * authentication scope, the method returns without altering the
 * client.
 *
 * @param url URL to be fetched
 */
private void resolveCredentials(URL url) {

  if (defaultUsername != null && defaultUsername.length() > 0) {

    int port = url.getPort();
    if (port == -1) {
      if ("https".equals(url.getProtocol()))
        port = 443;
      else
        port = 80;
    }

    AuthScope scope = new AuthScope(url.getHost(), port);

    if (client.getState().getCredentials(scope) != null) {
      if (LOG.isTraceEnabled())
        LOG.trace("Pre-configured credentials with scope - host: "
            + url.getHost() + "; port: " + port
            + "; found for url: " + url);

      // Credentials are already configured, so do nothing and return
      return;
    }

    if (LOG.isTraceEnabled())
        LOG.trace("Pre-configured credentials with scope -  host: "
            + url.getHost() + "; port: " + port
            + "; not found for url: " + url);

    AuthScope serverAuthScope = getAuthScope(
        url.getHost(), port, defaultRealm, defaultScheme);

    NTCredentials serverCredentials = new NTCredentials(
        defaultUsername, defaultPassword,
        agentHost, defaultRealm);

    client.getState().setCredentials(
        serverAuthScope, serverCredentials);
  }
}
 
Example #8
Source File: Http.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the HTTP client
 */
private void configureClient() {

  // Set up an HTTPS socket factory that accepts self-signed certs.
  ProtocolSocketFactory factory = new SSLProtocolSocketFactory();
  Protocol https = new Protocol("https", factory, 443);
  Protocol.registerProtocol("https", https);

  HttpConnectionManagerParams params = connectionManager.getParams();
  params.setConnectionTimeout(timeout);
  params.setSoTimeout(timeout);
  params.setSendBufferSize(BUFFER_SIZE);
  params.setReceiveBufferSize(BUFFER_SIZE);
  params.setMaxTotalConnections(maxThreadsTotal);

  // executeMethod(HttpMethod) seems to ignore the connection timeout on the connection manager.
  // set it explicitly on the HttpClient.
  client.getParams().setConnectionManagerTimeout(timeout);

  HostConfiguration hostConf = client.getHostConfiguration();
  ArrayList<Header> headers = new ArrayList<Header>();
  // Set the User Agent in the header
  headers.add(new Header("User-Agent", userAgent));
  // prefer English
  headers.add(new Header("Accept-Language", acceptLanguage));
  // prefer UTF-8
  headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
  // prefer understandable formats
  headers.add(new Header("Accept",
          "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
  // accept gzipped content
  headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
  hostConf.getParams().setParameter("http.default-headers", headers);

  // HTTP proxy server details
  if (useProxy) {
    hostConf.setProxy(proxyHost, proxyPort);

    if (proxyUsername.length() > 0) {

      AuthScope proxyAuthScope = getAuthScope(
          this.proxyHost, this.proxyPort, this.proxyRealm);

      NTCredentials proxyCredentials = new NTCredentials(
          this.proxyUsername, this.proxyPassword,
          Http.agentHost, this.proxyRealm);

      client.getState().setProxyCredentials(
          proxyAuthScope, proxyCredentials);
    }
  }

}
 
Example #9
Source File: Http.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * If credentials for the authentication scope determined from the
 * specified <code>url</code> is not already set in the HTTP client,
 * then this method sets the default credentials to fetch the
 * specified <code>url</code>. If credentials are found for the
 * authentication scope, the method returns without altering the
 * client.
 *
 * @param url URL to be fetched
 */
private void resolveCredentials(URL url) {

  if (defaultUsername != null && defaultUsername.length() > 0) {

    int port = url.getPort();
    if (port == -1) {
      if ("https".equals(url.getProtocol()))
        port = 443;
      else
        port = 80;
    }

    AuthScope scope = new AuthScope(url.getHost(), port);

    if (client.getState().getCredentials(scope) != null) {
      if (LOG.isTraceEnabled())
        LOG.trace("Pre-configured credentials with scope - host: "
            + url.getHost() + "; port: " + port
            + "; found for url: " + url);

      // Credentials are already configured, so do nothing and return
      return;
    }

    if (LOG.isTraceEnabled())
        LOG.trace("Pre-configured credentials with scope -  host: "
            + url.getHost() + "; port: " + port
            + "; not found for url: " + url);

    AuthScope serverAuthScope = getAuthScope(
        url.getHost(), port, defaultRealm, defaultScheme);

    NTCredentials serverCredentials = new NTCredentials(
        defaultUsername, defaultPassword,
        agentHost, defaultRealm);

    client.getState().setCredentials(
        serverAuthScope, serverCredentials);
  }
}