druid监控记录mysql_为Druid监控配置访问权限(配置访问监控信息的用户与密码)

Druid是一个强大的新兴数据库连接池,兼容DBCP,是阿里巴巴做的开源项目. 不仅提供了强悍的数据源实现,还内置了一个比较靠谱的监控组件。 GitHub项目主页: https://github.com/alibaba/druid QQ群: 点击链接加入群【阿里开源技术交流】 演示地址: http://cnco

Druid是一个强大的新兴数据库连接池,兼容DBCP,是阿里巴巴做的开源项目.

不仅提供了强悍的数据源实现,还内置了一个比较靠谱的监控组件。

GitHub项目主页: https://github.com/alibaba/druid

QQ群: 点击链接加入群【阿里开源技术交流】

演示地址: http://cncounter.duapp.com/druid/index.html

常见问题回答请参考: https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

一篇CSDN对Druid的介绍 druid简单教程

因为想要监控数据,又不愿意谁都可以访问,所以想要配置个密码.在开源群里一问,就知道原来内部已经有实现了.

先贴完成后的代码:

web.xml 部分:

DruidWebStatFilter

com.alibaba.druid.support.http.WebStatFilter

exclusions

*.js,*.gif,*.jpg,*.png,*.css,*.ico,*.jsp,/druid/*,/download/*

sessionStatMaxCount

2000

sessionStatEnable

true

principalSessionName

session_user_key

profileEnable

true

DruidWebStatFilter

/*

DruidStatView

com.alibaba.druid.support.http.StatViewServlet

resetEnable

true

loginUsername

druid

loginPassword

druid

DruidStatView

/druid/*

首先,因为使用的是 MAVEN, 所以查看源码时maven会自动帮你下载. 我们在 web.xml 中点击 com.alibaba.druid.support.http.StatViewServlet 进入class文件,等一会源码下载好就可以查看. 发现有类似下面这样的代码:

public class StatViewServlet extends ResourceSerlvet {

private final static Log LOG = LogFactory.getLog(StatViewServlet.class);

private static final long serialVersionUID = 1L;

public static final String PARAM_NAME_RESET_ENABLE = "resetEnable";

public static final String PARAM_NAME_JMX_URL = "jmxUrl";

public static final String PARAM_NAME_JMX_USERNAME = "jmxUsername";

public static final String PARAM_NAME_JMX_PASSWORD = "jmxPassword";

private DruidStatService statService = DruidStatService.getInstance();

/** web.xml中配置的jmx的连接地址 */

private String jmxUrl = null;

/** web.xml中配置的jmx的用户名 */

private String jmxUsername = null;

/** web.xml中配置的jmx的密码 */

private String jmxPassword = null;

.........

StatViewServlet extends ResourceSerlvet

而在其中的 jmxUrl、jmxUsername 和 jmxPassword 很显然是连接远程 JMX时使用的,那么我就想着去看看父类: com.alibaba.druid.support.http.ResourceSerlvet

@SuppressWarnings("serial")

public abstract class ResourceSerlvet extends HttpServlet {

private final static Log LOG = LogFactory.getLog(ResourceSerlvet.class);

public static final String SESSION_USER_KEY = "druid-user";

public static final String PARAM_NAME_USERNAME = "loginUsername";

public static final String PARAM_NAME_PASSWORD = "loginPassword";

public static final String PARAM_NAME_ALLOW = "allow";

public static final String PARAM_NAME_DENY = "deny";

public static final String PARAM_REMOTE_ADDR = "remoteAddress";

protected String username = null;

protected String password = null;

..........

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

......

if (isRequireAuth() //

&& !ContainsUser(request)//

&& !("/login.html".equals(path) //

|| path.startsWith("/css")//

|| path.startsWith("/js") //

|| path.startsWith("/img"))) {

if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) {

response.sendRedirect("/druid/login.html");

} else {

if ("".equals(path)) {

response.sendRedirect("druid/login.html");

} else {

response.sendRedirect("login.html");

}

}

return;

}

......

isRequireAuth() 方法,看着像是判断是否需要授权验证,于是进去看

public boolean isRequireAuth() {

return this.username != null;

}

那现在知道是 username 在作怪,也设置了,但是没有起作用,于是搜索 username ,

public void init() throws ServletException {

initAuthEnv();

}

private void initAuthEnv() {

String paramUserName = getInitParameter(PARAM_NAME_USERNAME);

if (!StringUtils.isEmpty(paramUserName)) {

this.username = paramUserName;

}

String paramPassword = getInitParameter(PARAM_NAME_PASSWORD);

if (!StringUtils.isEmpty(paramPassword)) {

this.password = paramPassword;

}

......

然后发现了初始化验证环境时使用了PARAM_NAME_USERNAME这个参数,顺便的学习了一个新API: getInitParameter 方法获取 Servlet的初始化参数, 是HttpServlet的父类 GenericServlet 类提供的:

String paramUserName = getInitParameter(PARAM_NAME_USERNAME);

那么很简单,找到 PARAM_NAME_USERNAME 即可:

public static final String PARAM_NAME_USERNAME = "loginUsername"; public static final String PARAM_NAME_PASSWORD = "loginPassword";

于是在 web.xml 中换上,OK,成功进行了拦截.

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值