javax.net.ssl.SSLException: Received fatal alert: protocol_version解决

这篇具有很好参考价值的文章主要介绍了javax.net.ssl.SSLException: Received fatal alert: protocol_version解决。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

今天在开发过程中,调用一个https的接口引发错误

javax.net.ssl.SSLException: Received fatal alert: protocol_version解决

在Java 1.8上,默认TLS协议是v1.2。在Java 1.6和1.7上,默认是已废弃的TLS1.0,由于此项目使用的是jdk1.6,因此引发错误。

解决方法1:

在发起请求前面设置TLSv1.2协议

	System.setProperty("https.protocols", "TLSv1.2");

解决方法2:

在发起请求前忽略ssl认证:

工具类:


import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
 
public class SslUtils {
 
    private static void trustAllHttpsCertificates() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[1];
        TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }
 
    static class miTM implements TrustManager,X509TrustManager {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
 
        public boolean isServerTrusted(X509Certificate[] certs) {
            return true;
        }
 
        public boolean isClientTrusted(X509Certificate[] certs) {
            return true;
        }
 
        public void checkServerTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
 
        public void checkClientTrusted(X509Certificate[] certs, String authType)
                throws CertificateException {
            return;
        }
    }
     
    /**
     * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
     * @throws Exception
     */
    public static void ignoreSsl() throws Exception{
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    }
}

使用方法:

public String doHttpGetUTF8(String postUrl,String token) {
		OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
        	URL realUrl = new URL(postUrl);
        	if("https".equalsIgnoreCase(realUrl.getProtocol())){
        	   SslUtils.ignoreSsl();
        	}
        	//打开 url之间的连接
        	HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
        	//设置通用的请求属性
        	conn.setRequestMethod("GET");
        	conn.setUseCaches(false);
        	conn.setRequestProperty("Accept-Charset", "utf-8");
        	conn.setRequestProperty("contentType", "utf-8");
        	conn.setRequestProperty("Content-Type", "application/x‑www‑form‑urlencoded");
        	conn.setRequestProperty("Authorization", "Bearer "+token);
        	conn.setConnectTimeout(5000);
        	conn.setReadTimeout(5000);
        	long start = System.currentTimeMillis();
        	//获取 URLConnection对象输出对应的输出流

        	// 定义BufferedReader输入流来读取URL的响应
        	in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
        	long end = System.currentTimeMillis();
        	
        	String line;
        	while (null != (line = in.readLine())) {
        		result += "\n" + line;
        	}
        	//建立
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

建立连接之前调用该方法即可 

javax.net.ssl.SSLException: Received fatal alert: protocol_version解决

 文章来源地址https://www.toymoban.com/news/detail-512940.html

到了这里,关于javax.net.ssl.SSLException: Received fatal alert: protocol_version解决的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • kettle工具连接MySQL数据库报错 Caused by: javax.net.ssl.SSLException: Received fatal alert: internal_error

            前几天为了修复MySQL数据库漏洞,项目上从5.7.41版本升级到了5.7.43,今天在使用kettle时发现数据库突然连不上了,测试连接报如下错误:            Error connecting to database: (using class org.gjt.mm.mysql.Driver) Communications link failure The last packet successfully received from the serve

    2024年02月06日
    浏览(39)
  • javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    原因:从java8 Update31开始,由于SSL协议中的安全漏洞,默认情况下禁用SSL v3协议 。 排查过程:可使用如下代码,打印http请求协议过程 插曲:最初报错异常是javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate),按网上教程,找到jdk.tls.

    2024年02月11日
    浏览(32)
  • https请求报错:javax.net.ssl.SSLHandshakeException:Received fatal alert: unrecognized_name 的解决过程

    提示:本地调试正常: 项目场景: 部署到WebSphere服务器上就会报上述错误; 一度认为是WebSphere服务器上的配置有问题,经过多次偿试,最终解决问题发现和服务配置无关; 测试环境使用HttpClient发送https请求下载附件时报错: 提示:项目地址是http,需要访问的地址是https: 因为访问

    2024年02月05日
    浏览(34)
  • 关于错误javax.net.ssl.SSLException: Received close_notify during handshake

          今天开发的小伙伴遇到一问题,报错内容是: javax.net.ssl.SSLException: Received close_notify during handshake at sun.security.ssl.Alerts.getSSLException(Unknown Source) at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown

    2024年02月07日
    浏览(27)
  • javax.net.ssl.SSLException: Connection reset

    https://www.cnblogs.com/colder/p/16612582.html 解决NoHttpResponseException问题 .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE) = 禁止连接重用,每次请求不再链接复用,而是创建一个新的链接 https://blog.csdn.net/yzf279533105/article/details/126963611 https://blog.csdn.net/enweitech/article/details/79261439 https://

    2024年02月04日
    浏览(29)
  • netty整合websocket支持自签证书出现netty websocket ssl Received fatal alert: certificate_unknown

    win+r cmd 生成自己jks文件,指向自己要生成jks的文件位置下,我直接生成到项目resources下 2.生成证书 3.迁移到行业标志 成功生成证书 将jks文件考入项目resources下 yaml配置: netty证书加载 这里我就只上关键代码了 不添加信任netty websocket ssl Received fatal alert: certificate_unknown。 错误原

    2024年02月02日
    浏览(34)
  • Gradle: javax.net.ssl.SSLException: No PSK available. Unable to resume.

    今天在使用 gradle 构建项目过程中, 发现一个 ssl 问题: JDK 11中存在一个错误:https://bugs.openjdk.java.net/browse/JDK-8213202 可通过如下方式解决/避免: 等待JDK 12发布 更新到JDK 11.0.3+,其中包括backport(向后移植) 使用此命令行参数作为解决方法:-Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2,TLSv1.3 建

    2024年02月07日
    浏览(33)
  • javax.net.ssl.SSLException: closing inbound before receiving peer‘s close_notify

    用 generator 逆向生成的时候遇到一个报错 这错误出现原因是要求开启了mysql的ssl验证( MySQL5.7+默认是开启SSL连接 ),需要我们主动配置ssl证书信息或者明确指出不适用ssl 1、明确不使用ssl严重 加参数 useSSL=false 在获取url最后加上 useSSL=false 即可: jdbc:mysql://localhost:3306/mydb?serverTi

    2024年02月13日
    浏览(43)
  • Gradle编译时报错 Caused by: javax.net.ssl.SSLException: No PSK available. Unable to resume.

    Gradle编译时报错 Caused by: javax.net.ssl.SSLException: No PSK available. Unable to resume. 这是 JDK 11 的一个bug, 升级到 JDK 11.0.3+ 可以解决 bug: https://bugs.openjdk.java.net/browse/JDK-8213202 不想升级的话可以手动修改 $JAVA_HOME/conf/security/java.security 文件, 找到 jdk.tls.disabledAlgorithms=SSLv3 所在的那一行,在行

    2024年02月12日
    浏览(68)
  • javax.net.ssl.SSLException: closing inbound before receiving peer‘s close_notify异常解决方法

    在项目打包时,报如下错误: 解决方法为数据库连接参数useSSL改为false。 说明:高版本的MySQL驱动(=8.0.13)默认情况下useSSL是true,这里需要明确设置useSSL=false。

    2024年02月03日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包