Java jpa用ssl连接到mysql

时间:2015-03-10 18:31:17

标签: java mysql spring jpa ssl

我有一个带有服务器私钥自我证书的mysql服务器,我想用spring和jpa(也有客户端私钥自我证书)将它与我的jsp应用程序连接,但它不起作用。

这是我的代码:

private EntityManager createEntityManager(){
    Properties prop = new Properties();
    String url = "", user = "", password = "";
    try {
        prop.load(getClass().getClassLoader().getResourceAsStream("jdbc.properties"));
        url = prop.getProperty("jdbc.url");
        user = prop.getProperty("jdbc.username");
        password = prop.getProperty("jdbc.password");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Map <String, String> properties = new HashMap<String,String>();
    properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    properties.put("hibernate.connection.url", url);
    properties.put("hibernate.connection.username", user);
    properties.put("hibernate.connection.password", password);
    properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    properties.put("hibernate.show-sql", "true");    
    System.setProperty("javax.net.ssl.keyStore","path_to_keystore_file");
    System.setProperty("javax.net.ssl.keyStorePassword","password");
    System.setProperty("javax.net.ssl.trustStore","path_to_truststore_file");
    System.setProperty("javax.net.ssl.trustStorePassword","password");      

    EntityManagerFactory fact = Persistence.createEntityManagerFactory("springappPU", properties);
    return fact.createEntityManager();  
}

网址属性包含useSSL=true&amp;requireSSL=true

0 个答案:

没有答案