Guest User

Untitled

a guest
Jul 3rd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7.  
  8.  
  9. <session-factory>
  10. <property name="hibernate.connection.provider_class">mypack.EncryptedPasswordC3P0ConnectionProvider</property>
  11. <property name="hibernate.connection.encryptor_registered_name">strongHibernateStringEncryptor</property>
  12. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  13. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=utf-8</property>
  14. <property name="hibernate.connection.username">root</property>
  15. <property name="hibernate.connection.password">ENC(Sq/CFOKQudnXQ8SjuS4t+Q==)</property>
  16. <property name="c3p0.min_size">5</property>
  17. <property name="c3p0.max_size">20</property>
  18. <property name="c3p0.timeout">1800</property>
  19. <property name="c3p0.max_statements">50</property>
  20.  
  21.  
  22. <mapping resource="user.hbm.xml" />
  23.  
  24.  
  25. </session-factory>
  26. </hibernate-configuration>
  27.  
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29. xmlns:context="http://www.springframework.org/schema/context"
  30. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  31. xmlns:tx="http://www.springframework.org/schema/tx"
  32. xmlns:util="http://www.springframework.org/schema/util"
  33. xsi:schemaLocation="http://www.springframework.org/schema/beans
  34. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  35. http://www.springframework.org/schema/context
  36. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  37. http://www.springframework.org/schema/tx
  38. http://www.springframework.org/schema/tx/spring-tx.xsd
  39. http://www.springframework.org/schema/util
  40. http://www.springframework.org/schema/util/spring-util.xsd">
  41.  
  42. <context:component-scan base-package="mypack">
  43. <context:exclude-filter type="annotation"
  44.  
  45. expression="org.springframework.stereotype.Controller"/>
  46. </context:component-scan>
  47. <context:property-placeholder /><!--enable accessing system environment
  48. variables-->
  49. <tx:annotation-driven transaction-manager="transactionManager"/>
  50.  
  51. <!--
  52. This is the key to link Spring's injection to Hibernate event-based
  53. validation.
  54. Notice the first constructor argument, this is our Spring ValidatorFactory
  55. instance.
  56. -->
  57. <bean id="customInitialize" class="mypack.CustomInitialize">
  58. <property name="encryptorPasswordVariable"
  59. value="MAIL_SERVICE_3DES_PASSWORD"/>
  60. </bean>
  61.  
  62. <bean id="hibernateStringEncryptor"
  63. class="org.jasypt.hibernate4.encryptor.HibernatePBEStringEncryptor">
  64. <property name="registeredName" value="strongHibernateStringEncryptor"/>
  65. <property name="algorithm" value="PBEWithMD5AndTripleDES"/>
  66. <property name="password" value="${MAIL_SERVICE_3DES_PASSWORD}"/>
  67. </bean>
  68. <bean id="transactionManager"
  69. class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  70. <property name="sessionFactory" ref="sessionFactory"/>
  71. </bean>
  72.  
  73. <bean id="sessionFactory" depends-on="hibernateStringEncryptor"
  74. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  75. <property name="configLocation" value="hibernate.cfg.xml"/>
  76. <property name="packagesToScan" value="mypack"/>
  77. <property name="hibernateProperties"
  78. value="org.hibernate.dialect.MySQLDialect">
  79. <props>
  80. <prop key="hibernate.hbm2ddl.auto">update</prop>
  81. <prop
  82. key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  83. <prop key="hibernate.connection.CharSet">utf8</prop>
  84. <prop key="hibernate.connection.characterEncoding">utf8</prop>
  85. <prop key="hibernate.connection.useUnicode">true</prop>
  86. <prop key="hibernate.show_sql">true</prop>
  87. <prop key="hibernate.format_sql">true</prop>
  88. <prop key="hibernate.cache.use_second_level_cache">true</prop>
  89. <prop key="hibernate.cache.use_query_cache">true</prop>
  90. <prop key="hibernate.generate_statistics">false</prop>
  91. <prop key="javax.persistence.validation.mode">none</prop>
  92. </props>
  93. </property>
  94. </bean>
  95.  
  96. package mypack;
  97.  
  98.  
  99. import java.util.Properties;
  100. import org.hibernate.c3p0.internal.C3P0ConnectionProvider;
  101. import org.hibernate.cfg.AvailableSettings;
  102. import org.jasypt.encryption.pbe.PBEStringEncryptor;
  103. import org.jasypt.exceptions.EncryptionInitializationException;
  104. import org.jasypt.hibernate4.connectionprovider.ParameterNaming;
  105. import org.jasypt.hibernate4.encryptor.HibernatePBEEncryptorRegistry;
  106. import org.jasypt.properties.PropertyValueEncryptionUtils;
  107.  
  108.  
  109.  
  110.  
  111. public class EncryptedPasswordC3P0ConnectionProvider extends
  112. C3P0ConnectionProvider {
  113. private static final long serialVersionUID = 5273353009914873806L;
  114.  
  115. public EncryptedPasswordC3P0ConnectionProvider() {
  116. super();
  117. }
  118.  
  119.  
  120. public void configure(final Properties props) {
  121. final String encryptorRegisteredName =
  122. (String)props.get(ParameterNaming.ENCRYPTOR_REGISTERED_NAME);
  123. final HibernatePBEEncryptorRegistry encryptorRegistry =
  124. HibernatePBEEncryptorRegistry.getInstance();
  125. final PBEStringEncryptor encryptor =
  126. encryptorRegistry.getPBEStringEncryptor(encryptorRegisteredName);
  127.  
  128. if (encryptor == null) {
  129. throw new EncryptionInitializationException(
  130. "No string encryptor registered for hibernate " +
  131. "with name "" + encryptorRegisteredName + """);
  132. }
  133.  
  134. final String driver = (String) props.get(AvailableSettings.DRIVER);
  135. final String url = (String) props.get(AvailableSettings.URL);
  136. final String user = (String) props.get(AvailableSettings.USER);
  137. final String password = (String) props.get(AvailableSettings.PASS);
  138.  
  139. if (PropertyValueEncryptionUtils.isEncryptedValue(driver)) {
  140. props.put(AvailableSettings.DRIVER,
  141. PropertyValueEncryptionUtils.decrypt(driver, encryptor));
  142. }
  143. if (PropertyValueEncryptionUtils.isEncryptedValue(url)) {
  144. props.put(AvailableSettings.URL,
  145. PropertyValueEncryptionUtils.decrypt(url, encryptor));
  146. }
  147. if (PropertyValueEncryptionUtils.isEncryptedValue(user)) {
  148. props.put(AvailableSettings.USER,
  149. PropertyValueEncryptionUtils.decrypt(user, encryptor));
  150. }
  151. if (PropertyValueEncryptionUtils.isEncryptedValue(password)) {
  152. props.put(AvailableSettings.PASS,
  153. PropertyValueEncryptionUtils.decrypt(password, encryptor));
  154. }
  155.  
  156. super.configure(props);
  157.  
  158. }
  159.  
  160. }
  161.  
  162. package mypack;
  163.  
  164.  
  165. import org.springframework.beans.factory.InitializingBean;
  166. import org.springframework.stereotype.Component;
  167.  
  168. @Component
  169. public class CustomInitialize implements InitializingBean {
  170.  
  171.  
  172. private String encryptorPasswordVariable;
  173.  
  174.  
  175. public void afterPropertiesSet() throws Exception {
  176. if (encryptorPasswordVariable != null) {
  177. Runtime runtime = Runtime.getRuntime();
  178.  
  179. runtime.exec("setx " + encryptorPasswordVariable+ " "" /M");
  180. }
  181.  
  182. }
  183. public String getEncryptorPasswordVariable() {
  184. return encryptorPasswordVariable;
  185. }
  186. public void setEncryptorPasswordVariable(String encryptorPassword) {
  187. this.encryptorPasswordVariable= encryptorPassword;
  188. }
  189. }
Add Comment
Please, Sign In to add comment