Skip to content

Commit 0d418d7

Browse files
author
Javen
committed
Fix proxy authentication failure - add Authenticator method
1 parent e2ca7f4 commit 0d418d7

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/cn/jpush/api/common/HttpProxy.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9+
import com.google.common.base.Preconditions;
10+
911
public class HttpProxy {
1012
private static final Logger LOG = LoggerFactory.getLogger(NativeHttpClient.class);
1113

@@ -24,6 +26,9 @@ public HttpProxy(String host, int port) {
2426
public HttpProxy(String host, int port, String username, String password) {
2527
this(host, port);
2628

29+
Preconditions.checkArgument(! (null == username), "username should not be null");
30+
Preconditions.checkArgument(! (null == password), "password should not be null");
31+
2732
this.username = username;
2833
this.password = password;
2934
authenticationNeeded = true;
@@ -44,4 +49,12 @@ public boolean isAuthenticationNeeded() {
4449
public String getProxyAuthorization() {
4550
return ServiceHelper.getBasicAuthorization(username, password);
4651
}
52+
53+
public String getUsername() {
54+
return this.username;
55+
}
56+
57+
public String getPassword() {
58+
return this.password;
59+
}
4760
}

src/cn/jpush/api/common/NativeHttpClient.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import java.io.InputStream;
55
import java.io.InputStreamReader;
66
import java.io.OutputStream;
7+
import java.net.Authenticator;
78
import java.net.HttpURLConnection;
9+
import java.net.PasswordAuthentication;
810
import java.net.SocketTimeoutException;
911
import java.net.URL;
1012
import java.security.cert.CertificateException;
@@ -96,6 +98,8 @@ private ResponseWrapper _sendRequest(String url, String content,
9698
conn = (HttpURLConnection) aUrl.openConnection(_proxy.getNetProxy());
9799
if (_proxy.isAuthenticationNeeded()) {
98100
conn.setRequestProperty("Proxy-Authorization", _proxy.getProxyAuthorization());
101+
Authenticator.setDefault(new SimpleProxyAuthenticator(
102+
_proxy.getUsername(), _proxy.getPassword()));
99103
}
100104
} else {
101105
conn = (HttpURLConnection) aUrl.openConnection();
@@ -234,7 +238,7 @@ protected void initSSL() {
234238
}
235239

236240

237-
public static class SimpleHostnameVerifier implements HostnameVerifier {
241+
private static class SimpleHostnameVerifier implements HostnameVerifier {
238242

239243
@Override
240244
public boolean verify(String hostname, SSLSession session) {
@@ -243,7 +247,7 @@ public boolean verify(String hostname, SSLSession session) {
243247

244248
}
245249

246-
public static class SimpleTrustManager implements TrustManager, X509TrustManager {
250+
private static class SimpleTrustManager implements TrustManager, X509TrustManager {
247251

248252
@Override
249253
public void checkClientTrusted(X509Certificate[] chain, String authType)
@@ -262,5 +266,20 @@ public X509Certificate[] getAcceptedIssuers() {
262266
return null;
263267
}
264268
}
269+
270+
private static class SimpleProxyAuthenticator extends java.net.Authenticator {
271+
private String username;
272+
private String password;
265273

274+
public SimpleProxyAuthenticator(String username, String password) {
275+
this.username = username;
276+
this.password = password;
277+
}
278+
279+
protected PasswordAuthentication getPasswordAuthentication() {
280+
return new PasswordAuthentication(
281+
this.username,
282+
this.password.toCharArray());
283+
}
284+
}
266285
}

src/cn/jpush/api/push/PushClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ public PushResult sendPush(String payloadString) throws APIConnectionException,
111111
try {
112112
_jsonParser.parse(payloadString);
113113
} catch (JsonParseException e) {
114-
e.printStackTrace();
115114
Preconditions.checkArgument(false, "payloadString should be a valid JSON string.");
116115
}
117116

test/cn/jpush/api/push/PushClientTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import cn.jpush.api.common.APIConnectionException;
66
import cn.jpush.api.common.APIRequestException;
7+
import cn.jpush.api.common.HttpProxy;
78

89
public class PushClientTest {
910
private static final String appKey ="dd1066407b044738b6479275";
@@ -35,6 +36,11 @@ public void test_empty_string() {
3536
}
3637
}
3738

39+
@Test(expected = IllegalArgumentException.class)
40+
public void test_empty_password() {
41+
new HttpProxy("127.0.0.1", 8080, "", null);
42+
}
43+
3844

3945

4046

0 commit comments

Comments
 (0)