Encrypt tool and Decrypt

This commit is contained in:
xiaoyu830411
2012-08-31 13:57:44 +08:00
parent 6290be2561
commit e19def644d
5 changed files with 41 additions and 33 deletions
@@ -67,10 +67,24 @@ import java.util.Properties;
* <property name="connectionProperties" value="config.file=http://localhost:8080/remote.propreties; />
* </bean>
*
* [使用系统属性配置远程文件]
* java -Ddruid.config.file=file:/home/test/my.properties ...
*
* 远程配置文件格式:
* 1. 其他的属性KEY请查看 @see com.alibaba.druid.pool.DruidDataSourceFactory
* 2. config filter 相关设置:
* config.xxxxx=yyyyy
* #远程文件路径
* config.file=http://xxxxx(http://开头或者file:开头)
*
* #AES解密, key不指定, 使用默认的
* config.decrypt=AES
* config.decrypt.key=abcdfeg
*
* #RSA解密, Key不指定, 使用默认的
* config.decrypt=RSA
* config.decrypt.key=密钥字符串
* config.decrypt.keyFile=密钥文件路径
* config.decrypt.x509File=证书路径
*
* </pre>
*/
@@ -78,9 +92,11 @@ public class ConfigFilter extends FilterAdapter {
private static Log log = LogFactory.getLog(ConfigFilter.class);
public final static String CONFIG_FILE = "config.file";
public static final String CONFIG_FILE = "config.file";
public final static String CONFIG_DECRYPT = "config.decrypt";
public static final String CONFIG_DECRYPT = "config.decrypt";
public static final String SYS_PROP_CONFIG_FILE = "druid.config.file";
public ConfigFilter(){
}
@@ -97,7 +113,12 @@ public class ConfigFilter extends FilterAdapter {
//获取远程配置文件
String protocol = connectinProperties.getProperty(CONFIG_FILE);
if (protocol != null) {
//如果系统参数有指定
if (protocol == null) {
protocol = System.getProperty(SYS_PROP_CONFIG_FILE);
}
if (protocol != null && protocol.length() > 0) {
if (log.isDebugEnabled()) {
log.debug("Config file will be load from [" + protocol + "].");
}
@@ -23,7 +23,6 @@ public class FileConfigLoader implements ConfigLoader {
private static Log LOG = LogFactory.getLog(FileConfigLoader.class);
public static final String PROTOCOL_PREFIX = "file:";
public static final String SYS_PROP_CONFIG_FILE = "druid.config.file";
@Override
public String getId() {
@@ -80,11 +79,7 @@ public class FileConfigLoader implements ConfigLoader {
String getFilePath(String protocol) {
String filePath = null;
if(protocol == null) {
filePath = System.getProperty(SYS_PROP_CONFIG_FILE);
} else {
filePath = protocol.substring(PROTOCOL_PREFIX.length());
}
filePath = protocol.substring(PROTOCOL_PREFIX.length());
return filePath;
}
@@ -95,10 +90,6 @@ public class FileConfigLoader implements ConfigLoader {
@Override
public boolean isSupported(String protocol) {
if (protocol == null) {
return System.getProperty(SYS_PROP_CONFIG_FILE) != null;
}
return protocol.toLowerCase().startsWith(PROTOCOL_PREFIX);
}
}
@@ -30,6 +30,21 @@ public class ConfigFilterTest extends ConfigFileGenerator {
}
}
@Test
public void testInitRemoteConfigFileBySystemProperty() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("config");
System.setProperty(ConfigFilter.SYS_PROP_CONFIG_FILE, FileConfigLoader.PROTOCOL_PREFIX + this.filePath);
try {
dataSource.init();
Assert.assertEquals("The username is " + dataSource.getUsername(), "test1", dataSource.getUsername());
} finally {
System.clearProperty(ConfigFilter.SYS_PROP_CONFIG_FILE);
JdbcUtils.close(dataSource);
}
}
@Test
public void testInitInvalidRemoteConfigFile() throws SQLException {
@@ -16,10 +16,6 @@ public class FileConfigLoaderTest {
isSupported = configLoader.isSupported("jdbc:/opt/mytest");
Assert.assertFalse("The value is " + isSupported + ", not false", isSupported);
System.setProperty(FileConfigLoader.SYS_PROP_CONFIG_FILE, "/opt/mytest");
isSupported = configLoader.isSupported(null);
Assert.assertTrue("The value is " + isSupported + ", not true", isSupported);
}
@Test
@@ -27,9 +23,5 @@ public class FileConfigLoaderTest {
FileConfigLoader configLoader = new FileConfigLoader();
String filePath = configLoader.getFilePath(FileConfigLoader.PROTOCOL_PREFIX + "/opt/test/test.properties");
Assert.assertEquals("The path is " + filePath, filePath, "/opt/test/test.properties");
System.setProperty(FileConfigLoader.SYS_PROP_CONFIG_FILE, "/opt/mytest");
filePath = configLoader.getFilePath(null);
Assert.assertEquals("The path is " + filePath, filePath, "/opt/mytest");
}
}
@@ -20,17 +20,6 @@ public class FileConfigLoaderTest1 extends ConfigFileGenerator {
Assert.assertEquals("The value is " + p.getProperty("username") + ", not test1", "test1", p.getProperty("username"));
}
@Test
public void testLoadConfigBySystemProperty() {
System.setProperty(FileConfigLoader.SYS_PROP_CONFIG_FILE, this.filePath);
FileConfigLoader configLoader = new FileConfigLoader();
Properties p = configLoader.loadConfig(null);
Assert.assertNotNull("The properties is null", p);
Assert.assertEquals("The value is " + p.getProperty("username") + ", not test1", "test1", p.getProperty("username"));
}
@Test
public void testLoadConfigByNotExistFile() {
FileConfigLoader configLoader = new FileConfigLoader();