All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pongsky.kit.config.DruidConfig Maven / Gradle / Ivy

There is a newer version: 3.10.1-2.4.13
Show newest version
package com.pongsky.kit.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.util.Utils;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.io.IOException;

/**
 * druid 配置
 *
 * @author pengsenhao
 */
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnClass(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
public class DruidConfig {

    /**
     * 去除 druid 监控底部广告
     *
     * @return 拦截器
     * @throws IOException IOException
     * @author pengsenhao
     */
    @Bean
    public FilterRegistrationBean removeDruidAdvertiseFilter() throws IOException {
        String text = Utils.readFromResource("support/http/resources/js/common.js");
        text = text.replaceAll("this.buildFooter\\(\\);", "");
        FilterRegistrationBean registration = new FilterRegistrationBean<>();
        registration.addUrlPatterns("/druid/js/common.js");
        String finalText = text;
        registration.setFilter((servletRequest, servletResponse, filterChain) -> {
            servletResponse.resetBuffer();
            servletResponse.setContentType("text/javascript;charset=utf-8");
            servletResponse.getWriter().write(finalText);
        });
        return registration;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy