Search in sources :

Example 41 with Authenticator

use of java.net.Authenticator in project td-client-java by treasure-data.

the class TestProxyAccess method proxyServerTest.

@Test
public void proxyServerTest() throws Exception {
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxyPort));
    Authenticator auth = new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(PROXY_USER, PROXY_PASS.toCharArray());
        }
    };
    String disabledSchemesProperty = "jdk.http.auth.tunneling.disabledSchemes";
    try {
        System.setProperty(disabledSchemesProperty, "");
        Authenticator.setDefault(auth);
        try (InputStream in = new URL("https://api.treasuredata.com/v3/system/server_status").openConnection(proxy).getInputStream()) {
            String ret = CharStreams.toString(new InputStreamReader(in));
            logger.info(ret);
        }
        assertEquals(1, proxyAccessCount.get());
    } finally {
        System.clearProperty(disabledSchemesProperty);
        Authenticator.setDefault(null);
    }
}
Also used : Proxy(java.net.Proxy) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 42 with Authenticator

use of java.net.Authenticator in project ripme by RipMeApp.

the class Proxy method setSocks.

/**
 * Set a Socks Proxy Server (globally).
 *
 * @param fullsocks the socks server, using format [user:password]@host[:port]
 */
public static void setSocks(String fullsocks) {
    Map<String, String> socksServer = parseServer(fullsocks);
    if (socksServer.get("user") != null && socksServer.get("password") != null) {
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication p = new PasswordAuthentication(socksServer.get("user"), socksServer.get("password").toCharArray());
                return p;
            }
        });
        System.setProperty("java.net.socks.username", socksServer.get("user"));
        System.setProperty("java.net.socks.password", socksServer.get("password"));
    }
    if (socksServer.get("port") != null) {
        System.setProperty("socksProxyPort", socksServer.get("port"));
    }
    System.setProperty("socksProxyHost", socksServer.get("server"));
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 43 with Authenticator

use of java.net.Authenticator in project ripme by RipMeApp.

the class Proxy method setHTTPProxy.

/**
 * Set a HTTP Proxy.
 * WARNING: Authenticated HTTP Proxy won't work from jdk1.8.111 unless
 * passing the flag -Djdk.http.auth.tunneling.disabledSchemes="" to java
 * see https://stackoverflow.com/q/41505219
 *
 * @param fullproxy the proxy, using format [user:password]@host[:port]
 */
public static void setHTTPProxy(String fullproxy) {
    Map<String, String> proxyServer = parseServer(fullproxy);
    if (proxyServer.get("user") != null && proxyServer.get("password") != null) {
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication p = new PasswordAuthentication(proxyServer.get("user"), proxyServer.get("password").toCharArray());
                return p;
            }
        });
        System.setProperty("http.proxyUser", proxyServer.get("user"));
        System.setProperty("http.proxyPassword", proxyServer.get("password"));
        System.setProperty("https.proxyUser", proxyServer.get("user"));
        System.setProperty("https.proxyPassword", proxyServer.get("password"));
    }
    if (proxyServer.get("port") != null) {
        System.setProperty("http.proxyPort", proxyServer.get("port"));
        System.setProperty("https.proxyPort", proxyServer.get("port"));
    }
    System.setProperty("http.proxyHost", proxyServer.get("server"));
    System.setProperty("https.proxyHost", proxyServer.get("server"));
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 44 with Authenticator

use of java.net.Authenticator in project jdk8u_jdk by JetBrains.

the class HttpsProxyStackOverflow method startServer.

static BadAuthProxyServer startServer() throws IOException {
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("xyz", "xyz".toCharArray());
        }
    });
    BadAuthProxyServer server = new BadAuthProxyServer(new ServerSocket(0));
    Thread serverThread = new Thread(server);
    serverThread.start();
    return server;
}
Also used : ServerSocket(java.net.ServerSocket) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 45 with Authenticator

use of java.net.Authenticator in project tdi-studio-se by Talend.

the class SforceBasicBulkConnection method setProxyToConnection.

private void setProxyToConnection(ConnectorConfig conn) {
    Proxy socketProxy = null;
    if (!useProxy) {
        proxyHost = System.getProperty("https.proxyHost");
        if (proxyHost != null && System.getProperty("https.proxyPort") != null) {
            proxyPort = Integer.parseInt(System.getProperty("https.proxyPort"));
            proxyUsername = System.getProperty("https.proxyUser");
            proxyPassword = System.getProperty("https.proxyPassword");
            useProxy = true;
        } else {
            proxyHost = System.getProperty("http.proxyHost");
            if (proxyHost != null && System.getProperty("http.proxyPort") != null) {
                proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
                proxyUsername = System.getProperty("http.proxyUser");
                proxyPassword = System.getProperty("http.proxyPassword");
                useProxy = true;
            } else {
                proxyHost = System.getProperty("socksProxyHost");
                if (proxyHost != null && System.getProperty("socksProxyPort") != null) {
                    proxyPort = Integer.parseInt(System.getProperty("socksProxyPort"));
                    proxyUsername = System.getProperty("java.net.socks.username");
                    proxyPassword = System.getProperty("java.net.socks.password");
                    useProxy = true;
                    SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
                    socketProxy = new Proxy(Proxy.Type.SOCKS, addr);
                }
            }
        }
    }
    if (useProxy) {
        if (socketProxy != null) {
            conn.setProxy(socketProxy);
        } else {
            conn.setProxy(proxyHost, proxyPort);
        }
        if (proxyUsername != null && !"".equals(proxyUsername)) {
            conn.setProxyUsername(proxyUsername);
            if (proxyPassword != null && !"".equals(proxyPassword)) {
                conn.setProxyPassword(proxyPassword);
                Authenticator.setDefault(new Authenticator() {

                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType() == Authenticator.RequestorType.PROXY) {
                            return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
                        } else {
                            return super.getPasswordAuthentication();
                        }
                    }
                });
            }
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Authenticator (java.net.Authenticator)83 PasswordAuthentication (java.net.PasswordAuthentication)52 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)8 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ServerSocket (java.net.ServerSocket)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3