懒羊羊
2024-03-23 5239031423deb80a31ac328838240adc51594804
提交 | 用户 | 时间
e57a89 1 package com.jcdm.framework.config;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6 import javax.servlet.Filter;
7 import javax.servlet.FilterChain;
8 import javax.servlet.ServletException;
9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.sql.DataSource;
12 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
13 import org.springframework.boot.context.properties.ConfigurationProperties;
14 import org.springframework.boot.web.servlet.FilterRegistrationBean;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.context.annotation.Configuration;
17 import org.springframework.context.annotation.Primary;
18 import com.alibaba.druid.pool.DruidDataSource;
19 import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
20 import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
21 import com.alibaba.druid.util.Utils;
22 import com.jcdm.common.enums.DataSourceType;
23 import com.jcdm.common.utils.spring.SpringUtils;
24 import com.jcdm.framework.config.properties.DruidProperties;
25 import com.jcdm.framework.datasource.DynamicDataSource;
26
27 /**
28  * druid 配置多数据源
29  * 
30  * @author jc
31  */
32 @Configuration
33 public class DruidConfig
34 {
35     @Bean
36     @ConfigurationProperties("spring.datasource.druid.master")
37     public DataSource masterDataSource(DruidProperties druidProperties)
38     {
39         DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
40         return druidProperties.dataSource(dataSource);
41     }
42
43     @Bean
44     @ConfigurationProperties("spring.datasource.druid.slave")
45     @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
46     public DataSource slaveDataSource(DruidProperties druidProperties)
47     {
48         DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
49         return druidProperties.dataSource(dataSource);
50     }
51
52     @Bean(name = "dynamicDataSource")
53     @Primary
54     public DynamicDataSource dataSource(DataSource masterDataSource)
55     {
56         Map<Object, Object> targetDataSources = new HashMap<>();
57         targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
58         setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
59         return new DynamicDataSource(masterDataSource, targetDataSources);
60     }
61     
62     /**
63      * 设置数据源
64      * 
65      * @param targetDataSources 备选数据源集合
66      * @param sourceName 数据源名称
67      * @param beanName bean名称
68      */
69     public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName)
70     {
71         try
72         {
73             DataSource dataSource = SpringUtils.getBean(beanName);
74             targetDataSources.put(sourceName, dataSource);
75         }
76         catch (Exception e)
77         {
78         }
79     }
80
81     /**
82      * 去除监控页面底部的广告
83      */
84     @SuppressWarnings({ "rawtypes", "unchecked" })
85     @Bean
86     @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
87     public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
88     {
89         // 获取web监控页面的参数
90         DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
91         // 提取common.js的配置路径
92         String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
93         String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
94         final String filePath = "support/http/resources/js/common.js";
95         // 创建filter进行过滤
96         Filter filter = new Filter()
97         {
98             @Override
99             public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
100             {
101             }
102             @Override
103             public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
104                     throws IOException, ServletException
105             {
106                 chain.doFilter(request, response);
107                 // 重置缓冲区,响应头不会被重置
108                 response.resetBuffer();
109                 // 获取common.js
110                 String text = Utils.readFromResource(filePath);
111                 // 正则替换banner, 除去底部的广告信息
112                 text = text.replaceAll("<a.*?banner\"></a><br/>", "");
113                 text = text.replaceAll("powered.*?shrek.wang</a>", "");
114                 response.getWriter().write(text);
115             }
116             @Override
117             public void destroy()
118             {
119             }
120         };
121         FilterRegistrationBean registrationBean = new FilterRegistrationBean();
122         registrationBean.setFilter(filter);
123         registrationBean.addUrlPatterns(commonJsPattern);
124         return registrationBean;
125     }
126 }