mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Encypt tool and Decrypt
This commit is contained in:
@@ -15,225 +15,210 @@
|
||||
*/
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
import com.alibaba.druid.filter.FilterAdapter;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.Base64;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.support.security.decryptor.DecryptException;
|
||||
import com.alibaba.druid.support.security.decryptor.Decrypter;
|
||||
import com.alibaba.druid.support.security.decryptor.DecrypterFactory;
|
||||
import com.alibaba.druid.support.security.decryptor.SensitiveParameters;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 这个类主要是负责两个事情, 解密, 和下载远程的配置文件
|
||||
* [解密]
|
||||
*
|
||||
* DruidDataSource dataSource = new DruidDataSource();
|
||||
* //dataSource.setXXX 其他设置
|
||||
* //下面两步很重要
|
||||
* //启用config filter
|
||||
* dataSource.setFilters("config");
|
||||
* //使用RSA解密(使用默认密钥)
|
||||
* dataSource.setConnectionPropertise("config.decrypt=RSA");
|
||||
* dataSource.setPassword("加密的密文");
|
||||
*
|
||||
* [远程配置文件]
|
||||
* DruidDataSource dataSource = new DruidDataSource();
|
||||
* //下面两步很重要
|
||||
* //启用config filter
|
||||
* dataSource.setFilters("config");
|
||||
* //使用RSA解密(使用默认密钥)
|
||||
* dataSource.setConnectionPropertise("config.file=http://localhost:8080/remote.propreties;");
|
||||
*
|
||||
* [Spring的配置解密]
|
||||
*
|
||||
* <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
|
||||
* <property name="password" value="加密的密文" />
|
||||
* <!-- 其他的属性设置 -->
|
||||
* <property name="filters" value="config" />
|
||||
* <property name="connectionProperties" value="config.decrypt=RSA" />
|
||||
* </bean>
|
||||
*
|
||||
* [Spring的配置远程配置文件]
|
||||
*
|
||||
* <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
|
||||
* <property name="filters" value="config" />
|
||||
* <property name="connectionProperties" value="config.file=http://localhost:8080/remote.propreties; />
|
||||
* </bean>
|
||||
*
|
||||
* 远程配置文件格式:
|
||||
* 1. 其他的属性KEY请查看 @see com.alibaba.druid.pool.DruidDataSourceFactory
|
||||
* 2. config filter 相关设置:
|
||||
* config.xxxxx=yyyyy
|
||||
*
|
||||
* </pre>
|
||||
*/
|
||||
public class ConfigFilter extends FilterAdapter {
|
||||
|
||||
public final static String URL_PREFIX = "druid-configFile=";
|
||||
public final static String SYS_PROP_CONFIG_FILE = "druid.config.file";
|
||||
public final static String SYS_PROP_CONFIG_KEY = "druid.config.key";
|
||||
public final static String SYS_PROP_CONFIG_ENCRYPTED_PASSWORD = "druid.config.encryptedPassword";
|
||||
public final static String DEFAULT_ALGORITHM = "RSA";
|
||||
private static Log log = LogFactory.getLog(ConfigFilter.class);
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(ConfigFilter.class);
|
||||
public final static String CONFIG_FILE = "config.file";
|
||||
|
||||
private String file;
|
||||
|
||||
private Cipher cipher;
|
||||
private String algorithm;
|
||||
private String key;
|
||||
private String encryptedPassword;
|
||||
public final static String CONFIG_DECRYPT = "config.decrypt";
|
||||
|
||||
public ConfigFilter(){
|
||||
this.setAlgorithm(DEFAULT_ALGORITHM);
|
||||
|
||||
{
|
||||
String property = System.getProperty(SYS_PROP_CONFIG_KEY);
|
||||
if (property != null && property.length() != 0) {
|
||||
this.setKey(property);
|
||||
}
|
||||
}
|
||||
{
|
||||
String property = System.getProperty(SYS_PROP_CONFIG_ENCRYPTED_PASSWORD);
|
||||
if (property != null && property.length() != 0) {
|
||||
this.setEncryptedPassword(property);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getAlgorithm() {
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
public final void setAlgorithm(String algorithm) {
|
||||
this.algorithm = algorithm;
|
||||
try {
|
||||
this.cipher = Cipher.getInstance(algorithm);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("illegal algorithm", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getEncryptedPassword() {
|
||||
return encryptedPassword;
|
||||
}
|
||||
|
||||
public final void setEncryptedPassword(String encryptedPassword) {
|
||||
this.encryptedPassword = encryptedPassword;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public final void setKey(String key) {
|
||||
this.key = key;
|
||||
|
||||
byte[] bytes = Base64.base64ToByteArray(key);
|
||||
|
||||
try {
|
||||
if (cipher.getAlgorithm().startsWith("RSA")) {
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(bytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
|
||||
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("illegal key", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void init(DataSourceProxy dataSourceProxy) {
|
||||
if (!(dataSourceProxy instanceof DruidDataSource)) {
|
||||
LOG.error("ConfigLoader only support DruidDataSource");
|
||||
log.error("ConfigLoader only support DruidDataSource");
|
||||
}
|
||||
|
||||
Properties info = null;
|
||||
DruidDataSource dataSource = (DruidDataSource) dataSourceProxy;
|
||||
Properties connectinProperties = dataSource.getConnectProperties();
|
||||
|
||||
Properties properties = loadConfig(dataSource);
|
||||
//获取远程配置文件
|
||||
String protocol = connectinProperties.getProperty(CONFIG_FILE);
|
||||
|
||||
if (encryptedPassword != null) {
|
||||
try {
|
||||
byte[] passwordBytes = Base64.base64ToByteArray(encryptedPassword);
|
||||
byte[] decryptedBytes = cipher.doFinal(passwordBytes);
|
||||
String decryptedPassword = new String(decryptedBytes, "ISO-8859-1");
|
||||
dataSource.setPassword(decryptedPassword);
|
||||
} catch (Exception e) {
|
||||
LOG.error("decrypt password error", e);
|
||||
if (protocol != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Config file will be load from [" + protocol + "].");
|
||||
}
|
||||
|
||||
//获取配置文件内容
|
||||
ConfigLoader configLoader = ConfigLoaderFactory.getConfigLoader(protocol);
|
||||
|
||||
if (configLoader == null) {
|
||||
throw new IllegalArgumentException("Druid doesn't support the [config.file=" + protocol + "] to load remote config file.");
|
||||
}
|
||||
|
||||
info = configLoader.loadConfig(protocol);
|
||||
|
||||
if (info == null) {
|
||||
throw new IllegalArgumentException("Cannot load remote config file from the [config.file=" + protocol + "].");
|
||||
}
|
||||
} else {
|
||||
info = new Properties();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("No remote config file.");
|
||||
}
|
||||
}
|
||||
|
||||
if (properties == null) {
|
||||
if (encryptedPassword == null) {
|
||||
LOG.error("load config error, return null");
|
||||
//判断是否需要解密,如果需要就进行解密行动
|
||||
String decrypterId = info.getProperty(CONFIG_DECRYPT);
|
||||
boolean isRemotedSecurityConfig = true;
|
||||
|
||||
if (decrypterId == null) {
|
||||
decrypterId = connectinProperties.getProperty(CONFIG_DECRYPT);
|
||||
isRemotedSecurityConfig = false;
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Get decrypt method " + decrypterId + " from local config.");
|
||||
}
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Get decrypt method " + decrypterId + " from remote config.");
|
||||
}
|
||||
}
|
||||
|
||||
if (decrypterId != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Use [" + decrypterId + "] to decrypt sensitive parameters.");
|
||||
}
|
||||
|
||||
try {
|
||||
Decrypter decrypter = DecrypterFactory.getDecrypter(decrypterId);
|
||||
if (decrypter == null) {
|
||||
throw new IllegalArgumentException("Druid doesn't support the decrypter [config.decrypt=" + decrypterId + "].");
|
||||
}
|
||||
|
||||
Properties securityInfo = isRemotedSecurityConfig
|
||||
? info
|
||||
: connectinProperties;
|
||||
|
||||
SensitiveParameters originalParameter = getSensitiveParameters(dataSource, info);
|
||||
SensitiveParameters parameters = decrypter.decrypt(originalParameter, securityInfo);
|
||||
|
||||
info.setProperty(DruidDataSourceFactory.PROP_URL, parameters.getUrl());
|
||||
info.setProperty(DruidDataSourceFactory.PROP_USERNAME, parameters.getUsername());
|
||||
info.setProperty(DruidDataSourceFactory.PROP_PASSWORD, parameters.getPassword());
|
||||
} catch (DecryptException e) {
|
||||
throw new IllegalArgumentException("Failed to decrypt.", e);
|
||||
}
|
||||
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("No decrypt.");
|
||||
}
|
||||
}
|
||||
|
||||
//没有任何配置
|
||||
if (info.size() == 0) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("No config.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (this.cipher != null) {
|
||||
String password = properties.getProperty(DruidDataSourceFactory.PROP_PASSWORD);
|
||||
if (password != null && password.length() != 0) {
|
||||
byte[] passwordBytes = Base64.base64ToByteArray(password);
|
||||
byte[] decryptedBytes = cipher.doFinal(passwordBytes);
|
||||
|
||||
String decryptedPassword = new String(decryptedBytes, "ISO-8859-1");
|
||||
properties.put(DruidDataSourceFactory.PROP_PASSWORD, decryptedPassword);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("decrypt password error", e);
|
||||
}
|
||||
|
||||
try {
|
||||
DruidDataSourceFactory.config(dataSource, properties);
|
||||
} catch (Exception e) {
|
||||
LOG.error("config dataSource error", e);
|
||||
DruidDataSourceFactory.config(dataSource, info);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalArgumentException("Config DataSource error.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Properties loadConfig(DruidDataSource dataSource) {
|
||||
String filePath = this.file;
|
||||
/**
|
||||
* 如果配置文件中没有值, 就取默认值
|
||||
* @param dataSource
|
||||
* @param info
|
||||
* @return
|
||||
*/
|
||||
SensitiveParameters getSensitiveParameters(DruidDataSource dataSource, Properties info) {
|
||||
String username = info.getProperty(DruidDataSourceFactory.PROP_USERNAME);
|
||||
String password = info.getProperty(DruidDataSourceFactory.PROP_PASSWORD);
|
||||
String url = info.getProperty(DruidDataSourceFactory.PROP_URL);
|
||||
|
||||
// jdbc:druid-config:
|
||||
if (file == null && dataSource.getUrl() != null && dataSource.getUrl().startsWith(ConfigFilter.URL_PREFIX)) {
|
||||
filePath = dataSource.getUrl().substring(ConfigFilter.URL_PREFIX.length());
|
||||
if (username == null) {
|
||||
username = dataSource.getUsername();
|
||||
}
|
||||
|
||||
if (filePath == null) {
|
||||
filePath = System.getProperty(SYS_PROP_CONFIG_FILE);
|
||||
if (password == null) {
|
||||
password = dataSource.getPassword();
|
||||
}
|
||||
|
||||
if (filePath == null) {
|
||||
if (this.encryptedPassword == null) {
|
||||
LOG.error("load config error, file is null");
|
||||
}
|
||||
return null;
|
||||
if (url == null) {
|
||||
url = dataSource.getUrl();
|
||||
}
|
||||
|
||||
Properties properties = new Properties();
|
||||
return new SensitiveParameters(url, username, password);
|
||||
}
|
||||
|
||||
InputStream inStream = null;
|
||||
try {
|
||||
boolean xml = false;
|
||||
if (filePath.startsWith("http://") || filePath.startsWith("https://")) {
|
||||
URL url = new URL(filePath);
|
||||
inStream = url.openStream();
|
||||
public int hashCode() {
|
||||
return this.getClass().hashCode();
|
||||
}
|
||||
|
||||
xml = url.getPath().endsWith(".xml");
|
||||
} else {
|
||||
File file = new File(filePath);
|
||||
if (file.exists()) {
|
||||
inStream = new FileInputStream(file);
|
||||
} else {
|
||||
inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
|
||||
}
|
||||
|
||||
xml = filePath.endsWith(".xml");
|
||||
}
|
||||
|
||||
if (inStream == null) {
|
||||
LOG.error("load config file error, file : " + filePath);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (xml) {
|
||||
properties.loadFromXML(inStream);
|
||||
} else {
|
||||
properties.load(inStream);
|
||||
}
|
||||
|
||||
return properties;
|
||||
} catch (Exception ex) {
|
||||
LOG.error("load config file error, file : " + filePath, ex);
|
||||
return null;
|
||||
} finally {
|
||||
JdbcUtils.close(inStream);
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String getFile() {
|
||||
return file;
|
||||
return obj.getClass().equals(ConfigFilter.class);
|
||||
}
|
||||
|
||||
public void setFile(String file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public Cipher getCipher() {
|
||||
return cipher;
|
||||
}
|
||||
|
||||
public void setCipher(Cipher cipher) {
|
||||
this.cipher = cipher;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 配置文件装载器.
|
||||
*
|
||||
* 要实现自己的装载器有如下要求.
|
||||
* 1. 一定要有自己的协议格式, 例如是由 my-protocol: 开头的protocol是由我支持的.
|
||||
* public isSupported(String protocol) {
|
||||
* return protocol.startsWith("my-protocol:");
|
||||
* }
|
||||
*
|
||||
* 2. 要在class path的根目录下的ConfigLoader.properties注册(如果没有这个文件, 可以自己创建一个). 例如:
|
||||
* WEB-INF/classes/ConfigLoader.properties [格式如下]:
|
||||
* ConfigLoader.myConfigLoader=com.xxx.xxx.xx.MyConfigLoader
|
||||
*
|
||||
* 3. 配置<code>DruidDataSource</code>的时候的URL就要设置为装载器的协议格式
|
||||
* DruidDataSource dataSource = new DruidDataSource();
|
||||
* dataSource.setUrl("my-protocol:http://xxxxxx/");
|
||||
*
|
||||
* </pre>
|
||||
* @see com.alibaba.druid.filter.config.impl.FileConfigLoader
|
||||
* @see com.alibaba.druid.filter.config.impl.HttpConfigLoader
|
||||
*
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public interface ConfigLoader {
|
||||
|
||||
/**
|
||||
* 唯一的ID
|
||||
* @return
|
||||
*/
|
||||
public String getId();
|
||||
|
||||
/**
|
||||
* 获得配置内容, 如果没有, 返回<code>null</code>.
|
||||
* @param protocol
|
||||
* @return
|
||||
*/
|
||||
public Properties loadConfig(String protocol);
|
||||
|
||||
/**
|
||||
* 是否支持该协议, 如果支持, 将会使用该装载器获取配置文件
|
||||
* @param protocol
|
||||
* @return
|
||||
*/
|
||||
public boolean isSupported(String protocol);
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 如果有扩展的装载器.
|
||||
* 可以放到classes目录下的ConfigLoader.properties
|
||||
*
|
||||
* ConfigLoader.properties 内容如下:
|
||||
* ConfigLoader.x=com.xx.xxx.xx.MyConfigLoader
|
||||
*
|
||||
* </pre>
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class ConfigLoaderFactory {
|
||||
|
||||
private static Log log = LogFactory.getLog(ConfigLoaderFactory.class);
|
||||
|
||||
private static ConcurrentHashMap<String, ConfigLoader> configLoaders = new ConcurrentHashMap<String, ConfigLoader>();
|
||||
|
||||
static {
|
||||
//加载默认的装载器
|
||||
initConfigLoader("META-INF/druid-config-loader-default.properties", ConfigLoaderFactory.class.getClassLoader());
|
||||
|
||||
//加载扩展的装载器
|
||||
initConfigLoader("druid-config-loader.properties", ClassLoader.getSystemClassLoader());
|
||||
|
||||
if (ClassLoader.getSystemClassLoader() != Thread.currentThread().getContextClassLoader()) {
|
||||
initConfigLoader("ConfigLoader.properties", Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
}
|
||||
|
||||
private ConfigLoaderFactory() {}
|
||||
|
||||
static void initConfigLoader(String resource, ClassLoader classLoader) {
|
||||
if (resource == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
InputStream inStream = null;
|
||||
|
||||
try {
|
||||
inStream = classLoader.getResourceAsStream(resource);
|
||||
if (inStream == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Properties p = new Properties();
|
||||
p.load(inStream);
|
||||
|
||||
for (String key : p.stringPropertyNames()) {
|
||||
String clazzName = p.getProperty(key);
|
||||
System.out.println(clazzName);
|
||||
Class clazz = classLoader.loadClass(clazzName);
|
||||
ConfigLoader configLoader = (ConfigLoader) clazz.newInstance();
|
||||
|
||||
addConfigLoader(configLoader);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Fail to init config loaders.", e);
|
||||
} finally {
|
||||
JdbcUtils.close(inStream);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 protocol 获得装载器. 如果没有返回 <code>null</code>.
|
||||
* @param protocol
|
||||
* @return 如果没有找到返回<code>null</code>
|
||||
*/
|
||||
public static ConfigLoader getConfigLoader(String protocol) {
|
||||
for (ConfigLoader configLoader : configLoaders.values()) {
|
||||
if (configLoader.isSupported(protocol)) {
|
||||
return configLoader;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void addConfigLoader(ConfigLoader configLoader) {
|
||||
if (configLoaders.putIfAbsent(configLoader.getId(), configLoader) != null) {
|
||||
log.warn("Duplicate config loader [" + configLoader.getId() + "]");
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Add config loader [" + configLoader.getId() + " = " + configLoader.getClass() + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ConfigLoader[] getConfigLoaders() {
|
||||
return configLoaders.values().toArray(new ConfigLoader[0]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
import com.alibaba.druid.util.Base64;
|
||||
|
||||
public class ConfigTool {
|
||||
public final static int DEFAULT_KEY_SIZE = 1024;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
String command = args[0];
|
||||
|
||||
if ("-gen".equals(command)) {
|
||||
int keySize = DEFAULT_KEY_SIZE;
|
||||
|
||||
if (args.length > 1) {
|
||||
keySize = Integer.parseInt(args[1]);
|
||||
}
|
||||
|
||||
Map<String, String> result = gen(keySize);
|
||||
print(result);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ("-encrypt".equals(command)) {
|
||||
String privateKey = args[1];
|
||||
String text = args[2];
|
||||
|
||||
Map<String, String> result = encrypt(privateKey, text);
|
||||
print(result);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ("-decrypt".equals(command)) {
|
||||
String publicKey = args[1];
|
||||
String text = args[2];
|
||||
|
||||
Map<String, String> result = decrypt(publicKey, text);
|
||||
print(result);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(Map<String, String> map) {
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
System.out.print(entry.getKey());
|
||||
System.out.print(" : ");
|
||||
System.out.println();
|
||||
System.out.print(entry.getValue());
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, String> decrypt(String publicKeyString, String cipherText) throws Exception {
|
||||
Map<String, String> map = new LinkedHashMap<String, String>();
|
||||
|
||||
byte[] publicKeyBytes = Base64.base64ToByteArray(publicKeyString);
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
|
||||
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
||||
|
||||
byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
|
||||
byte[] plainBytes = cipher.doFinal(cipherBytes);
|
||||
String plainText = new String(plainBytes);
|
||||
|
||||
map.put("result", plainText);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<String, String> encrypt(String privateKeyString, String plain) throws Exception {
|
||||
Map<String, String> map = new LinkedHashMap<String, String>();
|
||||
|
||||
byte[] privateKeyBytes = Base64.base64ToByteArray(privateKeyString);
|
||||
PKCS8EncodedKeySpec x509KeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(x509KeySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
||||
|
||||
byte[] plainBytes = plain.getBytes();
|
||||
byte[] cipherBytes = cipher.doFinal(plainBytes);
|
||||
|
||||
String cipherText = Base64.byteArrayToBase64(cipherBytes);
|
||||
|
||||
map.put("result", cipherText);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static Map<String, String> gen(int keySize) throws Exception {
|
||||
Map<String, String> map = new LinkedHashMap<String, String>();
|
||||
|
||||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
|
||||
keyPairGen.initialize(512, new SecureRandom());
|
||||
|
||||
KeyPair keyPair = keyPairGen.generateKeyPair();
|
||||
|
||||
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
|
||||
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
|
||||
|
||||
map.put("privateKey", Base64.byteArrayToBase64(privateKey.getEncoded()));
|
||||
map.put("publicKey", Base64.byteArrayToBase64(publicKey.getEncoded()));
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigLoader;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 通过指定的配置文件路径, 加载该配置. 使用使用NFS共享文件等情况.
|
||||
* DruidDataSource的connectionProperties必须是 <b>config.file=file:</b> 开头.
|
||||
* 注意: OSGI下的跨bundle获取资源, 没有测试过
|
||||
* </pre>
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
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() {
|
||||
return PROTOCOL_PREFIX;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果没有找到配置文件, 返回<code>null</code>
|
||||
* @param protocol
|
||||
* @return 如果有找到配置文件返回<code>null</code>
|
||||
*/
|
||||
public Properties loadConfig(String protocol) {
|
||||
if (!isSupported(protocol)) {
|
||||
throw new IllegalArgumentException("The protocol [" + protocol + "] is not accepted. Protocol must start with [" + PROTOCOL_PREFIX + "].");
|
||||
}
|
||||
|
||||
if(LOG.isDebugEnabled()) {
|
||||
LOG.debug("Load config file[" + protocol + "] by [" + getId() + "]");
|
||||
}
|
||||
|
||||
Properties properties = new Properties();
|
||||
InputStream inStream = null;
|
||||
|
||||
try {
|
||||
String filePath = getFilePath(protocol);
|
||||
File file = new File(filePath);
|
||||
|
||||
if (file.exists()) {
|
||||
inStream = new FileInputStream(file);
|
||||
} else {
|
||||
inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
|
||||
}
|
||||
|
||||
if (inStream == null) {
|
||||
LOG.error("load config file error, file : " + protocol);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isXml(filePath)) {
|
||||
properties.loadFromXML(inStream);
|
||||
} else {
|
||||
properties.load(inStream);
|
||||
}
|
||||
|
||||
return properties;
|
||||
} catch (Exception ex) {
|
||||
LOG.error("load config file error, file : " + protocol, ex);
|
||||
return null;
|
||||
} finally {
|
||||
JdbcUtils.close(inStream);
|
||||
}
|
||||
}
|
||||
|
||||
String getFilePath(String protocol) {
|
||||
String filePath = null;
|
||||
|
||||
if(protocol == null) {
|
||||
filePath = System.getProperty(SYS_PROP_CONFIG_FILE);
|
||||
} else {
|
||||
filePath = protocol.substring(PROTOCOL_PREFIX.length());
|
||||
}
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
boolean isXml(String filePath) {
|
||||
return filePath.endsWith(".xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(String protocol) {
|
||||
if (protocol == null) {
|
||||
return System.getProperty(SYS_PROP_CONFIG_FILE) != null;
|
||||
}
|
||||
|
||||
return protocol.toLowerCase().startsWith(PROTOCOL_PREFIX);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.alibaba.druid.filter.config.impl;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigLoader;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 通过HTTP方式获取资源文件.
|
||||
* DruidDataSource的connectionProperties必须是 <b>config.file=http://</b> 开头.
|
||||
* </pre>
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class HttpConfigLoader implements ConfigLoader {
|
||||
private static Log LOG = LogFactory.getLog(HttpConfigLoader.class);
|
||||
|
||||
public final static String PROTOCOL_PREFIX = "http://";
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return PROTOCOL_PREFIX;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果没有找到资源文件, 返回<code>null</code>
|
||||
* @param protocol
|
||||
* @return 如果没有找到资源文件, 返回<code>null</code>
|
||||
*/
|
||||
public Properties loadConfig(String protocol) {
|
||||
if (!isSupported(protocol)) {
|
||||
throw new IllegalArgumentException("The protocol [" + protocol + "] is not accepted. Protocol must start with [" + PROTOCOL_PREFIX + "].");
|
||||
}
|
||||
|
||||
if(LOG.isDebugEnabled()) {
|
||||
LOG.debug("Load config file[" + protocol + "] by [" + getId() + "]");
|
||||
}
|
||||
|
||||
Properties properties = new Properties();
|
||||
|
||||
InputStream inStream = null;
|
||||
try {
|
||||
URL url = getUrl(protocol);
|
||||
inStream = url.openStream();
|
||||
|
||||
if (inStream == null) {
|
||||
LOG.error("load config file error, file : " + protocol);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isXml(url)) {
|
||||
properties.loadFromXML(inStream);
|
||||
} else {
|
||||
properties.load(inStream);
|
||||
}
|
||||
|
||||
return properties;
|
||||
} catch (Exception ex) {
|
||||
LOG.error("load config file error, file : " + protocol, ex);
|
||||
return null;
|
||||
} finally {
|
||||
JdbcUtils.close(inStream);
|
||||
}
|
||||
}
|
||||
|
||||
URL getUrl(String protocol) throws MalformedURLException {
|
||||
return new URL(protocol.substring(PROTOCOL_PREFIX.length() - 7));
|
||||
}
|
||||
|
||||
boolean isXml(URL url) {
|
||||
return url.getPath().endsWith(".xml");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSupported(String protocol) {
|
||||
if (protocol == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return protocol.toLowerCase().startsWith(PROTOCOL_PREFIX);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
/**
|
||||
* 加密出错
|
||||
*
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class DecryptException extends Exception {
|
||||
public DecryptException() {
|
||||
super(); //To change body of overridden methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
public DecryptException(String s) {
|
||||
super(s); //To change body of overridden methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
public DecryptException(String s, Throwable throwable) {
|
||||
super(s, throwable); //To change body of overridden methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
public DecryptException(Throwable throwable) {
|
||||
super(throwable); //To change body of overridden methods use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
@@ -221,16 +221,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
protected AtomicLong createCount = new AtomicLong();
|
||||
protected AtomicLong destroyCount = new AtomicLong();
|
||||
|
||||
protected Decrypter decrypter;
|
||||
|
||||
public Decrypter getDecrypter() {
|
||||
return decrypter;
|
||||
}
|
||||
|
||||
public void setDecrypter(Decrypter decrypter) {
|
||||
this.decrypter = decrypter;
|
||||
}
|
||||
|
||||
public boolean isOracle() {
|
||||
return isOracle;
|
||||
}
|
||||
@@ -934,9 +924,9 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
this.jdbcUrl = jdbcUrl;
|
||||
|
||||
if (jdbcUrl.startsWith(ConfigFilter.URL_PREFIX)) {
|
||||
this.filters.add(new ConfigFilter());
|
||||
}
|
||||
// if (jdbcUrl.startsWith(ConfigFilter.URL_PREFIX)) {
|
||||
// this.filters.add(new ConfigFilter());
|
||||
// }
|
||||
}
|
||||
|
||||
public String getDriverClassName() {
|
||||
@@ -1247,18 +1237,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
}
|
||||
|
||||
Decrypter decrypter1 = this.getDecrypter();
|
||||
if (decrypter1 != null) {
|
||||
try {
|
||||
SensitiveParameters parameters = decrypter1.decrypt(new SensitiveParameters(url, user, password));
|
||||
url = parameters.getUrl();
|
||||
user = parameters.getUsername();
|
||||
password = parameters.getPassword();
|
||||
} catch (DecryptException e) {
|
||||
throw new SQLException("Failed to decrypt sensitive parameters.", e);
|
||||
}
|
||||
}
|
||||
|
||||
Properties physicalConnectProperties = new Properties();
|
||||
if (connectProperties != null) {
|
||||
physicalConnectProperties.putAll(connectProperties);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import com.alibaba.druid.pool.Decrypter;
|
||||
import com.alibaba.druid.util.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 加密抽象类
|
||||
@@ -17,6 +17,8 @@ public abstract class AbstractDecrypter implements Decrypter {
|
||||
*/
|
||||
public static final String KEY_PADDING = "FOLLOW YOUR HEART. YOU CAN DO BEST THAN ANY ONE.";
|
||||
|
||||
public final static String KEY = "config.decrypt.key";
|
||||
|
||||
/**
|
||||
* 传入密文, 返回明文
|
||||
* @param cipher 解密器
|
||||
|
||||
@@ -1,27 +1,42 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import com.alibaba.druid.pool.DecryptException;
|
||||
import com.alibaba.druid.pool.SensitiveParameters;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* AES 解密器
|
||||
* <pre>
|
||||
* AES 解密器, 如果不指定密钥, 使用默认的密钥.
|
||||
* 通过一下方式指定密钥:
|
||||
* Properties info = new Properties();
|
||||
* info.put("config.decrypt.key", "密钥");
|
||||
*
|
||||
* String 明文 = new AesDecrypter().decrypt("密文", info);
|
||||
* </pre>
|
||||
*
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class AesDecrypter extends AbstractDecrypter {
|
||||
private static Log log = LogFactory.getLog(AesDecrypter.class);
|
||||
|
||||
private final static int KEY_LENGTH = 16; //128
|
||||
|
||||
private volatile SecretKeySpec spec;
|
||||
public String getId() {
|
||||
return "AES";
|
||||
}
|
||||
|
||||
public SensitiveParameters decrypt(SensitiveParameters parameters) throws DecryptException {
|
||||
SecretKeySpec spec = getSpec();
|
||||
if (spec == null) {
|
||||
throw new DecryptException("No key.");
|
||||
}
|
||||
/**
|
||||
* 密钥 放在 <code>Properties</code> 参数中, key 是 config.decrypt.key, 如果不指定密钥, 将使用默认密钥
|
||||
* @param parameters 密文参数
|
||||
* @param info
|
||||
* @return
|
||||
* @throws DecryptException
|
||||
*/
|
||||
public SensitiveParameters decrypt(SensitiveParameters parameters, Properties info) throws DecryptException {
|
||||
SecretKeySpec spec = getSpec(info);
|
||||
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES");
|
||||
@@ -35,24 +50,25 @@ public class AesDecrypter extends AbstractDecrypter {
|
||||
}
|
||||
}
|
||||
|
||||
protected SecretKeySpec getSpec() {
|
||||
if (this.spec == null) {
|
||||
setKey("");
|
||||
SecretKeySpec getSpec(Properties info) {
|
||||
String key = null;
|
||||
|
||||
if (info != null) {
|
||||
key = info.getProperty(KEY);
|
||||
}
|
||||
|
||||
return this.spec;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
if (key == null) {
|
||||
throw new IllegalArgumentException("The key cannot be null");
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Decrypt by default key");
|
||||
}
|
||||
key = "";
|
||||
}
|
||||
|
||||
if (key.length() < 16) {
|
||||
key = KEY_PADDING.substring(0, KEY_LENGTH - key.length()) + key;
|
||||
}
|
||||
|
||||
this.spec = new SecretKeySpec(key.getBytes(), "AES");
|
||||
return new SecretKeySpec(key.getBytes(), "AES");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
/**
|
||||
* 加密出错
|
||||
*
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class DecryptException extends Exception {
|
||||
public DecryptException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DecryptException(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public DecryptException(String s, Throwable throwable) {
|
||||
super(s, throwable);
|
||||
}
|
||||
|
||||
public DecryptException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
}
|
||||
+19
-18
@@ -1,23 +1,18 @@
|
||||
package com.alibaba.druid.pool;
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 解密敏感参数
|
||||
* 使用解密器 只要简单实现这个类就可以了。
|
||||
*
|
||||
* public class MyDecrypter implement Decrypter {
|
||||
* public SensitiveParameters decrypt(SensitiveParameters parameters) throws DecryptException {
|
||||
* String url = ....;
|
||||
* String username = ....;
|
||||
* String password = ....;
|
||||
*
|
||||
* return new SensitiveParameters(url, username, password);
|
||||
* }
|
||||
* }
|
||||
* [代码例子]
|
||||
*
|
||||
* DruidDataSource dataSource = new DruidDataSource();
|
||||
* //设置DataSource的其他参数
|
||||
* dataSource.setDecrypter(new MyDecrypter);
|
||||
* dataSource.setFilters("config");
|
||||
* dataSource.setUsername("test");
|
||||
* dataSource.setPassword("加密的密码");
|
||||
* dataSource.setConnectionProperties("config.decrypt=RSA");
|
||||
*
|
||||
* [下面是Spring的配置]
|
||||
*
|
||||
@@ -25,25 +20,31 @@ package com.alibaba.druid.pool;
|
||||
* <property name="url" value="${jdbc_url}" />
|
||||
* <property name="username" value="${jdbc_user}" />
|
||||
* <property name="password" value="${jdbc_password}" />
|
||||
* <b><property name="decrypter"><ref bean="myDecrypter"/></property></b>
|
||||
* <b><property name="filters" value="config" /></b>
|
||||
* <b><property name="connectionProperties" value="config.decrypt=RSA" /></b>
|
||||
* </bean>
|
||||
*
|
||||
* <bean id="myDecrypter" class="MyDecrypter"/>
|
||||
*
|
||||
* </pre>
|
||||
* <br/>
|
||||
* <b>默认下, Druid 已经实现了AES, DES, RSA, Blowfish(兼容JBOSS)等加解密. AES, DES 解密的密文必须是通过Druid Tool的加密工具生成的</b>
|
||||
* <b>默认下, Druid 已经实现了AES, RSA加解密. AES, RSA 密文必须是通过Druid Tool的加密工具生成的</b>
|
||||
* <b>获取默认解密方式可以通过 DecrypterFactory.getDecrypter(String id)</b>
|
||||
* <br/>
|
||||
* <b>使用加密工具: java -cp druid-x.x.x.jar com.alibaba.druid.support.security.tool.Main</b>
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public interface Decrypter {
|
||||
|
||||
/**
|
||||
* 标识符
|
||||
* @return
|
||||
*/
|
||||
public String getId();
|
||||
|
||||
/**
|
||||
* 返回一个解密后的(明文)参数
|
||||
* @param parameters 密文参数
|
||||
* @return
|
||||
* @throws DecryptException 解密失败会抛出该异常
|
||||
*/
|
||||
public SensitiveParameters decrypt(SensitiveParameters parameters) throws DecryptException;
|
||||
public SensitiveParameters decrypt(SensitiveParameters parameters, Properties info) throws DecryptException;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 通过 ID 获取解密器
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class DecrypterFactory {
|
||||
|
||||
private static Log log = LogFactory.getLog(DecrypterFactory.class);
|
||||
|
||||
private static ConcurrentHashMap<String, Decrypter> decrypters = new ConcurrentHashMap<String, Decrypter>();
|
||||
|
||||
static {
|
||||
RsaDecrypter rsa = new RsaDecrypter();
|
||||
AesDecrypter aes = new AesDecrypter();
|
||||
|
||||
decrypters.put(rsa.getId(), rsa);
|
||||
decrypters.put(aes.getId(), aes);
|
||||
}
|
||||
|
||||
private DecrypterFactory() {}
|
||||
|
||||
|
||||
// static void initDecrypters(String resource, ClassLoader classLoader) {
|
||||
// if (resource == null) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// InputStream inStream = null;
|
||||
//
|
||||
// try {
|
||||
// inStream = classLoader.getResourceAsStream(resource);
|
||||
// if (inStream == null) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// Properties p = new Properties();
|
||||
// p.load(inStream);
|
||||
//
|
||||
// for (String key : p.stringPropertyNames()) {
|
||||
// String clazzName = p.getProperty(key);
|
||||
// Class clazz = classLoader.loadClass(clazzName);
|
||||
// Decrypter decrypter = (Decrypter) clazz.newInstance();
|
||||
//
|
||||
// addDecrypter(decrypter);
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// log.warn("Fail to init config loaders.", e);
|
||||
// } finally {
|
||||
// JdbcUtils.close(inStream);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 通过 ID 获取解密器, 如果没有找到, 返回 <code>null</code>
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public static Decrypter getDecrypter(String id) {
|
||||
return decrypters.get(id);
|
||||
}
|
||||
|
||||
public static void addDecrypter(Decrypter decrypter) {
|
||||
Decrypter oldDecrypter = decrypters.putIfAbsent(decrypter.getId(), decrypter);
|
||||
if (oldDecrypter != null) {
|
||||
log.warn("Replace decrypter [" + decrypter.getId() + ", " + oldDecrypter.getClass() + "] with [" + decrypter.getClass() + "]");
|
||||
}
|
||||
}
|
||||
|
||||
public static Decrypter[] getDecrypters() {
|
||||
return decrypters.values().toArray(new Decrypter[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
import com.alibaba.druid.pool.DecryptException;
|
||||
import com.alibaba.druid.pool.SensitiveParameters;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.Base64;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -15,6 +14,7 @@ import java.security.PublicKey;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* RSA 解密器, 支持证书
|
||||
@@ -24,12 +24,32 @@ public class RsaDecrypter extends AbstractDecrypter {
|
||||
|
||||
private static Log log = LogFactory.getLog(RsaDecrypter.class);
|
||||
|
||||
private static final String DEFAULT_PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHGwq7q2RmwuRgKxBypQHw0mYu4BQZ3eMsTrdK8E6igRcxsobUC7uT0SoxIjl1WveWniCASejoQtn/BY6hVKWsCAwEAAQ==";
|
||||
public static final String KEY = "config.decrypt.key";
|
||||
public static final String KEY_FILE = "config.decrypt.keyFile";
|
||||
public static final String X509_FILE = "config.decrypt.x509File";
|
||||
|
||||
private volatile PublicKey publicKey;
|
||||
protected static final String DEFAULT_PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHGwq7q2RmwuRgKxBypQHw0mYu4BQZ3eMsTrdK8E6igRcxsobUC7uT0SoxIjl1WveWniCASejoQtn/BY6hVKWsCAwEAAQ==";
|
||||
|
||||
public SensitiveParameters decrypt(SensitiveParameters parameters) throws DecryptException {
|
||||
PublicKey publicKey = getPublicKey();
|
||||
public String getId() {
|
||||
return "RSA";
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 密钥 放在 <code>Properties</code> 参数中,
|
||||
* 如果是密钥字符串, key 是 config.decrypt.key
|
||||
* 如果是密钥文件, key 是 config.decrypt.keyFile
|
||||
* 如果是证书文件, key 是 config.decrypt.x509File
|
||||
*
|
||||
* 如果不指定密钥, 将使用默认密钥
|
||||
* </pre>
|
||||
* @param parameters 密文参数
|
||||
* @param info
|
||||
* @return
|
||||
* @throws DecryptException
|
||||
*/
|
||||
public SensitiveParameters decrypt(SensitiveParameters parameters, Properties info) throws DecryptException {
|
||||
PublicKey publicKey = getPublicKey(info);
|
||||
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
@@ -40,21 +60,53 @@ public class RsaDecrypter extends AbstractDecrypter {
|
||||
} catch (DecryptException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new DecryptException("Failed to decrypt JDBC parameters", e);
|
||||
throw new DecryptException("Failed to decrypt parameters", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected PublicKey getPublicKey() {
|
||||
if (this.publicKey == null) {
|
||||
setPublicKeyString(DEFAULT_PUBLIC_KEY_STRING);
|
||||
/**
|
||||
* 通过指定
|
||||
* @param info
|
||||
* @return
|
||||
*/
|
||||
PublicKey getPublicKey(Properties info) {
|
||||
String key = null;
|
||||
String publicKeyFile = null;
|
||||
String x509File = null;
|
||||
|
||||
if (info != null) {
|
||||
key = info.getProperty(KEY);
|
||||
publicKeyFile = info.getProperty(KEY_FILE);
|
||||
x509File = info.getProperty(X509_FILE);
|
||||
}
|
||||
|
||||
return this.publicKey;
|
||||
if (publicKeyFile != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Decrypt by public key file");
|
||||
}
|
||||
return getPublicKeyByPublicKeyFile(publicKeyFile);
|
||||
}
|
||||
|
||||
if (x509File != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Decrypt by X509 file");
|
||||
}
|
||||
return getPublicKeyByX509(x509File);
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Decrypt by public key string");
|
||||
}
|
||||
|
||||
return getPublicKeyByString(key);
|
||||
}
|
||||
|
||||
public void setPublicKeyString(String publicKeyString) {
|
||||
if (publicKeyString == null) {
|
||||
throw new IllegalArgumentException("The publicKeyString cannot be null");
|
||||
PublicKey getPublicKeyByString(String publicKeyString) {
|
||||
if (publicKeyString == null || publicKeyString.length() == 0) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Decrypt by default key");
|
||||
}
|
||||
publicKeyString = DEFAULT_PUBLIC_KEY_STRING;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -62,13 +114,17 @@ public class RsaDecrypter extends AbstractDecrypter {
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
|
||||
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
this.publicKey = keyFactory.generatePublic(x509KeySpec);
|
||||
return keyFactory.generatePublic(x509KeySpec);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Failed to get public key", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPublicKeyFile(String publicKeyFile) {
|
||||
PublicKey getPublicKeyByPublicKeyFile(String publicKeyFile) {
|
||||
if (publicKeyFile == null || publicKeyFile.length() == 0) {
|
||||
return getPublicKeyByString(null);
|
||||
}
|
||||
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(publicKeyFile);
|
||||
@@ -82,36 +138,30 @@ public class RsaDecrypter extends AbstractDecrypter {
|
||||
byte[] publicKeyBytes = out.toByteArray();
|
||||
X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
|
||||
KeyFactory factory = KeyFactory.getInstance("RSA");
|
||||
this.publicKey = factory.generatePublic(spec);
|
||||
return factory.generatePublic(spec);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Failed to get public key", e);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
JdbcUtils.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
public void setX509File(String x509File) {
|
||||
PublicKey getPublicKeyByX509(String x509File) {
|
||||
if (x509File == null || x509File.length() == 0) {
|
||||
return getPublicKeyByString(null);
|
||||
}
|
||||
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(x509File);
|
||||
|
||||
CertificateFactory factory = CertificateFactory.getInstance("X.509");
|
||||
Certificate cer = factory.generateCertificate(in);
|
||||
this.publicKey = cer.getPublicKey();
|
||||
return cer.getPublicKey();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Failed to get public key", e);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
log.warn("Cannot close file [" + x509File + "] in initialized public key.");
|
||||
}
|
||||
}
|
||||
JdbcUtils.close(in);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.alibaba.druid.pool;
|
||||
package com.alibaba.druid.support.security.decryptor;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
Reference in New Issue
Block a user