jboss oracle-rs.xml password 加密方法,Jboss 數據源密碼明文加密 + 小程序

現在一個客戶做審計要求需要把存儲在文件中的數據庫密碼加密,網上也有很多教程了,這里主要是做一個補充,寫了一個小程序,批量修改配置,為什么這樣做呢?

我們這邊的產品在Jboss中配置了多個數據源,數一下:1,2,3...大概有10個吧,可能更多,一個個改費時呀.

這邊的Jboss版本為 5.1.0.

下面來看一看.

默認數據源配置如下:

jdbc/ADMIN

jdbc:oracle:thin:@sce10:1521:XXXX

oracle.jdbc.OracleDriver

XXXX

XXXX

0

100

ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy/mm/dd hh24:mi:ss'

用戶名密碼都是明文.

我們可以進行小小的更改,編輯它:

jdbc/ADMIN

jdbc:oracle:thin:@sce10:1521:XXXX

oracle.jdbc.OracleDriver

0

100

ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy/mm/dd hh24:mi:ss'

SCPRD_Encrypt_ADMIN_Password

編輯后,變成如上所示,那么這個安全域配置在哪里呢,往下看

編輯 JBOSS_HOME/server/Default/Conf/login-config.xml,添加一個節點:

wmwhse1

-7777fac1e4b05d45207a6df87216de44

jboss.jca:service=LocalTxCM,name=jdbc/ADMIN

name: DataSource中 security-domain 對應,一定要一致.

username:數據庫用戶名

password:加密后的密碼

managedConnectionFactoryName:name={Datasource 中的JndiName},這點也很重要

其實上面都挺簡單的.

下面重點來了,密碼加密命令(其實就是加載Jboss的一個jar文件,調用Java類生成輸出)

java -cp "C:\Infor\sce\jboss-5.1.0.GA\lib\jboss-logging-spi.jar;C:\Infor\sce\jboss-5.1.0.GA\lib\jboss-jmx.jar;C:\Infor\sce\jboss-5.1.0.GA\lib\jboss-common-core.jar;C:\Infor\sce\jboss-5.1.0.GA\common\lib\jboss-jca.jar;C:\Infor\sce\jboss-5.1.0.GA\common\lib\jbosssx.jar" org.jboss.resource.security.SecureIdentityLoginModule $password$

執行就行了,控制台會輸出密碼的.

重點又來了,數據源太多,一個個改費勁,上一小段代碼.

package org.xml.datasource;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Method;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.Node;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

/**

* 數據源批量加密

* @author berr

* 2014.11.07

*/

public class Convert2Encoded {

/**

* 加載XML

* @param path 文件路徑

* @return 返回Dom4j解析對象

*/

public Document load(String path){

InputStream is = null;

try {

is = new FileInputStream(new File(path));

SAXReader saxr = new SAXReader();

return saxr.read(is);

} catch (Exception e) {

is = null;

e.printStackTrace();

}finally{

try {

is.close();

} catch (IOException e) {

is = null;

}

}

return null;

}

/**

* 轉換

* @param document

* @throws Exception

*/

public void convertTo(Document document) throws Exception{

List datasources = document.selectNodes("//datasources/local-tx-datasource"); //XPATH 獲取數據源節點

for (Element d : datasources) {

String jndiName = d.selectSingleNode("jndi-name").getText();

Node user = d.selectSingleNode("user-name");

Node pass = d.selectSingleNode("password");

String userName = user.getText();

String password = pass.getText();

password = encryptPassword(password); //加密密碼,此classpath已加入jboss的幾個jar文件

//移除用戶名密碼節點

d.remove(user);

d.remove(pass);

d.addElement("security-domain"); //增加安全域節點

String newName = createName(jndiName);

d.selectSingleNode("security-domain").setText(newName);

output(newName,jndiName,userName,password);

}

}

/**

* 輸出 login-config.xml 配置信息,需要手工復制添加

* @param name

* @param jndiName

* @param user

* @param password

*/

public void output(String name,String jndiName,String user,String password){

StringBuffer tpl = new StringBuffer("\n");

tpl.append("\t\n");

tpl.append("\t\t\n");

tpl.append("\t\t\t%s\n");

tpl.append("\t\t\t%s\n");

tpl.append("\t\t\tjboss.jca:service=LocalTxCM,name=%s\n");

tpl.append("\t\t\n");

tpl.append("\t\n");

tpl.append("");

System.out.println(String.format(tpl.toString(), name,user,password,jndiName));

}

public void startConvert(String path) throws Exception{

Document doc = load(path);

convertTo(doc);

System.out.println("---------------------------------------------------------------------------");

System.out.println("1.The above content append to jboss/conf/login-config.xml ");

String newpath = path + ".new";

System.out.println("---------------------------------------------------------------------------");

System.out.println("2.The new datasrouce config file ["+newpath+"] replace to jboss/deploy/*.ds.xml ");

writer(newpath,doc);

}

public String createName(String jndiName){

int i = jndiName.indexOf("/");

String newName = jndiName;

if(i > -1){

newName = jndiName.substring(i + 1, jndiName.length());

}

String tpl = "SCPRD_Encrypt_%s_Password";

return String.format(tpl, newName);

}

/**

* 加密密碼

* @param password

* @return

* @throws Exception

*/

public String encryptPassword(String password) throws Exception{

/**

這里大家會覺得很奇怪,為什么要用反射,下面解答一下

Jboss這個SecureIdentityLoginModule類是做加密解密的,但是他的加密方法是私有的,我這個包不能訪問

你可能會問調用main方法也可以呀,回答是NO,main方法是在控制台輸出,不是返回一個變量

那么問題來了,怎么辦? 反射就可很簡單的解決了,下面代碼就是

* */

Class clz = org.jboss.resource.security.SecureIdentityLoginModule.class;

Method m = clz.getDeclaredMethod("encode", String.class);

m.setAccessible(true);

Object o = m.invoke(null, password);

return String.valueOf(o);

}

/**

* 寫入XML

* @param path

* @return

* @throws DocumentException

*/

public static void writer(String path,Document document) throws DocumentException{

XMLWriter writer = null;// 聲明寫XML的對象

try {

OutputFormat format = OutputFormat.createPrettyPrint();

writer = new XMLWriter(new FileWriter(path), format);

writer.write(document);

writer.close();

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

writer.close();

} catch (IOException e) {

writer = null;

}

}

}

public static void main(String[] args) throws Exception {

Convert2Encoded c2e = new Convert2Encoded();

c2e.startConvert("D:\\work\\workspace\\RESOURCE\\tools\\source\\scewebserver-ds.xml");

}

}

到此結束.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值