• spring监控配置

@Configuration@EnableAspectJAutoProxy(proxyTargetClass = true)**public class DruidAspectConfig {

  1. @Bean
  2. public DruidStatInterceptor druidStatInterceptor() {
  3. DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
  4. return dsInterceptor;
  5. }
  6. @Bean
  7. @Scope("prototype")
  8. public JdkRegexpMethodPointcut druidStatPointcut() {
  9. JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
  10. pointcut.setPatterns("com.xxx.dao.*","com.xxx.service.*");
  11. return pointcut;
  12. }
  13. @Bean
  14. public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
  15. DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
  16. defaultPointAdvisor.setPointcut(druidStatPointcut);
  17. defaultPointAdvisor.setAdvice(druidStatInterceptor);
  18. return defaultPointAdvisor;
  19. }

}

  • sql监控配置

@Configurationpublic class DruidConfig {

  1. @Bean
  2. public Slf4jLogFilter logFilter () {
  3. Slf4jLogFilter logFilter = new Slf4jLogFilter();
  4. logFilter.setStatementExecutableSqlLogEnable(true);
  5. logFilter.setStatementLogEnabled(false);
  6. return logFilter;
  7. }
  8. @Bean
  9. public StatFilter statFilter () {
  10. StatFilter statFilter = new StatFilter();
  11. statFilter.setSlowSqlMillis(3000);
  12. statFilter.setLogSlowSql(true);
  13. statFilter.setMergeSql(true);
  14. return statFilter;
  15. }
  16. /**
  17. * sql防火墙过滤器配置
  18. * @param wallConfig
  19. * @return
  20. */
  21. @Bean
  22. public WallFilter wallFilter (WallConfig wallConfig) {
  23. WallFilter wallFilter = new WallFilter();
  24. wallFilter.setConfig(wallConfig);
  25. wallFilter.setLogViolation(true);//对被认为是攻击的SQL进行LOG.error输出
  26. wallFilter.setThrowException(false);//对被认为是攻击的SQL抛出SQLException
  27. return wallFilter;
  28. }
  29. /**
  30. * sql防火墙配置
  31. * @return
  32. */
  33. @Bean
  34. public WallConfig wallConfig () {
  35. WallConfig wallConfig = new WallConfig();
  36. wallConfig.setAlterTableAllow(false);
  37. wallConfig.setCreateTableAllow(false);
  38. wallConfig.setDeleteAllow(false);
  39. wallConfig.setMergeAllow(false);
  40. wallConfig.setDescribeAllow(false);
  41. wallConfig.setShowAllow(false);
  42. return wallConfig;
  43. }

}

  • 注意引入aspectj支持,否则会报错

    org.aspectj aspectjrt xxx org.aspectj aspectjweaver xxxx