Skip to content

Commit 92be713

Browse files
author
liuyongkui
committed
增信任所有https url的接口功能,并进一步优化!
1 parent 057c67d commit 92be713

File tree

2 files changed

+116
-32
lines changed

2 files changed

+116
-32
lines changed

novate/src/main/java/com/tamic/novate/Novate.java

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public final class Novate {
123123
private Map<Object, Observable<ResponseBody>> downMaps = new HashMap<Object, Observable<ResponseBody>>() {
124124
};
125125
private Observable.Transformer exceptTransformer = null;
126+
127+
public static final String KEY_CACHE = "Novate_Http_cache";
126128
private static final int DEFAULT_TIMEOUT = 15;
127129
private static final int DEFAULT_MAXIDLE_CONNECTIONS = 5;
128130
private static final long DEFAULT_KEEP_ALIVEDURATION = 8;
@@ -152,7 +154,6 @@ public final class Novate {
152154
* create ApiService
153155
*/
154156
public <T> T create(final Class<T> service) {
155-
156157
return retrofit.create(service);
157158
}
158159

@@ -181,16 +182,26 @@ public <T> T call(Observable<T> observable, ResponseCallback callback) {
181182
.subscribe(new RxSubscriber<T, ResponseBody>(observable.getClass().getSimpleName(), callback));
182183
}
183184

185+
184186
/*public <R> Observable<R> compose() {
185187
return schedulersIo(okhttpBuilder);
186188
}
187189
*/
190+
191+
/**
192+
* Thread IO
193+
* @param observable
194+
*/
188195
public <T> Observable<T> schedulersIo(Observable<T> observable) {
189196
return observable.subscribeOn(Schedulers.io())
190197
.unsubscribeOn(Schedulers.io())
191198
.observeOn(Schedulers.io());
192199
}
193200

201+
/**
202+
* Thread mainThread
203+
* @param observable
204+
*/
194205
public <T> Observable<T> schedulersMain(Observable<T> observable) {
195206
return observable.subscribeOn(Schedulers.io())
196207
.unsubscribeOn(Schedulers.io())
@@ -211,6 +222,13 @@ public <T> T execute(NovateRequest request, BaseSubscriber<T> subscriber) {
211222
return call(request, subscriber);
212223
}
213224

225+
/**
226+
* call NovateRequest
227+
* @param request
228+
* @param subscriber
229+
* @param <T>
230+
* @return
231+
*/
214232
private <T> T call(NovateRequest request, BaseSubscriber<T> subscriber) {
215233
return (T) createRx(request)
216234
.compose(schedulersTransformer)
@@ -1574,7 +1592,7 @@ public static final class Builder {
15741592
private int writeTimeout = DEFAULT_TIMEOUT;
15751593
private int readTimeout = DEFAULT_TIMEOUT;
15761594
private int default_maxidle_connections = DEFAULT_MAXIDLE_CONNECTIONS;
1577-
private long default_keep_aliveduration = DEFAULT_MAXIDLE_CONNECTIONS;
1595+
private long default_keep_aliveduration = DEFAULT_KEEP_ALIVEDURATION;
15781596
private long cacheMaxSize = DEFAULT_CACHEMAXSIZE;
15791597
private int cacheTimeout = DEFAULT_MAX_STALE;
15801598
private okhttp3.Call.Factory callFactory;
@@ -1583,6 +1601,7 @@ public static final class Builder {
15831601
private Object tag;
15841602
private Boolean isCookie = false;
15851603
private Boolean isCache = true;
1604+
private Boolean isSkip = false;
15861605
private List<InputStream> certificateList;
15871606
private HostnameVerifier hostnameVerifier;
15881607
private CertificatePinner certificatePinner;
@@ -1887,18 +1906,35 @@ public Builder cookieManager(NovateCookieManager cookie) {
18871906
}
18881907

18891908
/**
1890-
*
1909+
*skipSSLSocketFactory
1910+
*/
1911+
public Builder skipSSLSocketFactory(boolean isSkip) {
1912+
this.isSkip = isSkip;
1913+
return this;
1914+
}
1915+
1916+
/**
1917+
* addSSLSocketFactory
18911918
*/
18921919
public Builder addSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
1920+
if (sslSocketFactory == null) throw new NullPointerException("sslSocketFactory == null");
18931921
this.sslSocketFactory = sslSocketFactory;
18941922
return this;
18951923
}
18961924

1925+
/**
1926+
* HostnameVerifier
1927+
* @param hostnameVerifier
1928+
* @return Builder
1929+
*/
18971930
public Builder addHostnameVerifier(HostnameVerifier hostnameVerifier) {
18981931
this.hostnameVerifier = hostnameVerifier;
18991932
return this;
19001933
}
19011934

1935+
/**
1936+
* addCertificatePinner
1937+
*/
19021938
public Builder addCertificatePinner(CertificatePinner certificatePinner) {
19031939
this.certificatePinner = certificatePinner;
19041940
return this;
@@ -1922,6 +1958,7 @@ public Builder addSSL(String[] hosts, int[] certificates) {
19221958
}
19231959

19241960
public Builder addNetworkInterceptor(Interceptor interceptor) {
1961+
if (interceptor == null) throw new NullPointerException("interceptor == null");
19251962
okhttpBuilder.addNetworkInterceptor(interceptor);
19261963
return this;
19271964
}
@@ -2023,17 +2060,23 @@ public Novate build() {
20232060
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
20242061
}
20252062

2026-
if (sslSocketFactory != null) {
2063+
if (isSkip) {
2064+
okhttpBuilder.sslSocketFactory(NovateHttpsFactroy.getSSLSocketFactory(),
2065+
NovateHttpsFactroy.creatX509TrustManager());
2066+
2067+
okhttpBuilder.hostnameVerifier(NovateHttpsFactroy.creatSkipHostnameVerifier());
2068+
}
2069+
2070+
if (!isSkip && sslSocketFactory != null) {
20272071
okhttpBuilder.sslSocketFactory(sslSocketFactory);
20282072
}
20292073

20302074
if (hostnameVerifier != null) {
20312075
okhttpBuilder.hostnameVerifier(hostnameVerifier);
20322076
}
20332077

2034-
20352078
if (httpCacheDirectory == null) {
2036-
httpCacheDirectory = new File(mContext.getCacheDir(), "Novate_Http_cache");
2079+
httpCacheDirectory = new File(mContext.getCacheDir(), KEY_CACHE);
20372080
}
20382081

20392082
if (isCache) {
@@ -2077,7 +2120,7 @@ public Novate build() {
20772120
* <p>If unset, a new connection pool will be used.
20782121
*/
20792122
if (connectionPool == null) {
2080-
connectionPool = new ConnectionPool(default_maxidle_connections, default_maxidle_connections, TimeUnit.SECONDS);
2123+
connectionPool = new ConnectionPool(default_maxidle_connections, default_keep_aliveduration, TimeUnit.SECONDS);
20812124
}
20822125
okhttpBuilder.connectionPool(connectionPool);
20832126

@@ -2102,6 +2145,11 @@ public Novate build() {
21022145
}
21032146

21042147
if (isCookie) {
2148+
/**
2149+
* Returns a modifiable list of interceptors that observe a single network request and response.
2150+
* These interceptors must call {@link Interceptor.Chain#proceed} exactly once: it is an error
2151+
* for a network interceptor to short-circuit or repeat a network request.
2152+
*/
21052153
okhttpBuilder.addInterceptor(new ReceivedCookiesInterceptor(context));
21062154
okhttpBuilder.addInterceptor(new AddCookiesInterceptor(context, ""));
21072155
}
@@ -2187,16 +2235,16 @@ public Observable<?> call(Throwable throwable) {
21872235
@Deprecated
21882236
public interface ResponseCallBack<T> {
21892237

2190-
public void onStart();
2238+
void onStart();
21912239

2192-
public void onCompleted();
2240+
void onCompleted();
21932241

2194-
public abstract void onError(Throwable e);
2242+
void onError(Throwable e);
21952243

21962244
@Deprecated
2197-
public abstract void onSuccee(NovateResponse<T> response);
2245+
void onSuccee(NovateResponse<T> response);
21982246

2199-
public void onsuccess(int code, String msg, T response, String originalResponse);
2247+
void onsuccess(int code, String msg, T response, String originalResponse);
22002248

22012249
}
22022250
}

novate/src/main/java/com/tamic/novate/NovateHttpsFactroy.java

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.security.cert.CertificateException;
3333
import java.security.cert.CertificateFactory;
3434
import java.security.cert.X509Certificate;
35+
import java.util.Arrays;
3536

3637
import javax.net.ssl.HostnameVerifier;
3738
import javax.net.ssl.SSLContext;
@@ -98,7 +99,7 @@ protected static SSLSocketFactory getSSLSocketFactory(Context context, int[] cer
9899
* set HostnameVerifier
99100
* {@link HostnameVerifier}
100101
*/
101-
protected static HostnameVerifier getHostnameVerifier(final String[] hostUrls) {
102+
public static HostnameVerifier getHostnameVerifier(final String[] hostUrls) {
102103

103104
HostnameVerifier TRUSTED_VERIFIER = new HostnameVerifier() {
104105

@@ -112,7 +113,6 @@ public boolean verify(String hostname, SSLSession session) {
112113
return ret;
113114
}
114115
};
115-
116116
return TRUSTED_VERIFIER;
117117
}
118118

@@ -138,6 +138,16 @@ protected static String getPins(Context context, int certificate) {
138138

139139
}
140140

141+
public static SSLSocketFactory getSSLSocketFactory() {
142+
try {
143+
SSLContext sslContext = SSLContext.getInstance("SSL");
144+
sslContext.init(null, creatTrustManager(), new SecureRandom());
145+
return sslContext.getSocketFactory();
146+
} catch (Exception e) {
147+
throw new RuntimeException(e);
148+
}
149+
}
150+
141151
/**
142152
* get SSLSocketFactory
143153
*
@@ -181,6 +191,7 @@ public static SSLSocketFactory creatSSLSocketFactory(Context context, String nam
181191

182192
/**
183193
* get SkipHostnameVerifier
194+
*
184195
* @return
185196
*/
186197
public static HostnameVerifier creatSkipHostnameVerifier() {
@@ -195,24 +206,49 @@ public boolean verify(String s, SSLSession sslSession) {
195206

196207

197208
/**
198-
* TrustManager
209+
* X509TrustManager
199210
*/
200-
private static TrustManager[] creatTrustManager() {
201-
TrustManager[] trustAllCerts = new TrustManager[]{
202-
new X509TrustManager() {
203-
@Override
204-
public void checkClientTrusted(X509Certificate[] chain, String authType) {
205-
}
206-
@Override
207-
public void checkServerTrusted(X509Certificate[] chain, String authType) {
208-
}
209-
210-
@Override
211-
public X509Certificate[] getAcceptedIssuers() {
212-
return new X509Certificate[]{};
213-
}
214-
}
215-
};
216-
return trustAllCerts;
211+
public static X509TrustManager creatX509TrustManager() {
212+
213+
return new X509TrustManager(){
214+
@Override
215+
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
216+
217+
}
218+
219+
@Override
220+
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
221+
222+
}
223+
224+
@Override
225+
public X509Certificate[] getAcceptedIssuers() {
226+
return new X509Certificate[0];
217227
}
228+
};
229+
230+
}
231+
232+
/**
233+
* TrustManager
234+
*/
235+
public static TrustManager[] creatTrustManager() {
236+
TrustManager[] trustAllCerts = new TrustManager[]{
237+
new X509TrustManager() {
238+
@Override
239+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
240+
}
241+
242+
@Override
243+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
244+
}
245+
246+
@Override
247+
public X509Certificate[] getAcceptedIssuers() {
248+
return new X509Certificate[]{};
249+
}
250+
}
251+
};
252+
return trustAllCerts;
253+
}
218254
}

0 commit comments

Comments
 (0)