May 31, 2021 Article blog
This article was reproduced from the public number: Java Travel
The biggest feature of Spring Boot is its automatic configuration, which greatly reduces the tedious configuration of traditional Spring frameworks and allows access to other components with a few simple lines of configuration. F or example, if you want to connect to the mysql database, you just need to include some configuration information from mysql in the configuration file. T o protect the security of their data, more and more companies are choosing to encrypt this important information. Let's take a look at how to configure encrypted files and successfully connect to the database.
There are several ways to configure information encryption, and here I'll just write about one of my more common ways. F irst, the user name and password are encrypted by some encryption algorithm, and then the original clear text is replaced by an encrypted string in the configuration file. Then customize the data source and decrypt the username and password in the custom data source.
Spring Boot's
automatic assembly, as detailed in previous tweets, is a quick review today. Y
ou'll find an annotation
@SpringBootApplication
on the launch class of each
Spring Boot
app that contains annotations
@EnableAutoConfiguration
are used to automate assembly.
This annotation is done by importing
AutoConfigurationImportSelector
which has a method,
selectImports
which scans the
META-INF/spring.factories
files in all jar packages to load the specific implementation classes inside for automatic assembly.
A class is specified in the
META-INF/spring.factories
file of the
spring-boot-autoconfigure
jar package to load database configuration information, which is
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
HikariDataSource
is used here as a custom data source for the purpose of understanding the configuration information in the secret configuration file.
@Configuration
public class DataSourceConfiguration {
@Autowired
DataSourceProperties properties;
@Bean
public DataSource dataSource() throws Exception{
String username = Des3.decryptThreeDESECB(properties.getUsername(),Des3.DES3KEY);
String password = Des3.decryptThreeDESECB(properties.getPassword(),Des3.DES3KEY);
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(properties.getDriverClassName());
dataSource.setJdbcUrl(properties.getUrl());
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
The profile information is as follows:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx
username: aMkeRCLWqNw=
password: rq-fzucH32I=
Specific decryption algorithms are not mentioned here, according to specific requirements to choose a reversible encryption algorithm on it.
Above is
W3Cschool编程狮
about
why the configuration file encrypted database configuration information, Spring Boot can still successfully connect to
the database related to the introduction, I hope to help you.