Spring's configuration file is a "drawing" used to guide Spring factory to generate beans, inject dependency and bean example distribution. It is one or more XML documents with bricks. J2EE programmers must learn to flexibly apply this "drawing" to accurately express their "generating intention". Spring configuration file is one or more standard XML documents. ApplicationContext.xml is Spring's default configuration file. When the specified configuration document cannot be found when the container starts, this default configuration file will be attempted.
In some production environments where security requirements are high, the spring framework does not allow plaintext username and password configurations, such as database configurations. This article is mainly used to solve the encryption of plaintext username and password.
Password decryption by inheriting the spring configuration class and rewriting the processing method
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private String[] encryptPropNames = {"username", "password"}; @Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactory,Properties props) throws BeansException {try {for (int i = 0;i<encryptPropNames.length;i++){ String value = props.getProperty(encryptPropNames[i]); if (value != null) {props.setProperty(encryptPropNames[i],new String(DES.decrypt(new BASE64Decoder().decodeBuffer(value), "Decrypt key"))); } } super.processProperties(beanFactory, props);} catch (Exception e) { e.printStackTrace(); throw new BeanInitializationException(e.getMessage());}} }Configure the applicationContext.xml file and set the ciphertext in jdbc.properties (generated based on the decryption key)
<!-- class Fill in the classpath of the code just now-><bean id="propertyConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean>
Summarize
The above is all about the example of spring configuration file encryption method in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Java programming implementation of springMVC simple login example
Detailed explanation of user query code for SpringMVC development restful API
Maven Management SpringBoot Profile Detailed explanation
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site.