网上好多处理动态密码或密码加密都是用 【 jasypt 】与 阿里的 【 Druid 】,但这些都无办法满足大部份的公司的安全要求,很多公司要求密码单独方在服务器上的一个地方,并且文件或者密码字符串被加密。在这样的需求下,密码的处理都必须要程序员自己在程序中进行处理,以下两种方式是折腾多天后的可行的两种处理方法。
密码直接放在一个类里面,大家想如何处理就如何处理。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.badtree</groupId>
<artifactId>badtree</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>badtree</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<!-- local library -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>6</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/ojdbc-6.jar</systemPath>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
PasswordUtil.java
package org.badtree.utils;
import org.springframework.stereotype.Component;
@Component
public class PasswordUtil {
public String getPassowrd() {
return "123456";
}
}
application.properties
org.badtree.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
org.badtree.datasource.url = jdbc:oracle:thin:@localhost:1521:badtreedb
org.badtree.datasource.username = badtree
1. XML配置方式
application.java
package org.badtree;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(locations={"classpath:applicationContext.xml"})
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
ConfigurableApplicationContext context = app.run(args);
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="driverClassName" value="${org.badtree.datasource.driver-class-name}"/>
<property name="jdbcUrl" value="${org.badtree.datasource.url}"/>
<property name="username" value="${org.badtree.datasource.username}"/>
<property name="password" value="#{passwordUtil.getPassowrd()}"/>
</bean>
<bean id="passwordUtil" class="org.badtree.utils.PasswordUtil" />
</beans>
2. Java 代码注册Bean
application.java
package org.badtree;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
ConfigurableApplicationContext context = app.run(args);
}
}
DataSourceConfig.java
package org.badtree.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.badtree.utils.PasswordUtil;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
public class DataSourceConfig {
@Value("${org.badtree.datasource.driver-class-name}")
private String driverClassName;
@Value("${org.badtree.datasource.url}")
private String jdbcUrl;
@Value("${org.badtree.datasource.username}")
private String username;
@Autowired
private PasswordUtil passwordUtil;
@Bean
public DataSource dataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUsername(username);
dataSource.setPassword(passwordUtil.getPassowrd());
return dataSource;
}
}