¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.config; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import javax.servlet.Filter; |
| | | import javax.servlet.FilterChain; |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.ServletRequest; |
| | | import javax.servlet.ServletResponse; |
| | | import javax.sql.DataSource; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | 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 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 com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.framework.aspectj.lang.enums.DataSourceType; |
| | | import com.ruoyi.framework.config.properties.DruidProperties; |
| | | import com.ruoyi.framework.datasource.DynamicDataSource; |
| | | |
| | | /** |
| | | * druid é
ç½®å¤æ°æ®æº |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Configuration |
| | | public class DruidConfig |
| | | { |
| | | @Bean |
| | | @ConfigurationProperties("spring.datasource.druid.master") |
| | | public DataSource masterDataSource(DruidProperties druidProperties) |
| | | { |
| | | DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | @Bean |
| | | @ConfigurationProperties("spring.datasource.druid.slave") |
| | | @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") |
| | | public DataSource slaveDataSource(DruidProperties druidProperties) |
| | | { |
| | | DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |
| | | @Bean(name = "dynamicDataSource") |
| | | @Primary |
| | | public DynamicDataSource dataSource(DataSource masterDataSource) |
| | | { |
| | | Map<Object, Object> targetDataSources = new HashMap<>(); |
| | | targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); |
| | | setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); |
| | | return new DynamicDataSource(masterDataSource, targetDataSources); |
| | | } |
| | | |
| | | /** |
| | | * è®¾ç½®æ°æ®æº |
| | | * |
| | | * @param targetDataSources å¤éæ°æ®æºéå |
| | | * @param sourceName æ°æ®æºåç§° |
| | | * @param beanName beanåç§° |
| | | */ |
| | | public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName) |
| | | { |
| | | try |
| | | { |
| | | DataSource dataSource = SpringUtils.getBean(beanName); |
| | | targetDataSources.put(sourceName, dataSource); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å»é¤çæ§é¡µé¢åºé¨ç广å |
| | | */ |
| | | @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; |
| | | } |
| | | } |