javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names

问题:在使用 org.apache.http.*下的 CloseableHttpClient 发送https请求时报了以上错误

解决方案一:使用java.net.HttpURLConnection 

import java.net.HttpURLConnection;

public static HttpURLConnection connectToWeb(String uri) {
    HttpURLConnection connection = null;
    try {
        URL url = new URL(uri);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return connection;
}

解决方案二:在创建SSLConnectionSocketFactory时,添加NoopHostnameVerifier.INSTANCE参数

 public static CloseableHttpClient createSSLClientDefault() {
        CloseableHttpClient client = null;
        try {
            SSLContext sslContext = null;
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);//这里的红色部分
            client = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            e.printStackTrace();
        }
        return client;
    }

原理扩展:

在org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname方法中有如下代码:

而NoopHostnameVerifier源代码如下;verify方法直接返回true

/**
* The NO_OP HostnameVerifier essentially turns hostname verification
* off. This implementation is a no-op, and never throws the SSLException.
* 关闭主机名验证,直接返回true
* @since 4.4
*/
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class NoopHostnameVerifier implements HostnameVerifier {

    public static final NoopHostnameVerifier INSTANCE = new NoopHostnameVerifier();

    @Override
    public boolean verify(final String s, final SSLSession sslSession) {
        return true;
    }

    @Override
    public final String toString() {
        return "NO_OP";
    }

}

 

展开阅读全文

cas 报错 No subject alternative names present

02-26

今天在做学习cas时 rnrn在登录cas时没有问题,一旦登录之后返回的时候就报了如下错误: rnrnjavax.servlet.ServletException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present rnedu.yale.its.tp.cas.client.filter.CASFilter.getAuthenticatedUser(CASFilter.java:254) rnedu.yale.its.tp.cas.client.filter.CASFilter.doFilter(CASFilter.java:184) rnrnrnjavax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present rncom.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1649) rncom.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241) rncom.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235) rncom.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206) rncom.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136) rncom.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593) rncom.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149) rnsun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434) rnsun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166) rnsun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177) rnsun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234) rnedu.yale.its.tp.cas.util.SecureURL.retrieve(SecureURL.java:70) rnedu.yale.its.tp.cas.client.ServiceTicketValidator.validate(ServiceTicketValidator.java:212) rnedu.yale.its.tp.cas.client.filter.CASFilter.getAuthenticatedUser(CASFilter.java:219) rnedu.yale.its.tp.cas.client.filter.CASFilter.doFilter(CASFilter.java:184) rnrnrnrnjava.security.cert.CertificateException: No subject alternative names present rnsun.security.util.HostnameChecker.matchIP(HostnameChecker.java:142) rnsun.security.util.HostnameChecker.match(HostnameChecker.java:75) rncom.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:264) rncom.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:250) rncom.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185) rncom.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136) rncom.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593) rncom.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165) rncom.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149) rnsun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434) rnsun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166) rnsun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177) rnsun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234) rnedu.yale.its.tp.cas.util.SecureURL.retrieve(SecureURL.java:70) rnedu.yale.its.tp.cas.client.ServiceTicketValidator.validate(ServiceTicketValidator.java:212) rnedu.yale.its.tp.cas.client.filter.CASFilter.getAuthenticatedUser(CASFilter.java:219) rnedu.yale.its.tp.cas.client.filter.CASFilter.doFilter(CASFilter.java:184) rnrnrnrn请赐教,到底是哪错了。 rnrn另外:在配置tomcat的https时。访问https://127.0.0.1:8443 rnIE总是报证书错误。不知道是不是与这有关 rnrn 论坛

SSL exception: "No subject alternative names matching IP address

01-12

出错代码是: rnURL url = new URL("https://c2.hoopchina.com.cn/uploads/star/event/images/170112/296cc05818572e743dd3cae7c1be2c6d1ac61621.jpg");rnInputStream is = url.openStream();rnrnjavax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative DNS name matching c2.hoopchina.com.cn found.rn at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1747)rn at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241)rn at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235)rn at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1209)rn at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:135)rn at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593)rn at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:943)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1188)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1215)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1199)rn at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)rn at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)rn at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)rn at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)rn at java.net.URL.openStream(URL.java:1010)rn at wartih.testing.App.main(App.java:23)rnCaused by: java.security.cert.CertificateException: No subject alternative DNS name matching c2.hoopchina.com.cn found.rn at sun.security.util.HostnameChecker.matchDNS(HostnameChecker.java:193)rn at sun.security.util.HostnameChecker.match(HostnameChecker.java:77)rn at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:264)rn at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:250)rn at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1188)rn ... 13 morern 论坛

Column count doesn't match value count at row 1

02-17

错误代码:rn[img=https://img-bbs.csdn.net/upload/201602/17/1455712800_299645.png][/img]rnrn这是我的数据表rn[img=https://img-bbs.csdn.net/upload/201602/17/1455713007_357504.png][/img]rnrnSqlHelper .javarn[code=java]rnpublic class SqlHelper rnrn rn rn private static Connection ct=null;//连接rn private static ResultSet rs=null;//结果rn private static PreparedStatement ps=null;rnpublic static void executeUpdate(String sql, Object[] parameters) rn try rn // 1.创建一个psrn ct=DBUtil.getCon();rn ps = ct.prepareStatement(sql);rn // 给?赋值rn if (parameters != null)rn for (int i = 0; i < parameters.length; i++) rn ps.setObject(i + 1, parameters[i]);rn rn // 执行rn ps.executeUpdate();rn catch (SQLException e) rn e.printStackTrace();// 开发阶段rn throw new RuntimeException(e.getMessage());rn finally rn // 关闭资源rn DBUtil.close(rs, ps, ct);rn rn rn[/code]rnrncargo.javarn[code=java]rnpublic class cargo rnrn private int cid;rn private String cname;rn private float cprice;rn private String cfactory;rn private int cinventory;rn rn public cargo(String cname,float cprice,String cfactory,int cinventory )rn super();rn this.cname=cname;rn this.cprice=cprice;rn this.cfactory=cfactory;rn this.cinventory=cinventory;rn rn rn public int getCid() rn return cid;rn rn public void setCid(int cid) rn this.cid = cid;rn rn public String getCname() rn return cname;rn rn public void setCname(String cname) rn this.cname = cname;rn rn public float getCprice() rn return cprice;rn rn public void setCprice(float cprice) rn this.cprice = cprice;rn rn public String getCfactory() rn return cfactory;rn rn public void setCfactory(String cfactory) rn this.cfactory = cfactory;rn rn public int getCinventory() rn return cinventory;rn rn public void setCinventory(int cinventory) rn this.cinventory = cinventory;rn rn rn[/code]rnrnaddCargoUIrn[code=java]public class addCargoUI extends HttpServlet rn rn public void doGet(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rnrn request.setCharacterEncoding("utf-8");rn response.setContentType("text/html;charset=utf-8");rn PrintWriter out = response.getWriter();rn rn String name=request.getParameter("name");rn float price=Float.parseFloat(request.getParameter("price").toString());rn String factory=request.getParameter("factory");rn int inventory=Integer.parseInt(request.getParameter("inventory").toString());rn rn cargo cargo=new cargo(name, price, factory, inventory);rn cargoService cargoService=new cargoService();rn rn rn rn if(cargoService.addCargo(cargo))rn request.getRequestDispatcher("cargo.jsp").forward(request, response);rn rn else rn request.getRequestDispatcher("cargo.jsp").forward(request, response);rn rn rnrn rn public void doPost(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rnrn this.doGet(request, response);rn rnrnrnrn[/code]rnrncargoServicern[code=java]public class cargoService public boolean addCargo(cargo cargo)rn rn String sql="insert into cargo(cname,cprice,cfactory,cinventory) values(?,?,?,?)";rn String name=cargo.getCname();rn float price=cargo.getCprice();rn String factory=cargo.getCfactory();rn int inventory=cargo.getCinventory();rn Object[] parameters=name,price,factory,inventory;rn SqlHelper.executeUpdate(sql, parameters);rn rn rn return true; rn rn[/code]rnrnaddCargo.jsprn[code=java]rn rn 添加餐具rn rn rn rn rn rn 添加rn rn rn rnrn [/code] 论坛

?java.sql.SQLException: Column count doesn't match value count at row

04-10

建表的SQL:[code=SQL]use info;rncreate table employee (rn id int not null primary key AUTO_INCREMENT,rn name char(20) not null,rn wkno char(20) not null,rn department char(50),rn birthday datetime ,rn salary decimal(7,2)rnrn)[/code]rnactionForm[code=Java]/*rn * Generated by MyEclipse Strutsrn * Template path: templates/java/JavaClass.vtlrn */rnpackage com.fuyou.struts.form;rnrnimport java.util.Date;rnrnimport javax.servlet.http.HttpServletRequest;rnimport org.apache.struts.action.ActionErrors;rnimport org.apache.struts.action.ActionForm;rnimport org.apache.struts.action.ActionMapping;rnrn/** rn * MyEclipse Strutsrn * Creation date: 04-10-2008rn * rn * XDoclet definition:rn * @struts.form name="regForm"rn */rnrnpublic class RegForm extends ActionForm rn /*rn * Generated Methodsrn */rnrn /** rn * Method validatern * @param mappingrn * @param requestrn * @return ActionErrorsrn */rn private String name;rn private String wkno;rn private String department;rn private String birthday;rn private double salary = 0.0;rn rn rn public String getName() rn return name;rn rnrn public void setName(String name) rn this.name = name;rn rnrn public String getWkno() rn return wkno;rn rnrn public void setWkno(String wkno) rn this.wkno = wkno;rn rnrn public String getDepartment() rn return department;rn rnrn public void setDepartment(String department) rn this.department = department;rn rnrn public String getBirthday() rn return birthday;rn rnrn public void setBirthday(String birthday) rn this.birthday = birthday;rn rnrn public double getSalary() rn return salary;rn rnrn public void setSalary(double salary) rn this.salary = salary;rn rnrn public ActionErrors validate(ActionMapping mapping,rn HttpServletRequest request) rn rn return null;rn rnrn /** rn * Method resetrn * @param mappingrn * @param requestrn */rn public void reset(ActionMapping mapping, HttpServletRequest request) rn // TODO Auto-generated method stubrn rn[/code]:rnaction代码:[code=Java]/*rn * Generated by MyEclipse Strutsrn * Template path: templates/java/JavaClass.vtlrn */rnpackage com.fuyou.struts.action;rnrnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnimport org.apache.struts.action.Action;rnimport org.apache.struts.action.ActionForm;rnimport org.apache.struts.action.ActionForward;rnimport org.apache.struts.action.ActionMapping;rnrnimport com.fuyou.beans.Employee;rnimport com.fuyou.dao.EmployeeDao;rnimport com.fuyou.struts.form.RegForm;rnrn/** rn * MyEclipse Strutsrn * Creation date: 04-10-2008rn * rn * XDoclet definition:rn * @struts.action path="/reg" name="regForm" input="/reg.jsp" scope="request" validate="true"rn */rnpublic class RegAction extends Action rn /*rn * Generated Methodsrn */rn rnrn /** rn * Method executern * @param mappingrn * @param formrn * @param requestrn * @param responsern * @return ActionForwardrn */rn public ActionForward execute(ActionMapping mapping, ActionForm form,rn HttpServletRequest request, HttpServletResponse response) rn String name;rn String wkno;rn String department;rn String date;rn double salary = 0.0;rn RegForm regForm = (RegForm) form;rn rn name = regForm.getName();rn wkno = regForm.getWkno();rn department = regForm.getDepartment();rn date = regForm.getBirthday();rn salary = regForm.getSalary();rn java.sql.Date birthday = java.sql.Date.valueOf(date);rn Employee employee = new Employee(name,wkno,department,birthday,salary);rn EmployeeDao employeeDao = new EmployeeDao();rn employeeDao.save(employee);rn return mapping.findForward("success");rn rn[/code]rn这样的类型,它报这个错!rn信息: Server startup in 6437 msrnjava.sql.SQLException: Column count doesn't match value count at row 1rn at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)rn at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)rn at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)rn at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)rn at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)rn at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:843)rn at com.fuyou.dao.EmployeeDao.save(EmployeeDao.java:126)rn at com.fuyou.struts.action.RegAction.execute(RegAction.java:56)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn at org.apache.catalina.core.ApplicationFilterChain.in 论坛

没有更多推荐了,返回首页