New file |
| | |
| | | package com.example.config; |
| | | |
| | | import com.alibaba.druid.pool.DruidDataSource; |
| | | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; |
| | | import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; |
| | | import com.alibaba.druid.util.Utils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.context.annotation.Primary; |
| | | |
| | | import javax.servlet.*; |
| | | import javax.sql.DataSource; |
| | | import java.io.*; |
| | | import java.sql.SQLException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * @Company: 西安路泰科技有限公司 |
| | | * @Author: zhaowenxuan |
| | | * @Date: 2024/7/1 10:27 |
| | | */ |
| | | @Slf4j |
| | | @Configuration |
| | | public class DruidConfig { |
| | | |
| | | @Value("${config.path}") |
| | | private String CONFIG_PATH; |
| | | @Value("${config.dir}") |
| | | private String CONFIG_DIR; |
| | | |
| | | private DruidDataSource dataSource; |
| | | |
| | | @Bean |
| | | public DataSource masterDataSource(DruidProperties druidProperties){ |
| | | dataSource = DruidDataSourceBuilder.create().build(); |
| | | Properties props = new Properties(); |
| | | try { |
| | | // 从文件中读取配置信息 |
| | | FileInputStream fis = null; |
| | | try { |
| | | fis = new FileInputStream(CONFIG_PATH); |
| | | } catch (FileNotFoundException e) { |
| | | log.info("数据库连接文件找不到 系统正在创建!"); |
| | | File f = new File(CONFIG_DIR); |
| | | if(!f.exists()){ |
| | | f.mkdirs(); |
| | | } |
| | | File file = new File(CONFIG_PATH); |
| | | try { |
| | | FileWriter fileWriter = new FileWriter(file); |
| | | fileWriter.write("ip = 你的主数据库连接ip地址\n"); |
| | | fileWriter.write("prot = 你的主数据库连接端口\n"); |
| | | fileWriter.write("name = 你的主数据库连接名称\n"); |
| | | fileWriter.write("username = 你的主数据库连接用户名\n"); |
| | | fileWriter.write("password = 你的主数据库连接密码\n"); |
| | | fileWriter.write("\n"); |
| | | fileWriter.write("hisenabled = 是否开启 his 从库数据库连接 (从数据源开关/默认关闭 fales)\n"); |
| | | fileWriter.write("hisip = 你的 his 数据库连接ip地址\n"); |
| | | fileWriter.write("hisprot = 你的 his 数据库连接端口\n"); |
| | | fileWriter.write("hisname = 你的 his 数据库连接名称\n"); |
| | | fileWriter.write("hisusername = 你的 his 数据库连接用户名\n"); |
| | | fileWriter.write("hispassword = 你的 his 数据库连接密码\n"); |
| | | fileWriter.write("\n"); |
| | | fileWriter.write("pacsenabled = 是否开启pacs从库数据库连接 (从数据源开关/默认关闭 fales)\n"); |
| | | fileWriter.write("pacsip = 你的pacs数据库连接ip地址\n"); |
| | | fileWriter.write("pacsprot = 你的pacs数据库连接端口\n"); |
| | | fileWriter.write("pacsname = 你的pacs数据库连接名称\n"); |
| | | fileWriter.write("pacsusername = 你的pacs数据库连接用户名\n"); |
| | | fileWriter.write("pacspassword = 你的pacs数据库连接密码\n"); |
| | | fileWriter.write("\n"); |
| | | fileWriter.write("lisenabled = 是否开启 lis 从库数据库连接 (从数据源开关/默认关闭 fales)\n"); |
| | | fileWriter.write("lisip = 你的 lis 数据库连接ip地址\n"); |
| | | fileWriter.write("lisprot = 你的 lis 数据库连接端口\n"); |
| | | fileWriter.write("lisname = 你的 lis 数据库连接名称\n"); |
| | | fileWriter.write("lisusername = 你的 lis 数据库连接用户名\n"); |
| | | fileWriter.write("lispassword = 你的 lis 数据库连接密码\n"); |
| | | fileWriter.close(); |
| | | log.info("数据库连接文件创建成功!"); |
| | | } catch (IOException ioException) { |
| | | log.info("数据库连接文件创建失败 请联系管理员手动创建!"); |
| | | ioException.printStackTrace(); |
| | | } |
| | | e.printStackTrace(); |
| | | } |
| | | props.load(fis); |
| | | fis.close(); |
| | | // 获取属性值并赋值 |
| | | Properties properties = new Properties(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | properties.setProperty("druid.url","jdbc:mysql://"+props.getProperty("ip")+":"+props.getProperty("prot")+"/"+props.getProperty("name")+"" + |
| | | "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8"); |
| | | properties.setProperty("druid.username",props.getProperty("username")); |
| | | properties.setProperty("druid.password",props.getProperty("password")); |
| | | dataSource.restart(properties); |
| | | log.info("数据库连接成功!!!"); |
| | | } catch (Exception e) { |
| | | log.info("数据库连接失败 请联系管理员!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | /** |
| | | * 去除监控页面底部的广告 |
| | | */ |
| | | @SuppressWarnings({"rawtypes", "unchecked"}) |
| | | @Bean |
| | | @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true") |
| | | public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) { |
| | | // 获取web监控页面的参数 |
| | | DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); |
| | | // 提取common.js的配置路径 |
| | | String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*"; |
| | | String commonJsPattern = pattern.replaceAll("\\*", "js/common.js"); |
| | | final String filePath = "support/http/resources/js/common.js"; |
| | | // 创建filter进行过滤 |
| | | Filter filter = new Filter() { |
| | | @Override |
| | | public void init(javax.servlet.FilterConfig filterConfig) throws ServletException { |
| | | } |
| | | |
| | | @Override |
| | | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| | | throws IOException, ServletException { |
| | | chain.doFilter(request, response); |
| | | // 重置缓冲区,响应头不会被重置 |
| | | response.resetBuffer(); |
| | | // 获取common.js |
| | | String text = Utils.readFromResource(filePath); |
| | | // 正则替换banner, 除去底部的广告信息 |
| | | text = text.replaceAll("<a.*?banner\"></a><br/>", ""); |
| | | text = text.replaceAll("powered.*?shrek.wang</a>", ""); |
| | | response.getWriter().write(text); |
| | | } |
| | | |
| | | @Override |
| | | public void destroy() { |
| | | } |
| | | }; |
| | | FilterRegistrationBean registrationBean = new FilterRegistrationBean(); |
| | | registrationBean.setFilter(filter); |
| | | registrationBean.addUrlPatterns(commonJsPattern); |
| | | return registrationBean; |
| | | } |
| | | |
| | | public void changeDataSource(Properties properties) throws SQLException { |
| | | dataSource.restart(properties); |
| | | } |
| | | } |