博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

druid数据源配置、数据库密码加密

Posted on 2020-11-28 14:17  海绵谷  阅读(541)  评论(0编辑  收藏  举报
  • 温故:jdbc连接数据库配置的步骤
public static void main(String[] args) throws Exception {
        
         //1.加载驱动类,得到DriverManager;不加载DriverManager 为空,
         // 现在不需要显示的声明,因为驱动的service里写了类名,spi会加载这个
         //Class.forName("com.mysql.jdbc.Driver");
         //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/taotao?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC","root","root");
        //3.获取执行器
        Statement statement =  connection.createStatement();
        //4.执行sql,得到执行结果
        ResultSet rs = statement.executeQuery("select * from tb_content ");
        //5.结果集处理
        while (rs.next()){
            int id = rs.getInt("id");
            String title = rs.getString("title");
            System.out.println("id:"+id+" 姓名:"+title);
        }
    }

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns=" http://www.springframework.org/schema/beans" 
 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:batch=" http://www.springframework.org/schema/batch" 
 xsi:schemaLocation=" http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
  init-method="init" destroy-method="close"> 
  <property name="driverClassName" value="..." /> 
  <property name="url" value=".." /> 
  <property name="username" value="..." /> 
  <property name="password" value="..." /> 
  <property name="minIdle" value="1" /> 
  <property name="maxActive" value="10" />
  <!-- 配置获取连接等待超时的时间 --> 
  <property name="maxWait" value="10000" />
 </bean>
</beans>
  • 所以如果用代码配置,我们需要创建一个bean交给spring管理,首先添加@Configuration,声明这是配置类,声明bean,在方法上添加@bean(等于xml的bean标签),方法的返回值类型就是bean标签里的class属性,修改换成java代码配置如下,
import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DataSourceConfig{
    /**
     * 数据源属性配置
     * @return
     * @throws Exception
     */
    @Bean
    public DruidDataSource dataSource() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setPassword("root");
        //可以将publicKey和password放在别处读取,比如其他服务
        String password = "soGGv6Gtv7IApYYhvLk2n6L6sKg1MU1D3EfONrUQZQrg5NiJA26BlsNv6oOIdS1hOUZaLqJrgHDdYYEBrbgJQw==";
        String pk = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALNHtlFe4LZG4YWj46BJAeGn3FTMAOwGZPW2VJrbAugB7dnnVy1IppOn5N0TBko8rm+FvqD254RTXmSJHICFQLUCAwEAAQ==";
        dataSource.setUsername(ConfigTools.decrypt(pk,password));
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/taotao?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        //设置游标缓存,对于oracle 数据库,性能有巨大提升;mysql 建议关掉
        dataSource.setPoolPreparedStatements(false);
        dataSource.setMaxActive(10);
        dataSource.setMinIdle(5);
        dataSource.setMaxWait(1000);
        //去掉之后,druid监控页面不显示sql,
        dataSource.setFilters("stat");
        return dataSource;
    }
    
    /**
     * druid 监控配置
     * @return
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        Map<String, String> initParameters = new HashMap<>();
        //向里面添加参数
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","pass");
        //设置
        bean.setInitParameters(initParameters);
        return bean;
    }
}
  • 使用druid对密码进行加密处理,进入druid的jar包位置

  • 然后执行命令,将会得到私钥、公钥、加密后的密码

java -cp druid-1.0.20.jar com.alibaba.druid.filter.config.ConfigTools your_pass

  • 然后将publickey,password 在配置DataSource属性时候进行解密处理,就会得到数据库的真正密码 ,解密方法
ConfigTools.decrypt(publickey,password)