- import java.util.Properties;
-
- import org.hibernate.HibernateException;
- import org.hibernate.cfg.Environment;
- import org.hibernate.connection.DriverManagerConnectionProvider;
-
- public class CustomDriverManagerConnectionProvider extends
- DriverManagerConnectionProvider {
-
- public CustomDriverManagerConnectionProvider() {
- super();
- }
-
- @Override
- public void configure(Properties props) throws HibernateException{
- String user = props.getProperty(Environment.USER);
- String password = props.getProperty(Environment.PASS);
- props.setProperty(Environment.USER, SecUtil.decrypt(user));
- props.setProperty(Environment.PASS, SecUtil.decrypt(password));
- super.configure(props);
- }
-
- }
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.connection.DriverManagerConnectionProvider;
public class CustomDriverManagerConnectionProvider extends
DriverManagerConnectionProvider {
public CustomDriverManagerConnectionProvider() {
super();
}
@Override
public void configure(Properties props) throws HibernateException{
String user = props.getProperty(Environment.USER);
String password = props.getProperty(Environment.PASS);
props.setProperty(Environment.USER, SecUtil.decrypt(user));
props.setProperty(Environment.PASS, SecUtil.decrypt(password));
super.configure(props);
}
}
再写一个类,使用AES负责字符串的加密与解密这里我们参照原作者的方法
-
-
-
-
-
-
-
-
-
- public class SecUtil {
-
- private static byte[] keybytes = { 0x31, 0x32, …… };
-
- public static void main(String[] args) throws Exception {
- String e1 = encrypt("newpassword");
- System.out.println(e1);
- String e2 = decrypt(e1);
- System.out.println(e2);
- }
-
-
-
-
-
-
- public static String encrypt(String value) {
-
- String s=null;
-
- int mode = Cipher.ENCRYPT_MODE;
-
- try {
- Cipher cipher = initCipher(mode);
-
- byte[] outBytes = cipher.doFinal(value.getBytes());
-
- s = String.valueOf(Hex.encodeHex(outBytes));
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return s;
- }
-
-
-
-
-
-
- public static String decrypt(String value) {
-
- String s = null;
-
- int mode = Cipher.DECRYPT_MODE;
-
- try {
- Cipher cipher = initCipher(mode);
-
- byte[] outBytes = cipher.doFinal(Hex.decodeHex(value.toCharArray()));
-
- s = new String(outBytes);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return s;
- }
-
- private static Cipher initCipher(int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
- Key key = new SecretKeySpec(keybytes, "AES");
- cipher.init(mode, key);
- return cipher;
- }
- }
/**
* AES加密工具
*
* @author Bany
*
* @version 创建时间:2008-8-5 上午10:58:16
*
*/
public class SecUtil {
private static byte[] keybytes = { 0x31, 0x32, …… };
public static void main(String[] args) throws Exception {
String e1 = encrypt("newpassword");
System.out.println(e1);
String e2 = decrypt(e1);
System.out.println(e2);
}
/**
* 加密
* @param value
* @return
*/
public static String encrypt(String value) {
String s=null;
int mode = Cipher.ENCRYPT_MODE;
try {
Cipher cipher = initCipher(mode);
byte[] outBytes = cipher.doFinal(value.getBytes());
s = String.valueOf(Hex.encodeHex(outBytes));
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
/**
* 解密
* @param value
* @return
*/
public static String decrypt(String value) {
String s = null;
int mode = Cipher.DECRYPT_MODE;
try {
Cipher cipher = initCipher(mode);
byte[] outBytes = cipher.doFinal(Hex.decodeHex(value.toCharArray()));
s = new String(outBytes);
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
private static Cipher initCipher(int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Key key = new SecretKeySpec(keybytes, "AES");
cipher.init(mode, key);
return cipher;
}
}
当然,你也可以使用自己写的加密方法,或者是
Java官方推荐的一些加密方法
调用SecUtil.encrypt的方法,把用户密码加密生成密文,然后根据密文修改hibernate.cfg.xml文件
- <property name="connection.username">c59cd98</property>
- <property name="connection.password">68e32593ea5943a6a</property>
- <property name="connection.provider_class">com.CustomDriverManagerConnectionProvider</property>