springboot 报whitelabel Error Page错误的可能原因

时间:2024-03-21 15:18:13

初学springboot,用类而不是配置文件的方式注册servlet与filter ,然后访问localhost:8080/druid/index.html时报whitelabel Error Page错误whitelabel Error Page,我的类配置类的代码如下

缺少必要的注解:发现注册 servlet的druidStatViewServlet方法上少了@Bean标签如下

whitelabel Error Page错误的原因还有可能是Application启动类的位置不对.要将Application类放在最外侧,即包含所有子包 
原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.

@Configuration
public class DruidConfiguration {
    
    public ServletRegistrationBean druidStatViewServlet(){
        //ServletRegistrationBean提供类的进行注册
        ServletRegistrationBean servletRegistrationBean =
                new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //添加初始化参数:initParams
        //白名单:
        servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //IP黑名单(同时存在时,deny优先于allow)
        //如果满足deny,就提示:sorry,you are not permitted to view this page
        servletRegistrationBean.addInitParameter("deny","192.168.1.73");
        //登录查看信息的账号和密码
        servletRegistrationBean.addInitParameter("loginUsername","admin");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean druidStatFilter() {
        FilterRegistrationBean filterRegistrationBean =
                new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则
        filterRegistrationBean.addUrlPatterns("/*");
        //添加需要忽略的格式信息
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif," +
                "*.jpg,*.png, *.css,*.ico,/druid/*");
        return filterRegistrationBean;

    }
}

最后结果得以成功访问 

springboot 报whitelabel Error Page错误的可能原因