SpringMVC uses implicit jdbc connection information
In the local test environment, if we use SpringMVC to build a project, the "user name, password, and connection address" of the database will be written in plain text in the configuration file. Once the project is built on a server in the running environment, the connection information of "user name, password, connection address" must be hidden, which is more secure, so how do I implement the implicit jdbc connection information?
1. feasible scheme of implicit jdbc connection information
SpringMVC generally provides the following information to the project's JdbcConnection in the "applicationContext. xml" file when constructing jdbc connection information.
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%20%E5%BC%95%E5%85%A5jdbc%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%20%2D%2D%3E--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>file:C:/properties/hcmanage.properties</value> </property></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}?useUnicode=true&characterEncoding=utf8&"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> <property name="testOnBorrow" value="true"> <property name="validationQuery"> <value>select 1 from DUAL</value> </property></property></bean></code>
Then we configure the plaintext information of "user name, password, connection address" in the hcmanage. properties file.
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/jdbcusername=rootpassword=root
This original practice directly exposes key information such as the user name, password, and connection address. How can we avoid such key information? The best practice is to provide only the following information in the hcmanage. properties file:
driver=com.mysql.jdbc.Driverurl=username=password=
We store the real information of "user name, password, connection address" in a relatively safe location, for example, our own database, which is not in the production environment, if someone else wants to know the "user name, password, connection address" in the production environment, they must first crack our own server and then crack the database on the server, which is relatively more difficult.
So what should we do to achieve this security effect?
The key point is"PropertyplaceholderpolicerClass!
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%20%E5%BC%95%E5%85%A5jdbc%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%20%2D%2D%3E--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>file:C:/properties/hcmanage.properties</value> </property></bean></code>Ii. inherit the propertyplaceholderpolicer class
Yes, we have created a custom PropertyPlaceholderConfigurer class that inherits "org. springframework. beans. factory. config. PropertyPlaceholderConfigurer ".
EncryptPropertyPlaceholderConfigurer. java
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { @Override protected String convertProperty(String propertyName, String propertyValue) { if (isEncryptProperty(propertyName)) { return Constants.databaseMap.get(propertyName); } return super.convertProperty(propertyName, propertyValue); } private boolean isEncryptProperty(String pname) { for (String name : Constants.DATABASE_PROPERTY_NAMES) { if (name.equals(pname)) { return true; } } return false; }}
Ps: Key Methods
Protected String convertProperty (String propertyName, String propertyValue)
This method is converted according to the propertyName provided in the configuration file and returns the corresponding propertyValue.
That is to say, we can
driver=com.mysql.jdbc.Driverurl=username=password=
Convert
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/jdbcusername=rootpassword=root
This process is not transparent, that is, implicit conversion!
3. Use webservice to obtain jdbc connection information. Java implements SSH mode encryption and communication between WebServices.
For more information about how to establish a webservice communication connection, see section 2.
For how to perform asymmetric encryption during communication, refer to article 1.
Since I have previously written a similar blog, I will not go into details here. It is important to provide a solution for SpringMVC to use implicit jdbc connection information!