精华内容
下载资源
问答
  • 更新 我们正在努力刷新此代码库。 请查看当前估计日期的以及计划工作的相关问题/项目。 Grafana版本兼容性: Grafana支持Druid插件版本0.0.3及更低版本:3.xx Grafana支持Druid插件0.0.4及更高版本:4.xx ...
  • DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driverClass, filters); druidPlugin.setPublicKey(publicKey); // 数据库密码加密 druidPlugin.addFilter( new StatFilter()); ...
    //继承JbootAppListenerBase
        @Override
        public void onPluginConfig(JfinalPlugins plugins) {
            PropKit.use("config.properties");
            
            Prop prop=PropKit.use("jdbc.properties");
            String url = prop.get("jdbcUrl");
            String username = prop.get("user");
            String password = prop.get("password");
    //        String privateKey = PropKit.get("privateKey");
            String publicKey = prop.get("publicKey");
            String driverClass = prop.get("driverClass");
            String filters = prop.get("filters");//监控统计:"stat" 、 防SQL注入:"wall"、使用加密:"config"
            int initialSize = prop.getInt("db.initialSize");
            int minIdle = prop.getInt("db.minIdle");
            int maxActive = prop.getInt("db.maxActive");
            long maxWait = prop.getLong("db.maxWait");
     
            //DB+Record
            DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driverClass, filters);
            druidPlugin.setPublicKey(publicKey);//数据库密码加密
            druidPlugin.addFilter(new StatFilter());
            druidPlugin.set(initialSize, minIdle, maxActive);
            druidPlugin.setMaxWait(maxWait);
            druidPlugin.setName(prop.get("poolName"));
            druidPlugin.start();
            plugins.add(druidPlugin);
            
            //处理Model方式获取数据源、连接数据库
            ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
            activeRecordPlugin.setShowSql(false);
            DataSourceConfig datasourceConfig = Jboot.config(DataSourceConfig.class, "jboot.datasource");
            datasourceConfig.setUrl(url);
            datasourceConfig.setDriverClassName(driverClass);
            datasourceConfig.setType(prop.get("type"));
            datasourceConfig.setUser(username);
            datasourceConfig.setPassword(password);
            datasourceConfig.setName(prop.get("name"));
            datasourceConfig.setPrepStmtCacheSize(prop.getInt("prepStmtCacheSize"));
            datasourceConfig.setPrepStmtCacheSqlLimit(prop.getInt("prepStmtCacheSqlLimit"));
            datasourceConfig.setCachePrepStmts(prop.getBoolean("cachePrepStmts"));
            datasourceConfig.setMinimumIdle(prop.getInt("minimumIdle"));
            datasourceConfig.setMaximumPoolSize(prop.getInt("maximumPoolSize"));
            datasourceConfig.setNeedAddMapping(prop.getBoolean("needAddMapping"));
            DataSourceConfigManager.me().addConfig(datasourceConfig);
            //获得该数据源匹配的表
            List<TableInfo> tableInfos = TableInfoManager.me().getMatchTablesInfos(datasourceConfig);
    
            for (TableInfo ti : tableInfos) {
                if (StrUtil.isNotBlank(ti.getPrimaryKey())) {
                    activeRecordPlugin.addMapping(ti.getTableName(), ti.getPrimaryKey(), (Class<? extends Model<?>>) ti.getModelClass());
                } else {
                    activeRecordPlugin.addMapping(ti.getTableName(), (Class<? extends Model<?>>) ti.getModelClass());
                }
            }
            //template,DbKit
            activeRecordPlugin.addSqlTemplate(prop.get("sqlTemplate"));
            activeRecordPlugin.start();
            plugins.add(activeRecordPlugin);
        }
    driverClass = com.mysql.jdbc.Driver
    jdbcUrl = jdbc\:mysql\://127.0.0.1\:3306/use?characterEncoding\=utf8&useSSL\=false
    filters = stat,wall,config
    user = 
    #password = 
    password = 
    #privateKey=
    publicKey=
    
    poolName=xbzx-subscriber
    db.initialSize=10
    db.minIdle=20
    db.maxActive=500
    db.maxWait=60000
    
    
    #dataSourceConfig
    type=mysql
    name=main
    prepStmtCacheSize=500
    prepStmtCacheSqlLimit=2048
    cachePrepStmts=true
    minimumIdle=0
    maximumPoolSize=10
    needAddMapping=true
    sqlTemplate=/sql/sys.sql

    *jfinal 模板sql

     

    *加密druid数据源

    *私钥自己持有,私钥可以解密,公钥加密

    方式一:

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

    (user为数据库密码)

    方式二

    参考源码

    /*
     * Copyright 1999-2101 Alibaba Group Holding Ltd.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package com.alibaba.druid.filter.config;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.security.InvalidKeyException;
    import java.security.Key;
    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.SecureRandom;
    import java.security.cert.Certificate;
    import java.security.cert.CertificateFactory;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.RSAPrivateKeySpec;
    import java.security.spec.RSAPublicKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    
    import javax.crypto.Cipher;
    
    import com.alibaba.druid.util.Base64;
    import com.alibaba.druid.util.JdbcUtils;
    
    public class ConfigTools {
    
        private static final String DEFAULT_PRIVATE_KEY_STRING = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAocbCrurZGbC5GArEHKlAfDSZi7gFBnd4yxOt0rwTqKBFzGyhtQLu5PRKjEiOXVa95aeIIBJ6OhC2f8FjqFUpawIDAQABAkAPejKaBYHrwUqUEEOe8lpnB6lBAsQIUFnQI/vXU4MV+MhIzW0BLVZCiarIQqUXeOhThVWXKFt8GxCykrrUsQ6BAiEA4vMVxEHBovz1di3aozzFvSMdsjTcYRRo82hS5Ru2/OECIQC2fAPoXixVTVY7bNMeuxCP4954ZkXp7fEPDINCjcQDywIgcc8XLkkPcs3Jxk7uYofaXaPbg39wuJpEmzPIxi3k0OECIGubmdpOnin3HuCP/bbjbJLNNoUdGiEmFL5hDI4UdwAdAiEAtcAwbm08bKN7pwwvyqaCBC//VnEWaq39DCzxr+Z2EIk=";
        public static final String DEFAULT_PUBLIC_KEY_STRING = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHGwq7q2RmwuRgKxBypQHw0mYu4BQZ3eMsTrdK8E6igRcxsobUC7uT0SoxIjl1WveWniCASejoQtn/BY6hVKWsCAwEAAQ==";
    
        public static void main(String[] args) throws Exception {
            String password = args[0];
            String[] arr = genKeyPair(512);
            System.out.println("privateKey:" + arr[0]);
            System.out.println("publicKey:" + arr[1]);
            System.out.println("password:" + encrypt(arr[0], password));
        }
    
        public static String decrypt(String cipherText) throws Exception {
            return decrypt((String) null, cipherText);
        }
    
        public static String decrypt(String publicKeyText, String cipherText)
                throws Exception {
            PublicKey publicKey = getPublicKey(publicKeyText);
    
            return decrypt(publicKey, cipherText);
        }
    
        public static PublicKey getPublicKeyByX509(String x509File) {
            if (x509File == null || x509File.length() == 0) {
                return ConfigTools.getPublicKey(null);
            }
    
            FileInputStream in = null;
            try {
                in = new FileInputStream(x509File);
    
                CertificateFactory factory = CertificateFactory
                        .getInstance("X.509");
                Certificate cer = factory.generateCertificate(in);
                return cer.getPublicKey();
            } catch (Exception e) {
                throw new IllegalArgumentException("Failed to get public key", e);
            } finally {
                JdbcUtils.close(in);
            }
        }
    
        public static PublicKey getPublicKey(String publicKeyText) {
            if (publicKeyText == null || publicKeyText.length() == 0) {
                publicKeyText = ConfigTools.DEFAULT_PUBLIC_KEY_STRING;
            }
    
            try {
                byte[] publicKeyBytes = Base64.base64ToByteArray(publicKeyText);
                X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(
                        publicKeyBytes);
    
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                return keyFactory.generatePublic(x509KeySpec);
            } catch (Exception e) {
                throw new IllegalArgumentException("Failed to get public key", e);
            }
        }
    
        public static PublicKey getPublicKeyByPublicKeyFile(String publicKeyFile) {
            if (publicKeyFile == null || publicKeyFile.length() == 0) {
                return ConfigTools.getPublicKey(null);
            }
    
            FileInputStream in = null;
            try {
                in = new FileInputStream(publicKeyFile);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int len = 0;
                byte[] b = new byte[512 / 8];
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
                }
    
                byte[] publicKeyBytes = out.toByteArray();
                X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
                KeyFactory factory = KeyFactory.getInstance("RSA");
                return factory.generatePublic(spec);
            } catch (Exception e) {
                throw new IllegalArgumentException("Failed to get public key", e);
            } finally {
                JdbcUtils.close(in);
            }
        }
    
        public static String decrypt(PublicKey publicKey, String cipherText)
                throws Exception {
            Cipher cipher = Cipher.getInstance("RSA");
            try {
                cipher.init(Cipher.DECRYPT_MODE, publicKey);
            } catch (InvalidKeyException e) {
                // 因为 IBM JDK 不支持私钥加密, 公钥解密, 所以要反转公私钥
                // 也就是说对于解密, 可以通过公钥的参数伪造一个私钥对象欺骗 IBM JDK
                RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
                RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
                Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
                cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one.
                cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
            }
            
            if (cipherText == null || cipherText.length() == 0) {
                return cipherText;
            }
    
            byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
            byte[] plainBytes = cipher.doFinal(cipherBytes);
    
            return new String(plainBytes);
        }
    
        public static String encrypt(String plainText) throws Exception {
            return encrypt((String) null, plainText);
        }
    
        public static String encrypt(String key, String plainText) throws Exception {
            if (key == null) {
                key = DEFAULT_PRIVATE_KEY_STRING;
            }
    
            byte[] keyBytes = Base64.base64ToByteArray(key);
            return encrypt(keyBytes, plainText);
        }
    
        public static String encrypt(byte[] keyBytes, String plainText)
                throws Exception {
            PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
            KeyFactory factory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = factory.generatePrivate(spec);
            Cipher cipher = Cipher.getInstance("RSA");
            try {
                cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            } catch (InvalidKeyException e) {
                //For IBM JDK, 原因请看解密方法中的说明
                RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
                RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
                Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
                cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey);
            }
    
            byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
            String encryptedString = Base64.byteArrayToBase64(encryptedBytes);
    
            return encryptedString;
        }
    
        public static byte[][] genKeyPairBytes(int keySize)
                throws NoSuchAlgorithmException {
            byte[][] keyPairBytes = new byte[2][];
    
            KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
            gen.initialize(keySize, new SecureRandom());
            KeyPair pair = gen.generateKeyPair();
    
            keyPairBytes[0] = pair.getPrivate().getEncoded();
            keyPairBytes[1] = pair.getPublic().getEncoded();
    
            return keyPairBytes;
        }
    
        public static String[] genKeyPair(int keySize)
                throws NoSuchAlgorithmException {
            byte[][] keyPairBytes = genKeyPairBytes(keySize);
            String[] keyPairs = new String[2];
    
            keyPairs[0] = Base64.byteArrayToBase64(keyPairBytes[0]);
            keyPairs[1] = Base64.byteArrayToBase64(keyPairBytes[1]);
    
            return keyPairs;
        }
    
    }

    转载于:https://www.cnblogs.com/zzlcome/p/11179973.html

    展开全文
  • JAVAmain方法建立数据库连接

    千次阅读 2020-03-11 11:01:19
    DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim()); ActiveRecordPlugin arp = new ActiveRecordPlugin("mysql", druidPlugin); druidPlu....
    DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
    ActiveRecordPlugin arp = new ActiveRecordPlugin("mysql", druidPlugin);
    druidPlugin.start();
    arp.start()
    
    import java.sql.*;
    
    /**
     */
    public class Text {
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "数据库连接路径";
        static  final String USER = "用户名";
        static  final String PASS = "用户密码!";
        public static void main(String[] args) {
            Connection conn = null;
            Statement stmt = null;
            try {
                // 注册 JDBC 驱动
                Class.forName(JDBC_DRIVER);
    
                // 打开链接
                System.out.println("连接数据库...");
                conn = DriverManager.getConnection(DB_URL, USER, PASS);
    
                // 执行查询
                System.out.println(" 实例化Statement对象...");
                stmt = conn.createStatement();
                String sql = "SELECT * FROM ";
                ResultSet rs = stmt.executeQuery(sql);
    
                // 展开结果集数据库
                while (rs.next()) {
                    // 通过字段检索
                    String name = rs.getString("parameter");
                    // 输出数据
                    System.out.print("取到参数: " + name);
                    System.out.print("\n");
                }
                // 完成后关闭
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException se) {
                // 处理 JDBC 错误
                se.printStackTrace();
            } catch (Exception e) {
                // 处理 Class.forName 错误
                e.printStackTrace();
            } finally {
                try {
                    if (stmt != null) stmt.close();
                } catch (SQLException se2) {
                    try {
                        if (conn != null) conn.close();
                    } catch (SQLException se) {
                        se.printStackTrace();
                    }
                }
            }
        }
    
    }
    

     

    展开全文
  • public static void main(String[] args) { DruidPlugin druidPlugin = BaseConfig.getDruidPlugin(); ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin); ...
        public static void main(String[] args) {
            DruidPlugin druidPlugin = BaseConfig.getDruidPlugin();
            ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
            druidPlugin.start();
            activeRecordPlugin.start();
            // new OrderService().exportOrder();
            // Db. ...
        }

    展开全文
  • 在JFinalConfig子类中的configPlugin...DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password")); ActiveRecordPlug...

    在JFinalConfig子类中的configPlugin中配置插件。注意需要导入各自的jar包。

    一:DruidPlugin 

    DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password")); 
    		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
    		//将数据库中的user表映射到User类中,这样就可以通过User的Model对象操作数据库了
    		arp.addMapping("user", User.class);
    		//查看sql
    		arp.setShowSql(true);
    		me.add(druidPlugin);
    		me.add(arp);

    二:C3p0Plugin

    
    		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
    		//ORM Activerecord
    		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
    		arp.setShowSql(true);
    		arp.addMapping("user", User.class);
    		me.add(c3p0Plugin);
    		me.add(arp);

     

    展开全文
  • public void configPlugin(Plugins me) { // TODO Auto-generated method stub DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcurl"), getProperty("user"), getProperty("password")); me.add(dru
  • jfinal 多数据源 Db+record 分页

    千次阅读 2017-06-14 15:23:20
    使用Db+record 无需Model 类无需对数据库表进行映射 多数据源配置: public void configPlugin(Plugins me...DruidPlugin druidPlugin = createDruidPlugin(); me.add(druidPlugin); // 配置ActiveRecord插件 ActiveRe
  • JFinal数据库连接配置

    千次阅读 2017-04-09 10:46:45
    public void configPlugin(Plugins me) { logger.debug("------配置插件 START--------"); logger.debug("--------配置数据源--------...DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProper
  • 首先导入druid包,然后配置configPlugin 1 @Override 2 public void configPlugin(Plugins me) { 3 /**配置druid数据连接池插件**/ 4 DruidPlugin dp=new DruidPlugin(getProperty("j...
  • #关于maven的jar包下找不到类的一种可能 严重: Exception starting filter jfinal-web java.lang.NoClassDefFoundError: ...at com.jfinal.plugin.druid.DruidPlugin.start(DruidPlugin.java:1...
  • 配置数据库连接池插件,此处以Druid为例,还需要配置数据库访问插件,即ActiveRecord插件,用于建立数据库中Table和Java Bean的mapping映射: public void configPlugin... DruidPlugin druidPlugin = new DruidPlu...
  • JFinal 多数据源切换,实现数据迁移

    千次阅读 2016-12-01 09:46:24
    第一步:使用单例模式,启动两个数据池 public class DataSource1 { ... private static DruidPlugin druidPlugin1 ; private static Properties prop = new Properties(); static { try { prop.loa
  • 直接上代码: public void configPlugin(Plugins me) { // 这里是在数据库的配置文件中读取数据库的... DruidPlugin dbPlugin = new DruidPlugin (PropKit.get("jdbcUrl").trim(), PropKit.get("user").trim()...
  • //读取数据库配置 ... DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),getProperty("user"),getProperty("password")); //WallFilter wall = new WallFi...
  • 尝试了ActiveRecordPlugin和DruidPlugin的多数据源,但是因为DruidPlugin在本地一直报错一个很奇怪的语法错误,所以就选用了ActiveRecordPlugin 总的来说就是多个数据源配置多个ActiveRecordPlugin 直接使用如下...
  • 一:在Config中添加数据库 protected void plugin(Plugins me) { ...DruidPlugin dp = new DruidPlugin(PropKit.use("mysql.txt").get("url"), PropKit.use("mysql.txt").get("user"), PropKit.use("mysql.txt").ge
  • Jfinal+DbRecord多数据源如何配置

    千次阅读 2018-11-24 11:23:16
    我们在做java web 项目的时候,有时候会遇到一个项目中需要引用多数据...配置新数据源的数据连接池new DruidPlugin; 启动该连接池(druidPlugin.start()); 实例化ActiveRecordPlugin; 启动该实例; 2.具体实...
  • )https://github.com/grafana-druid-plugin/druidplugin/issues/2</li></ul> <p>Plugin Version 0.0.4: - Added support for Grafana-4-x-x - Fixed bug: (error on Timeseries with Grafana 4.1.2 #8 ) ...
  • 1、把本地的一个文件插入到数据库中,数据库字段用text保存 public static void main(String...DruidPlugin druid = new DruidPlugin(PropKit.get(“jdbcUrl”), PropKit.get(“user”), PropKit.get(“password”...
  • 1.4 打包DruidPlugin和ActiveRecordPlugin; 以上让你的config更加轻便 加入ActionExtentionHandler 更方面的伪静态处理 加入 NotFoundActionInterceptor 当找不到对应的 action 时,fire 404 com.jfinal.ext2.kit ...
  • JFinal配置durid

    2019-09-19 01:57:54
    1、在configPlugin中配置以下代码 // DruidPluginDruidPlugin dp = new DruidPlugin(url, username, password, "com.mysql.jdbc.Driver");dp.addFilter(new StatF...
  • springboot 集成JFinal ActiveRecord

    千次阅读 2019-04-18 11:19:59
    springboot 集成JFinal ActiveRecord 下面是JfinalActiveRecordPluginConfig 文件: import ... import com.jfinal.plugin.druid.DruidPlugin; import com.jfuso.model._Map...
  • Jfinal Generator 生成Model

    千次阅读 2019-04-18 11:30:54
    import com.jfinal.kit.PathKit; import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; import ... import com.jfinal.plugin.druid.DruidPlugin; import...
  • JFInal model 自动生成

    千次阅读 2018-02-09 16:53:06
    package com.activiti.generator; import javax.sql.DataSource; import com.jfinal.kit.PathKit; import ... import com.jfinal.plugin.druid.DruidPlugin; pu...
  • validation query

    千次阅读 2016-04-13 10:23:03
    public static void mySQLConfigPlugin(Plugins me) { // C3p0Plugin dbplugin = createC3p0Plugin();...DruidPlugin dbplugin = createDruidPlugin(); /** * hsqldb - "select 1 from INFORMATION_SCHEMA.SYSTEM_

空空如也

空空如也

1 2
收藏数 34
精华内容 13
关键字:

druidplugin