springboot 配置 servlet filter 2
以配置Druid为例
Servlet
@WebServlet(
urlPatterns = "/druid/*",
initParams = {
@WebInitParam(name = "loginUsername", value = "admin"),// 登录用户名
@WebInitParam(name = "loginPassword", value = "123456"),// 登录密码
@WebInitParam(name = "resetEnable", value = "false"),// 禁用HTML页面上“Reset All”功能
@WebInitParam(name = "deny", value = ""), // ip黑名单(deny优于allow),多个用,隔开
@WebInitParam(name = "allow", value = "127.0.0.1")// IP白名单(没有配置或则为空,则允许访问所有),多个用,隔开
})
public class DruidStatViewServlet extends StatViewServlet {
}
Filter
@WebFilter(
filterName = "druidWebStatFilter",
urlPatterns = {"/*"},
initParams = {
@WebInitParam(name = "exclusions", value = "*.js,*.jpg,*.png,*.gif,*.ico,*.css,/druid/*")
}
)
public class DruidStatFilter extends WebStatFilter {
}
Config
@Configuration
@ServletComponentScan // 用于扫描所有的Servlet、filter、listener
public class DruidConfiguration {
/**
* 加载时读取指定的配置信息,前缀为spring.datasource.druid
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
}