gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:22:49 25 4
gpt4 key购买 nike

我遇到问题:连接到 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);

}

}

自定义初始化.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 (Hibernate 5)EncryptedPasswordC3P0ConnectionProvider (Hibernate 4) ,configure方法定义为

  • void configure(java.util.Map props) 对于 Hibernate 5
  • void configure(java.util.Properties props) for Hibernate 4

但是,在您的代码中,您有 void configure(final Properties props) 但使用 Hibernate 5。因此您需要使用正确的方法定义(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, ...
}

引用:

关于java - 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment](已更新),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51167070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com