解决mybits配置文件中存在明文密码问题
解决思路
重写DriverManagerDataSource中的setPassword和setUsername方法,在其中加入解密过程
方法重写
public class MyDataSource extends DriverManagerDataSource {
public MyDataSource() {
super();
}
@Override
public void setPassword(String password){
try{
super.setPassword(decode(password));
}catch(Exception e){
logger.error(e.getMessage());
}
}
@Override
public void setUsername(String username) {
try{
super.setUsername(decode(username));
}catch(Exception e){
logger.error(e.getMessage());
}
}
//加密
public static String encodeToString(String password){
//编码
String str=null;
try {
str = Base64.getEncoder().encodeToString(password.getBytes("utf-8"));
System.out.println("编码结果为:"+str);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
//解密
public static String decode(String password){
//解码
String str=null;
byte[] str1=Base64.getDecoder().decode(password);
try {
str = new String(str1,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return str;
}
}
修改配置文件
calss是重写后的类名