繁体   English   中英

无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment](已更新)

[英]Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] (UPDATED)

我有一个问题:连接到mySql。 有没有人知道如何解决这个问题? 我想加密我的数据库密码。

我已经加密了密码。 我的代码正在解密。 我希望不是。

你能帮助我吗 :(

安慰:

2906 [main] INFO  
org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator  - 
HHH000130: Instantiating explicit connection provider: 
mypack.EncryptedPasswordC3P0ConnectionProvider
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable 
to create requested service 
[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.createService(AbstractServiceRegistryImpl.java:275)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.initializeService(AbstractServiceRegistryImpl.java:237)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory
.injectServices(DefaultIdentifierGeneratorFactory.java:152)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.injectDependencies(AbstractServiceRegistryImpl.java:286)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.initializeService(AbstractServiceRegistryImpl.java:243)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl
.<init>(InFlightMetadataCollectorImpl.java:179)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess
.complete(MetadataBuildingProcess.java:119)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess
.build(MetadataBuildingProcess.java:84)
at org.hibernate.boot.internal.MetadataBuilderImpl
.build(MetadataBuilderImpl.java:474)
at org.hibernate.boot.internal.MetadataBuilderImpl
.build(MetadataBuilderImpl.java:85)
at org.hibernate.cfg.Configuration
.buildSessionFactory(Configuration.java:689)
at 
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at mypack.DataInsertion.insertInfo(DataInsertion.java:21)
at mypack.DataInsertion.main(DataInsertion.java:13)
Caused by: org.jasypt.exceptions.EncryptionInitializationException: No string 
encryptor registered for hibernate with name "strongHibernateStringEncryptor"
at mypack.EncryptedPasswordC3P0ConnectionProvider
.configure(EncryptedPasswordC3P0ConnectionProvider.java:30)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl
.configureService(StandardServiceRegistryImpl.java:100)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.initializeService(AbstractServiceRegistryImpl.java:246)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.getService(AbstractServiceRegistryImpl.java:214)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator
.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator
.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator
.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl
.initiateService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl
.createService(AbstractServiceRegistryImpl.java:263)
... 15 more

有没有人知道如何解决这个问题? 我想加密我的数据库密码。 我的源代码在这里:

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>


<session-factory>
    <property name="hibernate.connection.provider_class">mypack.EncryptedPasswordC3P0ConnectionProvider</property>
    <property name="hibernate.connection.encryptor_registered_name">strongHibernateStringEncryptor</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb?autoReconnect=true&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">ENC(Sq/CFOKQudnXQ8SjuS4t+Q==)</property>
    <property name="c3p0.min_size">5</property>
    <property name="c3p0.max_size">20</property>
    <property name="c3p0.timeout">1800</property>
    <property name="c3p0.max_statements">50</property>


    <mapping resource="user.hbm.xml" />


</session-factory>
</hibernate-configuration>

Beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd 
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="mypack">
    <context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder /><!--enable accessing system environment 
variables-->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!--
This is the key to link Spring's injection to Hibernate event-based 
validation.
Notice the first constructor argument, this is our Spring ValidatorFactory 
instance.
-->
<bean id="customInitialize" class="mypack.CustomInitialize">
    <property name="encryptorPasswordVariable" 
value="MAIL_SERVICE_3DES_PASSWORD"/>
</bean>

<bean id="hibernateStringEncryptor"
      class="org.jasypt.hibernate4.encryptor.HibernatePBEStringEncryptor">
    <property name="registeredName" value="strongHibernateStringEncryptor"/>
    <property name="algorithm" value="PBEWithMD5AndTripleDES"/>
    <property name="password" value="${MAIL_SERVICE_3DES_PASSWORD}"/>
</bean>
<bean id="transactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="sessionFactory" depends-on="hibernateStringEncryptor"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="configLocation" value="hibernate.cfg.xml"/>
    <property name="packagesToScan" value="mypack"/>
    <property name="hibernateProperties" 
value="org.hibernate.dialect.MySQLDialect">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>                
            <prop 
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.connection.CharSet">utf8</prop>
            <prop key="hibernate.connection.characterEncoding">utf8</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.generate_statistics">false</prop>
            <prop key="javax.persistence.validation.mode">none</prop>
        </props>
    </property>
</bean>

EncryptedPasswordC3P0ConnectionProvider.class

package mypack;


import java.util.Map;
import org.hibernate.c3p0.internal.C3P0ConnectionProvider;
import org.hibernate.cfg.AvailableSettings;
import org.jasypt.encryption.pbe.PBEStringEncryptor;
import org.jasypt.exceptions.EncryptionInitializationException;
import org.jasypt.hibernate4.connectionprovider.ParameterNaming;
import org.jasypt.hibernate4.encryptor.HibernatePBEEncryptorRegistry;
import org.jasypt.properties.PropertyValueEncryptionUtils;




public class EncryptedPasswordC3P0ConnectionProvider extends 
C3P0ConnectionProvider { 
private static final long serialVersionUID = 5273353009914873806L;

public EncryptedPasswordC3P0ConnectionProvider() {
     super();
     }


public void configure(Map props) {
     final String encryptorRegisteredName = 
(String)props.get(ParameterNaming.ENCRYPTOR_REGISTERED_NAME);
     final HibernatePBEEncryptorRegistry encryptorRegistry = 
HibernatePBEEncryptorRegistry.getInstance();
     final PBEStringEncryptor encryptor = 
encryptorRegistry.getPBEStringEncryptor(encryptorRegisteredName);

     if (encryptor == null) {
     throw new EncryptionInitializationException(
     "No string encryptor registered for hibernate " +
     "with name \"" + encryptorRegisteredName + "\"");
     }

     final String driver = (String) props.get(AvailableSettings.DRIVER);
     final String url = (String) props.get(AvailableSettings.URL);
     final String user = (String) props.get(AvailableSettings.USER);
     final String password = (String) props.get(AvailableSettings.PASS);

     if (PropertyValueEncryptionUtils.isEncryptedValue(driver)) {
         props.put(AvailableSettings.DRIVER, 
PropertyValueEncryptionUtils.decrypt(driver, encryptor)); 
         }
     if (PropertyValueEncryptionUtils.isEncryptedValue(url)) {
         props.put(AvailableSettings.URL, 
PropertyValueEncryptionUtils.decrypt(url, encryptor));
         }
     if (PropertyValueEncryptionUtils.isEncryptedValue(user)) {
         props.put(AvailableSettings.USER, 
PropertyValueEncryptionUtils.decrypt(user, encryptor));
         }
     if (PropertyValueEncryptionUtils.isEncryptedValue(password)) {
         props.put(AvailableSettings.PASS, 
PropertyValueEncryptionUtils.decrypt(password, encryptor));
         }

         super.configure(props);

 }

}

CustomInitialize.class

package mypack;


import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class CustomInitialize implements InitializingBean {


private String encryptorPasswordVariable;


public void afterPropertiesSet() throws Exception {
    if (encryptorPasswordVariable != null) {
        Runtime runtime = Runtime.getRuntime();

     runtime.exec("setx " + encryptorPasswordVariable+ " \"\" /M");
}

}
public String getEncryptorPasswordVariable() {
    return encryptorPasswordVariable;
}
public void setEncryptorPasswordVariable(String encryptorPassword) {
    this.encryptorPasswordVariable= encryptorPassword;
}
}

最初的问题:由以下原因引起:java.sql.SQLException:用户被拒绝访问

根据C3P0ConnectionProvider(休眠5)EncryptedPasswordC3P0ConnectionProvider(休眠4) ,将configure方法定义为

  • void configure(java.util.Map props) Hibernate 5的 void configure(java.util.Map props)
  • Hibernate 4的 void configure(java.util.Properties props)

但是,在您的代码中,您具有void configure(final Properties props)但使用了Hibernate5。因此,您需要使用正确的方法定义(使用Map而不是Properties)。

我会尝试:

import java.util.Map;
import org.hibernate.c3p0.internal.C3P0ConnectionProvider;
import org.hibernate.cfg.AvailableSettings;
import org.jasypt.encryption.pbe.PBEStringEncryptor;
import org.jasypt.exceptions.EncryptionInitializationException;
import org.jasypt.hibernate4.connectionprovider.ParameterNaming;
import org.jasypt.hibernate4.encryptor.HibernatePBEEncryptorRegistry;
import org.jasypt.properties.PropertyValueEncryptionUtils;

public final class EncryptedPasswordC3P0ConnectionProvider extends C3P0ConnectionProvider {

    public EncryptedPasswordC3P0ConnectionProvider() {
        // call the constructor of the super class
        super();
    }

    // configure method get a Map object (old hibernate version got a Properties object)
    public void configure(Map props) {
        final String encryptorRegisteredName =
        (String) props.get(ParameterNaming.ENCRYPTOR_REGISTERED_NAME);

        final HibernatePBEEncryptorRegistry encryptorRegistry =
        HibernatePBEEncryptorRegistry.getInstance();
        final PBEStringEncryptor encryptor = 
        encryptorRegistry.getPBEStringEncryptor(encryptorRegisteredName);

        // encryptor should never be null
        if (encryptor == null) {
            throw new EncryptionInitializationException(
               "No string encryptor registered for hibernate " +
               "with name \"" + encryptorRegisteredName + "\"");
        }

        // Get the original values from your hibernate config file
        final String user = (String) props.get(AvailableSettings.USER);
        final String password = (String) props.get(AvailableSettings.PASS);
        final String driver = (String) props.get(AvailableSettings.DRIVER);
        final String url = (String) props.get(AvailableSettings.URL);

        // Perform decryption operations as needed and store the new values
        if (PropertyValueEncryptionUtils.isEncryptedValue(driver)) {
            props.put(
              AvailableSettings.DRIVER, 
              PropertyValueEncryptionUtils.decrypt(driver, encryptor));
        }
        if (PropertyValueEncryptionUtils.isEncryptedValue(url)) {
            props.put(
              AvailableSettings.URL, 
              PropertyValueEncryptionUtils.decrypt(url, encryptor));
        }
        if (PropertyValueEncryptionUtils.isEncryptedValue(user)) {
            props.put(
              AvailableSettings.USER, 
              PropertyValueEncryptionUtils.decrypt(user, encryptor));
        }
        if (PropertyValueEncryptionUtils.isEncryptedValue(password)) {
            props.put(
              AvailableSettings.PASS, 
              PropertyValueEncryptionUtils.decrypt(password, encryptor));
        }

        // Let Hibernate framework do the rest
        super.configure(props);
        }  
    }
    ... // your getters, setters, ...
}

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM