druid資料庫連線池的配置類

FH-Admin發表於2022-01-18
package org.fh;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 說明:druid資料庫連線池的配置類(必須與啟動類包目錄同級)
 * 作者:FH Admin
 * from:fhadmin.cn
 */
@Configuration
public class DruidConfiguration {

    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet() {
        // 建立servlet註冊實體
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet(), "/druid/*");
        // 設定ip白名單
        //servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        // 設定ip黑名單,如果allow與deny共同存在時,deny優先於allow
        //servletRegistrationBean.addInitParameter("deny", "192.168.0.1");
        // 設定控制檯管理使用者
        //servletRegistrationBean.addInitParameter("loginUsername", "druid");
        //servletRegistrationBean.addInitParameter("loginPassword", "fhadmin");
        // 是否可以重置資料
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<WebStatFilter> statFilter() {
        // 建立過濾器
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>(new WebStatFilter());
        // 設定過濾器過濾路徑
        filterRegistrationBean.addUrlPatterns("/*");
        // 忽略過濾的形式
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章