1.先来看一个普通的数据源配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <datasources>
- <local-tx-datasource>
-
- <jndi-name>MySqlDS</jndi-name>
-
- <use-java-context>false</use-java-context>
- <connection-url>jdbc:mysql://10.16.175.137:3306/test</connection-url>
-
- <driver-class>com.mysql.jdbc.Driver</driver-class>
-
- <user-name>root</user-name>
-
- <password>123456</password>
- <exception-sorter-class-name> org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
- <metadata>
- <type-mapping>mySQL</type-mapping>
- </metadata>
- </local-tx-datasource>
- </datasources>
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<!-- jndi名字 -->
<jndi-name>MySqlDS</jndi-name>
<!--URL地址 -->
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://10.16.175.137:3306/test</connection-url>
<!-- 驱动 -->
<driver-class>com.mysql.jdbc.Driver</driver-class>
<!-- 用户名 -->
<user-name>root</user-name>
<!-- 密码 -->
<password>123456</password>
<exception-sorter-class-name> org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
看一下这个文件,里面用户名与密码都是以明文方式存储的,这样子对系统的安全而言带来了极大的威协。所以我们要为我们这个明文的密码加一下密,这就是本文的目的.
2.说到密码加密,这里我们用到了JBoss下的一个类org.jboss.resource.security.SedureIdentityLoginModule,看看我们该如何用它来帮我们的密码加密。
先看个配置数据源的例子(mysql-ds.xml)
- <?xml version="1.0" encoding="UTF-8"?>
- <datasources>
- <local-tx-datasource>
- <jndi-name>MySqlDS</jndi-name>
- <use-java-context>false</use-java-context>
- <connection-url>jdbc:mysql://192.168.1.91:3306/atteam</connection-url>
- <driver-class>com.mysql.jdbc.Driver</driver-class>
-
- <security-domain>EncryptDBPassword</security-domain>
- <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
- <metadata>
- <type-mapping>mySQL</type-mapping>
- </metadata>
- </local-tx-datasource>
- </datasources>
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:mysql://192.168.1.91:3306/atteam</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<!--这里不用写上你的用户名与密码了,我们可以在login-config.xml里做点手脚,就OK了 -->
<security-domain>EncryptDBPassword</security-domain>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
接着我们修改server\default\conf\login-config.xml文件,加上下面这一段配置文件
- <application-policy name="EncryptDBPassword">
-
- <authentication>
- <login-module code="org.jboss.resource.security.SecureIdentityLoginModule" flag="required">
-
- <module-option name="username">test</module-option>
-
- <module-option name="password">64c5fd2979a86168</module-option>
-
- <module-option name="managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MySqlDS</module-option>
- </login-module>
- </authentication>
- </application-policy>
<application-policy name="EncryptDBPassword">
<!--这里的name应该是你在配置数据源时写的security-domain里的字符串-->
<authentication>
<login-module code="org.jboss.resource.security.SecureIdentityLoginModule" flag="required">
<!-- 数据库的用户名 -->
<module-option name="username">test</module-option>
<!--数据库的密码,不过是加密过的了 -->
<module-option name="password">64c5fd2979a86168</module-option>
<!-- 注意name等于你的数据源的jndi-name,这里是MySqlDS -->
<module-option name="managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MySqlDS</module-option>
</login-module>
</authentication>
</application-policy>
3.补充一下,这个加密的密码是哪来的
- java -cp "D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\lib\jboss-jmx.jar;D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\lib\jboss-common.jar;D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\server\default\lib\jboss-jca.jar;D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\server\default\lib\jbosssx.jar" org.jboss.resource.security.SecureIdentityLoginModule 123456 Encoded password: 64c5fd2979a86168
java -cp "D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\lib\jboss-jmx.jar;D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\lib\jboss-common.jar;D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\server\default\lib\jboss-jca.jar;D:\TDdownload\jboss-4.2.0.CR1\jboss-4.2.0.CR1\server\default\lib\jbosssx.jar" org.jboss.resource.security.SecureIdentityLoginModule 123456 Encoded password: 64c5fd2979a86168
写上你自己的路径和你要加密的密码就行了,我这里要加密的密码是123456,加密过之后就是64c5fd2979a86168