栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用HttpsURLConnection时如何覆盖Android发送到服务器的密码列表?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用HttpsURLConnection时如何覆盖Android发送到服务器的密码列表?

这段代码有点原始。请谨慎使用。

public class PreferredCipherSuiteSSLSocketFactory extends SSLSocketFactory {private static final String PREFERRED_CIPHER_SUITE = "TLS_RSA_WITH_AES_128_CBC_SHA";private final SSLSocketFactory delegate;public PreferredCipherSuiteSSLSocketFactory(SSLSocketFactory delegate) {    this.delegate = delegate;}@Overridepublic String[] getDefaultCipherSuites() {    return setupPreferredDefaultCipherSuites(this.delegate);}@Overridepublic String[] getSupportedCipherSuites() {    return setupPreferredSupportedCipherSuites(this.delegate);}@Overridepublic Socket createSocket(String arg0, int arg1) throws IOException,        UnknownHostException {    Socket socket = this.delegate.createSocket(arg0, arg1);    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);    return socket;}@Overridepublic Socket createSocket(InetAddress arg0, int arg1) throws IOException {    Socket socket = this.delegate.createSocket(arg0, arg1);    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);    return socket;}@Overridepublic Socket createSocket(Socket arg0, String arg1, int arg2, boolean arg3)        throws IOException {    Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);    return socket;}@Overridepublic Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3)        throws IOException, UnknownHostException {    Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);    return socket;}@Overridepublic Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2,        int arg3) throws IOException {    Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);    return socket;}private static String[] setupPreferredDefaultCipherSuites(SSLSocketFactory sslSocketFactory) {    String[] defaultCipherSuites = sslSocketFactory.getDefaultCipherSuites();    ArrayList<String> suitesList = new ArrayList<String>(Arrays.asList(defaultCipherSuites));    suitesList.remove(PREFERRED_CIPHER_SUITE);    suitesList.add(0, PREFERRED_CIPHER_SUITE);    return suitesList.toArray(new String[suitesList.size()]);}private static String[] setupPreferredSupportedCipherSuites(SSLSocketFactory sslSocketFactory) {    String[] supportedCipherSuites = sslSocketFactory.getSupportedCipherSuites();    ArrayList<String> suitesList = new ArrayList<String>(Arrays.asList(supportedCipherSuites));    suitesList.remove(PREFERRED_CIPHER_SUITE);    suitesList.add(0, PREFERRED_CIPHER_SUITE);    return suitesList.toArray(new String[suitesList.size()]);}}

当您想使用它时。

 HttpsURLConnection connection = (HttpsURLConnection) (new URL(url))     .openConnection();        SSLContext context = SSLContext.getInstance("TLS");        TrustManager tm[] = {new SSLPinningTrustManager()};        context.init(null, tm, null);        SSLSocketFactory preferredCipherSuiteSSLSocketFactory = new PreferredCipherSuiteSSLSocketFactory(context.getSocketFactory());        connection.setSSLSocketFactory(preferredCipherSuiteSSLSocketFactory);         connection.connect();

谢谢。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号