zoukankan      html  css  js  c++  java
  • JBoss下配置数据源加密

    一、JBoss下配置数据源时,如果密码直接暴露给了系统的操作员或者维护人员,显然就增加了数据库不安全的因素。

    MySQL Datasource配置样例 
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2.   
    3. <!-- ===================================================================== -->  
    4. <!--                                                                       -->  
    5. <!--  JBoss Server Configuration                                           -->  
    6. <!--                                                                       -->  
    7. <!-- ===================================================================== -->  
    8.   
    9. <!-- See http://www.jboss.org/community/wiki/Multiple1PC for information about local-tx-datasource -->  
    10. <!-- $Id: mssql-ds.xml 97536 2009-12-08 14:05:07Z jesper.pedersen $ -->  
    11.   
    12.   <!-- ======================================================================-->  
    13.   <!-- New ConnectionManager setup for Microsoft SQL Server 2005  driver     -->  
    14.   <!-- Further information about the Microsoft JDBC Driver version 1.1      -->  
    15.   <!-- can be found here:                                                   -->  
    16.   <!-- http://msdn2.microsoft.com/en-us/library/aa496082.aspx               -->    
    17.   <!-- ===================================================================== -->  
    18.   
    19. <datasources>  
    20.   <local-tx-datasource>  
    21.     <jndi-name>MSSQLDS</jndi-name>  
    22.     <connection-url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase</connection-url>  
    23.     <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>  
    24.     <user-name>admin</user-name>  
    25.     <password>password</password>  
    26.           
    27.     <!-- sql to call when connection is created  
    28.     <new-connection-sql>some arbitrary sql</new-connection-sql>  
    29.     -->  
    30.   
    31.     <!-- sql to call on an existing pooled connection when it is obtained from pool   
    32.     <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>  
    33.     -->  
    34.   
    35.     <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->  
    36.     <metadata>  
    37.         <type-mapping>MS SQLSERVER2000</type-mapping>  
    38.     </metadata>  
    39.   </local-tx-datasource>  
    40.   
    41. </datasources>  



    不用担心,JBoss本身提供了对密码进行加密的工具org.jboss.resource.security.SecureIdentityLoginModule
    可以在Windows下用如下命令拿到密码的加密字串: 
    1. D:JBossjboss-6.1.0.Final>java -cp clientjboss-logging.jar;libjbosssx.jar org.jboss.resource.security.SecureIdentityLoginModule password  
    2.   
    3. Encoded password: 5dfc52b51bd35553df8592078de921bc  




    二、拿到加密的密码后就可以进行加密的数据源配置了
    1. 配置 MySQL Datasource 
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2.   
    3. <!-- ===================================================================== -->  
    4. <!--                                                                       -->  
    5. <!--  JBoss Server Configuration                                           -->  
    6. <!--                                                                       -->  
    7. <!-- ===================================================================== -->  
    8.   
    9. <!-- See http://www.jboss.org/community/wiki/Multiple1PC for information about local-tx-datasource -->  
    10. <!-- $Id: mssql-ds.xml 97536 2009-12-08 14:05:07Z jesper.pedersen $ -->  
    11.   
    12.   <!-- ======================================================================-->  
    13.   <!-- New ConnectionManager setup for Microsoft SQL Server 2005  driver     -->  
    14.   <!-- Further information about the Microsoft JDBC Driver version 1.1      -->  
    15.   <!-- can be found here:                                                   -->  
    16.   <!-- http://msdn2.microsoft.com/en-us/library/aa496082.aspx               -->    
    17.   <!-- ===================================================================== -->  
    18.   
    19. <datasources>  
    20.   <local-tx-datasource>  
    21.     <jndi-name>MSSQLDS</jndi-name>  
    22.     <connection-url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase</connection-url>  
    23.     <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>  
    24.       
    25.     <!-- REPLACED WITH security-domain BELOW  
    26.     <user-name>admin</user-name>  
    27.     <password>password</password>  
    28.     -->  
    29.     <security-domain>EncryptDBPassword</security-domain>  
    30.       
    31.       
    32.     <!-- sql to call when connection is created  
    33.     <new-connection-sql>some arbitrary sql</new-connection-sql>  
    34.     -->  
    35.   
    36.     <!-- sql to call on an existing pooled connection when it is obtained from pool   
    37.     <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>  
    38.     -->  
    39.   
    40.     <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->  
    41.     <metadata>  
    42.        <type-mapping>MS SQLSERVER2000</type-mapping>  
    43.     </metadata>  
    44.   </local-tx-datasource>  
    45.   
    46. </datasources>  



    2. 配置login-config.xml(一般放到 srcmain esourcesMETA-INF 目录下) 
    1. <?xml version='1.0'?>  
    2. <!DOCTYPE policy PUBLIC  
    3.       "-//JBoss//DTD JBOSS Security Config 3.0//EN"  
    4.       "http://www.jboss.org/j2ee/dtd/security_config.dtd">  
    5.   
    6. <policy>  
    7.     <!-- Example usage of the SecureIdentityLoginModule -->  
    8.     <application-policy name="EncryptedMySQLDbRealm">  
    9.         <authentication>  
    10.             <login-module code="org.jboss.resource.security.SecureIdentityLoginModule" flag="required">  
    11.                 <module-option name="username">admin</module-option>  
    12.                 <module-option name="password">5dfc52b51bd35553df8592078de921bc</module-option>  
    13.                 <module-option name="managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MSSQLDS</module-option>  
    14.             </login-module>  
    15.         </authentication>  
    16.     </application-policy>  
    17. </policy>  

    3. 配置jboss-service.xml(一般放到 srcmain esourcesMETA-INF 目录下。我是通过EJB实现DynamicLoginConfig,希望各位提供更便捷的方案)

     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <server>  
    3.     <mbean code="org.jboss.security.auth.login.DynamicLoginConfig" name="jboss:service=Test.DynamicLoginConfig">  
    4.         <attribute name="AuthConfig">META-INF/login-config.xml</attribute>  
    5.         <!-- The service which supports dynamic processing of login-config.xml configurations. -->  
    6.         <depends optional-attribute-name="LoginConfigService">jboss.security:service=XMLLoginConfig</depends>  
    7.         <!-- Optionally specify the security mgr service to use when this service   
    8.             is stopped to flush the auth caches of the domains registered by this service. -->  
    9.         <depends optional-attribute-name="SecurityManagerService">jboss.security:service=JaasSecurityManager</depends>  
    10.     </mbean>  
    11. </server>  




    三、最后给大家介绍一种解密JBoss加密工具加密的密码
    1. 找到SecureIdentityLoginModule所在的包 D:JBossjboss-6.1.0.Finallibjbosssx.jar
    找到SecureIdentityLoginModule.class,反编译一下就全明白了
    加密使用的是: private static String encode(String secret)
    自然解密的到是:private static char[] decode(String secret)

    注:推荐反编译工具 JDGUI

    2. 在Eclipse下新建包 org.jboss.resource.security,新建SecureIdentityLoginModule.java 和PasswordDecoder.java

    SecureIdentityLoginModule.java
    (这个类只是为了能让 PasswordDecoder 编译通过,没有实际意义)

     
    1. package org.jboss.resource.security;  
    2.   
    3. public class SecureIdentityLoginModule {  
    4.     private static String encode(String secret) {  
    5.         return secret;  
    6.     }  
    7.   
    8.     private static char[] decode(String secret) {  
    9.         System.out.println("Input password: " + secret);  
    10.         return new char[] { '0', '1', '2', '3', '4', '5' };  
    11.     }  
    12. }  

    PasswordDecoder.java(利用反射调用SecureIdentityLoginModule 里 private static char[] decode(String secret) 方法)

     
    1. package org.jboss.resource.security;  
    2.   
    3. import java.lang.reflect.Method;  
    4.   
    5. /** 
    6.  * Decode the encoded password. 
    7.  *  
    8.  * @author 酒樽舞曲 
    9.  *  
    10.  */  
    11. public class PasswordDecoder {  
    12.     public static void main(String args[]) throws Exception {  
    13.         Class<SecureIdentityLoginModule> cla = SecureIdentityLoginModule.class;  
    14.         Method m = cla.getDeclaredMethod("decode", String.class);  
    15.         m.setAccessible(true);  
    16.   
    17.         Object obj = m.invoke(null, args[0]);  
    18.         char[] chars = (char[]) obj;  
    19.   
    20.         System.out.println("Decoded password: " + new String(chars));  
    21.     }  
    22. }  

    3. 将编译好的 PasswordDecoder.class 放到 D:JBossjboss-6.1.0.Finallibjbosssx.jar 中 orgjboss esourcesecurity 下 (做坏事前先备份)

    4. 解密
    在Windows下用如下命令拿到密文的解密字串:

     
    1. D:JBossjboss-6.1.0.Final>java -cp clientjboss-logging.jar;libjbosssx.jar org.jboss.resource.security.PasswordDecoder 5dfc52b51bd35553df8592078de921bc  
    2.   
    3. Decoded password: password  
  • 相关阅读:
    R语言中gsub使用示例记录
    linux 系统中wget实现并行下载
    R语言中实现将多行数据合并为一行
    python安装包国内镜像加速
    VMware Workstation 与 Device/Credential Guard不兼容
    缩放矩阵, 这里面有关于矩阵的 种种操作 这个要学下 很有用
    ffd 点的控制效果、
    摘抄 : max mel 的一些用法。
    帮朋友写的查找选择的父子骨骼。之前我都是用递归,这个好、
    距离的算法。 下面两个是对等的。
  • 原文地址:https://www.cnblogs.com/leo3689/p/4414551.html
Copyright © 2011-2022 走看看