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);
}
}
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"));
}
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"));
}
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;
}
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();
}
}
});
}
}
}
}
Aggregations