1

我已将 Spring3.1、Hibernate4 和 Jasypt1.90 的加密密码配置到 MySQL 服务器 5 的配置文件中,但它不起作用并给出以下错误:

Caused by:java.sql.SQLException: Access denied for user 'root'@'iplcewks01056.noida.innodata.net' (using 
password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:934)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4104)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1299)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2338)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2186)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2168)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at 
org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.
getConnection(DatasourceConnectionProviderImpl.java:141)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.
obtainConnection(AbstractSessionImpl.java:292)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.
obtainConnection(LogicalConnectionImpl.java:297)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.
getConnection(LogicalConnectionImpl.java:169)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.
begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1395)
at org.springframework.orm.hibernate4.HibernateTransactionManager.
doBegin(HibernateTransactionManager.java:399)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.
getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.
createTransactionIfNecessary(TransactionAspectSupport.java:334)
at org.springframework.transaction.interceptor.TransactionInterceptor.
invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.
invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy59.getAllRoles(Unknown Source)
at com.synodex.webapp.listener.StartupListener.setupContext(StartupListener.java:113)

让我描述一下,到目前为止我所做的一切:

Step-I-Jasypt-Spring-Hibernate Maven 依赖

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt-spring31</artifactId>
    <version>1.9.0</version>
    <scope>compile</scope>
</dependency>
<dependency>
        <groupId>org.jasypt</groupId>
        <artifactId>jasypt-hibernate4</artifactId>
        <version>1.9.0</version>
        <scope>compile</scope>
</dependency>

applicationContext-resources.xml 中的 Step-II-Jasypt-Spring-Hibernate 配置:我在这里使用了两种方法,但都不起作用。

第一种方法:

<bean id="propertyConfigurer" 
            class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
             <constructor-arg ref="configurationEncryptor" />
             <property name="locations">
                 <list>
                    <value>classpath:jdbc.properties</value>
                    <value>classpath:mail.properties</value>
                    <value>classpath:hibernate.properties</value>
                    <value>classpath:ldap.properties</value>
                 </list>
               </property>
            </bean>
            <bean id="configurationEncryptor" class="com.synodex.util.JasyptDecryptorUtil" 
            factory-method="getEncriptorInstance" />


public class JasyptDecryptorUtil {
private final static JasyptDecryptorUtil jasyptutil = new JasyptDecryptorUtil();
private static StandardPBEStringEncryptor encryptor;

private JasyptDecryptorUtil() {
    String PWD = "SDHLKSHUWEHDKSLKLJKSALJDLKA00IUAY98273492JLKASJDLKASJDKLAJSD";
    encryptor = new StandardPBEStringEncryptor();
    encryptor.setAlgorithm("PBEWithMD5AndDES");
    encryptor.setPassword(PWD);
}

public static String getDecryptedString(String dec) {
    return encryptor.decrypt(dec);
}

public static String getEncryptedString(String dec) {
    return encryptor.encrypt(dec);
}

public static synchronized StandardPBEStringEncryptor getEncriptorInstance() {
    return encryptor;
}

/*
 * Get decrypted values stored in a property file
 */
public static Properties getDecryptedProperties(String pfile)throws IOException, FileNotFoundException {
    Properties properties = new EncryptableProperties(encryptor);
    properties.load(new FileInputStream(new File(pfile)));

    return properties;
}

}

第二种方法:

<bean id="propertyConfigurer" 
           class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
           <constructor-arg ref="configurationEncryptor" />
           <property name="locations">
               <list>
                   <value>classpath:jdbc.properties</value>
                   <value>classpath:mail.properties</value>
                   <value>classpath:hibernate.properties</value>
                   <value>classpath:ldap.properties</value>
               </list>
              </property>
            </bean>
           <bean id="configurationEncryptor" 
           class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
              <property name="config" ref="environmentVariablesConfiguration" />
           </bean>
           <bean id="environmentVariablesConfiguration" 
           class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
              <property name="algorithm" value="PBEWithMD5AndDES" />
              <property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" />
          </bean>

注意:我必须将 APP_ENCRYPTION_PASSWORD 系统属性设置为 pom.xml 以获取主密码:

<plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.3.v20120416</version>
            <configuration>
                <webApp>
                    <contextPath>/sc</contextPath>
                </webApp>
               <systemProperties>
                <systemProperty>
                  <name>APP_ENCRYPTION_PASSWORD</name>
                  <value>SDHLKSHUWEHDKSLKLJKSALJDLKA00IUAY98273492JLKASJDLKASJDKLAJSD</value>
                </systemProperty>
               </systemProperties>
                <connectors>
                    <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                        <port>8080</port>
                        <maxIdleTime>60000</maxIdleTime>
                    </connector>
                    <connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">
                        <port>8443</port>
                        <maxIdleTime>60000</maxIdleTime>
                        <keystore>${project.build.directory}/jetty-ssl.keystore</keystore>
                        <password>jetty6</password>
                        <keyPassword>jetty6</keyPassword>
                    </connector>
                </connectors> 
            </configuration>
        </plugin>

Step-III- 我使用了 c3p0 连接池而不是 dbcp 池,但它也不起作用。

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxPoolSize" value="100" />
    <property name="maxStatements" value="10" />
    <property name="minPoolSize" value="20" />
    <property name="testConnectionOnCheckin" value="true"/> 
</bean>

请建议我在这里缺少的任何东西。我做了很多尝试,但没有取得任何成功。

4

1 回答 1

0

使用嵌套属性值后已解决:<property name="password" value="${${jdbc.password.meta}}"/>

于 2013-04-10T03:47:09.303 回答