In the development Web project using SPRINGMVC, the user name of the database, the password is typically configured in the. Properties file
Variables that are then introduced into the. Properties through the. XML configuration file, for example
In the Config.properties file, configure the following variable, the value of the variable is configured under the Profile tab of the Pom.xml, no longer repeat
Jdbc.driverclassname=com.mysql.jdbc.driver
Jdbc.url=jdbc\:mysql\://${p.jdbc.url}/${p.jdbc.dbname}? useunicode\=true&characterencoding\=utf-8&zerodatetimebehavior\=converttonull& Rewritebatchedstatements\=true
Jdbc.username=${p.jdbc.username}
Jdbc.password=${p.jdbc.password}
In Applicationcontext.xml, these variable values are introduced by the following tags
<context:property-placeholder location= "Classpath:/config.properties"/>
This is clear in the account, password, there is no problem. But if my account password in the config file is encrypted, then how do I use the configuration?
Workaround:
1. First determine the encryption and decryption algorithm, in this case, we certainly choose to be symmetric encryption and decryption algorithm, the preferred des algorithm, here to take his example
2. Complete the encryption and decryption algorithm, this code is very simple, do not repeat, the key to their own decision, confidentiality can
public class Desutils {private static key key;
private static String key_str= "MyKey";
static{try {keygenerator generator = keygenerator.getinstance ("DES");
SecureRandom securerandom=securerandom.getinstance ("sha1prng");
Securerandom.setseed (Key_str.getbytes ());
Generator.init (SecureRandom);
Key = Generator.generatekey ();
Generator=null;
} catch (Exception e) {throw new RuntimeException (e); }}/** * Encrypt string, return BASE64 encryption String * < function details > * @param str * @return * @see [Class, Class # method, Class # Member] */public static string getencryptstring (String str) {Base64encoder Base64encoder = new BASE64
Encoder ();
SYSTEM.OUT.PRINTLN (key);
try {byte[] strbytes = str.getbytes ("UTF-8");
Cipher Cipher = cipher.getinstance ("DES"); Cipher.init (cipher.encryPt_mode, key);
byte[] encryptstrbytes = cipher.dofinal (strbytes);
Return Base64encoder.encode (encryptstrbytes);
} catch (Exception e) {throw new RuntimeException (e); }}/** * Decrypt BASE64 encrypted String * < function description > * @param str * @return * @see [ Class, Class # method, Class # Member] */public static String getdecryptstring (String str) {Base64decoder Base64decoder = new BASE
64Decoder ();
try {byte[] strbytes = Base64decoder.decodebuffer (str);
Cipher Cipher = cipher.getinstance ("DES");
Cipher.init (Cipher.decrypt_mode, key);
byte[] encryptstrbytes = cipher.dofinal (strbytes);
return new String (Encryptstrbytes, "UTF-8");
} catch (Exception e) {throw new RuntimeException (e); }} public static void Main (string[] args) {String name = "ROot ";
String password= "1234";
String encryname = getencryptstring (name);
String Encrypassword = getencryptstring (password);
System.out.println (Encryname);
System.out.println (Encrypassword);
System.out.println (getdecryptstring (encryname));
System.out.println (getdecryptstring (Encrypassword)); }
}
Interfaces that need to be decrypted in 3.springMVC
The Convertproperty method needs to be overwritten, Encryptpropnames stores the properties that need to be decrypted
public class Encryptpropertyplaceholderconfigurer extends Propertyplaceholderconfigurer
{
private string[] Encryptpropnames = {"Jdbc.username", "Jdbc.password"};
@Override
protected String Convertproperty (String propertyname, String propertyvalue)
{
// If the attribute is found in the encrypted attribute list (
Isencryptprop (PropertyName))
{
String decryptvalue = desutils.getdecryptstring ( PropertyValue);
System.out.println (decryptvalue);
return decryptvalue;
} else {
return propertyvalue;
}
}
Private Boolean Isencryptprop (String propertyname)
{for
(string encryptname:encryptpropnames)
{
if (Encryptname.equals (PropertyName))
{
return true;
}
}
return false;
}
}
4. Configure this decryption class
With
<bean class= "Com.cyou.web.common.EncryptPropertyPlaceholderConfigurer" p:location= "classpath:/ Config.properties "></bean>
instead of
<context:property-placeholder location=" classpath:/ Config.properties "/>