package com.gonghang.h5clientapp.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;

import javax.sql.DataSource;

/**
 * 访问地址：http://localhost:8089/druid/login.html
 * Updated by shiwn on 2021/11/22 10:19
 */
@Configuration
public class DruidConfig {
    /**
     * 将所有前缀为spring.datasource下的配置项都加载DataSource中
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druid() {
        return new DruidDataSource();
    }

    //  配置druid的监控
    @Bean
    public ServletRegistrationBean registrationBean() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 白名单，默认是允许所有的访问
        registrationBean.addInitParameter("allow", "localhost");
        // 黑名单
        registrationBean.addInitParameter("deny", "");
        // 用户名
        registrationBean.addInitParameter("loginUsername", "admin");
        // 密码
        registrationBean.addInitParameter("loginPassword", "123456");
        // 是否可以重置数据源
        registrationBean.addInitParameter("resetEnable", "false");
        return registrationBean;
    }


    /**
     * 配置监控的filter
     */
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        // 所有请求进行监控处理
        bean.addUrlPatterns("/*");
        // 排除名单
        bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return bean;
    }
}
