SSL异常:证书为<>,不匹配任何主题备用名称:[]

7

当我在Postman中使用我的个人证书访问URL时,它可以正常工作。但是当我尝试使用Rest Assured测试用例时,会抛出上述异常。

配置类

public class Configuration {

    protected SSLConfig config = null;
    private static final Logger LOG = LoggerFactory.getLogger(Configuration.class);

    @SuppressWarnings("deprecation")
    @BeforeClass
    public void setKeystore()

    {
        KeyStore keyStore = null;

        KeyStore trustStore = null;
        try {
            String certPassword = System.getProperty("certPassword");
            String certPath = System.getProperty("certPath");

            String trustStorePassword = System.getProperty("trustStorePassword");
            String trustStorePath = System.getProperty("trustStorePath");
            Validate.notNull(certPath, "Path to Certificate on the file system cannot be null");
            Validate.notEmpty(certPassword, "Password cannot be empty");
            Validate.notNull(trustStorePath, "Path to trustStore on the file system cannot be null");
            Validate.notEmpty(trustStorePassword, "TrustStore Password cannot be empty");

            keyStore = KeyStore.getInstance("JKS");
            keyStore.load(new FileInputStream(certPath), certPassword.toCharArray());
            trustStore = KeyStore.getInstance("JKS");
            trustStore.load(new FileInputStream(trustStorePath), trustStorePassword.toCharArray());

            if (keyStore != null) {

                org.apache.http.conn.ssl.SSLSocketFactory clientAuthFactory = new org.apache.http.conn.ssl.SSLSocketFactory(
                        keyStore, certPassword, trustStore);
                config = new SSLConfig().with().sslSocketFactory(clientAuthFactory).and().allowAllHostnames();

            }
            EnvironmentConstants.getEnvironment();

        } catch (Exception e) {
            LOG.error("Error while loading keystore");
            e.printStackTrace();
        }
    }

    @BeforeTest
    public Properties loadproperties() {

        InputStream input = getClass().getClassLoader().getResourceAsStream("errorMessages.properties");
        Properties properties = new Properties();
        try {
            properties.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

}

我们必须查看您的代码才能提供帮助。 - Rabbit Guy
你解决了这个问题吗? - phani
是的,请看下面我的回答。 - Rocky4Ever
但是为什么它可以在POSTMan中工作呢? - dzieciou
Postman和soapUI具有绕过DNS问题的功能,而在Java中,您需要编写自己的代码。 - Rocky4Ever
这个回答解决了你的问题吗?证书<localhost>与任何主题备用名称都不匹配 - Hakan54
3个回答

5
这个问题是因为我们公司配置了新的服务器,但是没有在服务器证书中包含DNS。所以我们公司在证书中加入了服务器名称。现在它可以正常工作了。

4
根据RFC 2818(HTTPS规范)
如果主机名可用,客户端必须将其与服务器的证书消息中呈现的服务器标识进行检查,以防止中间人攻击......如果存在类型为dNSName的subjectAltName扩展名,则必须将其用作标识。否则,必须使用证书主题字段中最具体的通用名称字段。尽管使用通用名称是现有的做法,但已被弃用,并鼓励认证机构改用dNSName。
您应该生成包含您计划使用证书的所有主机名的SAN扩展的证书:
keytool -genkeypair \
    -keystore server-keystore.pkcs12 \
    -deststoretype pkcs12 \
    -dname "CN=mydomain.local" \
    -keypass changeit \
    -storepass changeit \
    -keyalg RSA \
    -validity 1825 \
    -keysize 4096 \
    -alias mydomain.local \
    -ext SAN=dns:mydomain.local,dns:mydomain.dev,dns:mydomain.test,dns:localhost

1
如果您只想使用REST Assured成功发出请求,可以尝试使用.allowAllHostnames()方法来配置SSLConfig。这样客户端就不会验证主机名。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接