資料庫連接資訊如下步驟:
1.配置屬性檔案。
2.配置spring的設定檔。
步驟1代碼如下:
pts4.jdbc.driverClassName=com.mysql.jdbc.Driver pts4.jdbc.url=jdbc:mysql://localhost:3306/acmeeting pts4.jdbc.username=ENC(T3B1IVb7jiRLgf9LkRmsQA==) pts4.jdbc.password=ENC(2g69lhQHTq8Fzrz8F5dMew==)
步驟2代碼如下:
<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig"> <property name="algorithm" value="PBEWithMD5AndDES" /> <property name="password" value="jasypt" /> </bean> <!-- 指定加密的類 --> <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"> <property name="config" ref="environmentVariablesConfiguration" /> </bean> <!-- 讀取屬性檔案 --> <bean id="propertyConfigurer" class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer"> <constructor-arg ref="configurationEncryptor" /> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> </bean>
對資料庫特殊欄位的加密只要對hibernate的映射設定檔設定即可,代碼如下:
<hibernate-mapping package="cn.edu.cqu.acMeeting.domain"> <typedef name="encryptedString" class="org.jasypt.hibernate.type.EncryptedStringType"> <param name="algorithm">PBEWithMD5AndDES</param> <param name="password">jasypt</param> </typedef> <class name="User"> <id name="id"> <generator class="native"/> </id> <property name="userName" type="encryptedString"/> <property name="password" type="encryptedString"></property> </class> </hibernate-mapping >
配置個typedef然後在要加密的特殊欄位上設定type屬性即可。