首页 > 编程知识 正文

数据库存储密码加密,hibernate自动创建数据库

时间:2023-05-06 04:20:35 阅读:152215 作者:1027

实现jbpm - hibernate数据库加密解密1 .描述2. jbpm -hibernate数据库加密配置2.1项目引用解密算法2.2生成加密字符串2.3配置文件2.4重写数据库连接提供程序

1 .说明

JPM工作流中与数据库相关的操作本质上是与hibernate相关的操作。 开发部署时,系统安全认证需要加密数据库的用户名、密码。 本节提供了基于java的数据库加密、解密的算法实现和jbpm- hibernate的数据库加密实现。

一.加密解密算法实现参考:

解密数据库帐户: https://blog.csdn.net/weixin _ 44462773/article/details/120453344

2 .加密解密源下载链接:

3359 download.csdn.net/download/weixin _ 44462773/24395402。

2. jbpm -hibernate数据库加密配置2.1下载项目引用和解密算法“加密解密算法源代码”,然后导出到jar包并浏览

2 .加密解密源下载链接:

3359 download.csdn.net/download/weixin _ 44462773/24395402。

2.2生成加密的字符串,写入main函数,调用加密算法,生成以数据库名为key的用户名、密码的加密字符串

publicstaticvoidmain (string [ ] args ) {String databaseName='jbpmdb '; String username='root '; String passward='1234 '; system.out.println (username ' : ' encryptanddecryptstr.encryptstr (databasename,username ); system.out.println (passward ' : ' encryptanddecryptstr.encryptstr (databasename,passward ); }结果输出示例:

2.3配置文件生成的加密字符串在jbpm.hibernate.cfg.xml配置文件的“数据库连接配置”中配置。 配置文件的示例如下所示。

? XML version=' 1.0 ' encoding=' utf-8 '? -- Hibernate主配置文件- -! 文档类型hibernate-configuration public '-//hibernate/hibernateconfigurationdtd 3.0//en ' ' http://hibernate.sourcefource -1.数据库连接配置--property name=' hibernate.connection.driver _ class ' com.MySQL.JDBC.driver/property property useUnicode=trueamp; 害羞的春天=utf-8/property property name=' hibernate.connection.username ' ev w5 eozw 7li=/property property name=' hibernat y property property name=' hibernate.dialect ' org.hibernate.dialect.MySQL5innodb dialed -- 2 .其他相关配置---! -- 2.1显示hibernate在运行时执行的sql语句--property name=' hibernate.show _ SQL ' false/property! -- 2.3在自动构建表:表不存在的情况下创建; 如果表存在,则不创建; - property name=' hibernate.hbm2DDL.auto ' update/property! _

- 使用c3p0的指定 --><property name="hibernate.connection.provider_class">com.math.jbpm.MyC3P0ConnectionProvider</property><!--使用c3p0的最小数据连接数量--><property name="hibernate.c3p0.min_size">5</property><!-- 使用c3p0维护的最大连接数量 --><property name="hibernate.c3p0.min_size">20</property><!-- 使用c3p0的超时限制 --><property name="hibernate.c3p0.timeout">300</property><!-- 使用c3p0维护的最sqdyj数量 --><property name="hibernate.c3p0.max_statements">50</property><!-- 使用c3p0生效的空闲时间 --><property name="hibernate.c3p0.idle_test_period">3000</property> <!--3. 加载所有映射--> <mapping resource="jbpm.repository.hbm.xml" /> <mapping resource="jbpm.execution.hbm.xml" /> <mapping resource="jbpm.history.hbm.xml" /> <mapping resource="jbpm.task.hbm.xml" /> <mapping resource="jbpm.identity.hbm.xml" /> <!-- 自定义对象的变量定义文件引入 --> <!-- <mapping resource="variable/power_device.hbm.xml" /> --> <!-- <mapping resource="variable/user.hbm.xml" /> --> </session-factory></hibernate-configuration> 2.4 重写数据库连接供应器 在上面的jbpm.hibernate.cfg.xml配置文件中,指定“数据库连接供应器”的实现类: <property name="hibernate.connection.provider_class">com.math.jbpm.MyC3P0ConnectionProvider</property> 连接供应器实现: /** * A connection provider that uses a C3P0 connection pool. Hibernate will use * this by default if the <tt>hibernate.c3p0.*</tt> properties are set. * @author various people * @see ConnectionProvider */public class MyC3P0ConnectionProvider implements ConnectionProvider {private static final Log log = LogFactory.getLog(MyC3P0ConnectionProvider.class);private DataSource ds;private Integer isolation;private static DatabaseEnDeCryptInterface databaseInter; static{ databaseInter = new DatabaseEnDeCryptInterface() {@Overridepublic String getEncryptUserName(String arg0, String arg1) {return EncryptAndDecryptStr.encryptStr(arg0, arg1);}@Overridepublic String getEncryptPassward(String arg0, String arg1) {return EncryptAndDecryptStr.encryptStr(arg0, arg1);}@Overridepublic String getDecryptUserName(String arg0, String arg1) {return EncryptAndDecryptStr.decryptStr(arg0, arg1);}@Overridepublic String getDecryptPassward(String arg0, String arg1) {return EncryptAndDecryptStr.decryptStr(arg0, arg1);}}; }public Connection getConnection() throws SQLException {final Connection c = ds.getConnection();if (isolation != null)c.setTransactionIsolation(isolation.intValue());if (c.getAutoCommit())c.setAutoCommit(false);return c;}public void closeConnection(Connection conn) throws SQLException {conn.close();}public void configure(Properties props) throws HibernateException {String jdbcDriverClass = props.getProperty(Environment.DRIVER);if(jdbcDriverClass == null) {log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);}else {try {Class.forName(jdbcDriverClass);} catch (ClassNotFoundException cnfe) {String msg = "JDBC Driver class not found: " + jdbcDriverClass;log.fatal(msg);throw new HibernateException(msg);}}Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);try {int minPoolSize = PropertiesHelper.getInt(Environment.C3P0_MIN_SIZE, props, 1);int maxPoolSize = PropertiesHelper.getInt(Environment.C3P0_MAX_SIZE, props, 100);int maxIdleTime = PropertiesHelper.getInt(Environment.C3P0_TIMEOUT, props, 0);int maxStatements = PropertiesHelper.getInt(Environment.C3P0_MAX_STATEMENTS, props, 0);int acquireIncrement = PropertiesHelper.getInt(Environment.C3P0_ACQUIRE_INCREMENT, props, 1);int idleTestPeriod = PropertiesHelper.getInt(Environment.C3P0_IDLE_TEST_PERIOD, props, 0);PoolConfig pcfg = new PoolConfig();pcfg.setInitialPoolSize(minPoolSize);pcfg.setMinPoolSize(minPoolSize);pcfg.setMaxPoolSize(maxPoolSize);pcfg.setAcquireIncrement(acquireIncrement);pcfg.setMaxIdleTime(maxIdleTime);pcfg.setMaxStatements(maxStatements);pcfg.setIdleConnectionTestPeriod(idleTestPeriod);String jdbcUrl = props.getProperty(Environment.URL);connectionProps.setProperty("user", databaseInter.getDecryptUserName(getDatabaseNameFromURL(jdbcUrl), props.getProperty(Environment.USER)));connectionProps.setProperty("password",databaseInter.getDecryptPassward(getDatabaseNameFromURL(jdbcUrl), props.getProperty(Environment.PASS)));DataSource unpooled = DataSources.unpooledDataSource(jdbcUrl, connectionProps);ds = DataSources.pooledDataSource(unpooled, pcfg);} catch (Exception e) {log.fatal("could not instantiate C3P0 connection pool", e);throw new HibernateException("Could not instantiate C3P0 connection pool", e);}String i = props.getProperty(Environment.ISOLATION);if (i == null) {isolation = null;} else {isolation = new Integer(i);log.info("JDBC isolation level: " + Environment.isolationLevelToString(isolation.intValue()));}}/** * 从url中获取数据库名称: * jdbc:mysql://127.0.0.1:3306/powflow_dstrnet_anhui?useSSL=true&amp;useUnicode=true&amp;害羞的春天=UTF-8 * @param url * @return */public String getDatabaseNameFromURL(String url){if(url != null && !"".equals(url)){url = url.substring(0,url.lastIndexOf("?"));String[] ss = url.split("/");url = ss[ss.length-1];}return url; }public void close() {try {DataSources.destroy(ds);} catch (SQLException sqle) {log.warn("could not destroy C3P0 connection pool", sqle);}}@Overridepublic boolean supportsAggressiveRelease() {return false;}}

上一章: 数据库账号的加密解密.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。