From bf29e26a6ad6ff8be93516adea13a2fcf1d568c3 Mon Sep 17 00:00:00 2001 From: melin Date: Thu, 23 Aug 2012 17:31:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=99=BB=E5=BD=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../druid/support/http/StatViewServlet.java | 87 +++++++++++----- .../support/http/resources/login.html | 99 +++++++++++++++++++ 2 files changed, 160 insertions(+), 26 deletions(-) create mode 100644 src/main/resources/support/http/resources/login.html diff --git a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java index 164982c310..fe80e01458 100644 --- a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java +++ b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java @@ -8,6 +8,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import com.alibaba.druid.stat.DruidStatService; import com.alibaba.druid.support.http.util.IPAddress; @@ -30,18 +31,34 @@ public class StatViewServlet extends HttpServlet { public static final String PARAM_NAME_RESET_ENABLE = "resetEnable"; public static final String PARAM_NAME_ALLOW = "allow"; public static final String PARAM_NAME_DENY = "deny"; + + public static final String PARAM_NAME_USERNAME = "login_username"; + public static final String PARAM_NAME_PASSWORD = "login_password"; + public static final String SESSION_USER_KEY = "druid-user"; private final static String RESOURCE_PATH = "support/http/resources"; private final static String TEMPLATE_PAGE_RESOURCE_PATH = RESOURCE_PATH + "/template.html"; - private DruidStatService statService = DruidStatService.getInstance(); + private DruidStatService statService = DruidStatService.getInstance(); public String templatePage; private List allowList = new ArrayList(); private List denyList = new ArrayList(); + + private String username = "admin"; + private String password = "admin"; public void init() throws ServletException { + String _username = getInitParameter(PARAM_NAME_USERNAME); + String _password = getInitParameter(PARAM_NAME_PASSWORD); + + if (_username != null && _username.trim().length() != 0 && + _password != null && _password.trim().length() != 0) { + username = _username; + password = _password; + } + try { templatePage = IOUtils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH); @@ -138,10 +155,11 @@ public class StatViewServlet extends HttpServlet { } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String contextPath = request.getContextPath(); - String servletPath = request.getServletPath(); + HttpSession session = request.getSession(); + String contextPath = request.getContextPath(); + String servletPath = request.getServletPath(); String requestURI = request.getRequestURI(); - + response.setCharacterEncoding("utf-8"); if (contextPath == null) { // root context @@ -149,38 +167,55 @@ public class StatViewServlet extends HttpServlet { } String uri = contextPath + servletPath; String path = requestURI.substring(contextPath.length() + servletPath.length()); - + if (!isPermittedRequest(request)) { path = "/nopermit.html"; returnResourceFile(path, uri, response); return; } - - if ("".equals(path)) { - if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) { - response.sendRedirect("/druid/index.html"); + + if ("/submitLogin".equals(path)) { + String _username = request.getParameter(PARAM_NAME_USERNAME); + String _password = request.getParameter(PARAM_NAME_PASSWORD); + if(username.equals(_username) && password.equals(_password)) { + request.getSession().setAttribute(SESSION_USER_KEY, username); + response.getWriter().print("success"); + } else + response.getWriter().print("error"); + } else if (session.getAttribute(SESSION_USER_KEY) == null && !("/login.html".equals(path) || path.startsWith("/css") || path.startsWith("/js"))) { + if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) { + response.sendRedirect("/login.html"); } else { - response.sendRedirect("druid/index.html"); + response.sendRedirect("login.html"); } - return; - } + } else { - if ("/".equals(path)) { - response.sendRedirect("index.html"); - return; - } - - if (path.indexOf(".json") >= 0) { - String fullUrl = path; - if (request.getQueryString() != null && request.getQueryString().length() > 0) { - fullUrl += "?" + request.getQueryString(); + if ("".equals(path)) { + if (contextPath == null || contextPath.equals("") || contextPath.equals("/")) { + response.sendRedirect("/druid/index.html"); + } else { + response.sendRedirect("druid/index.html"); + } + return; } - response.getWriter().print(statService.service(fullUrl)); - return; - } - // find file in resources path - returnResourceFile(path, uri, response); + if ("/".equals(path)) { + response.sendRedirect("index.html"); + return; + } + + if (path.indexOf(".json") >= 0) { + String fullUrl = path; + if (request.getQueryString() != null && request.getQueryString().length() > 0) { + fullUrl += "?" + request.getQueryString(); + } + response.getWriter().print(statService.service(fullUrl)); + return; + } + + // find file in resources path + returnResourceFile(path, uri, response); + } } private void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, diff --git a/src/main/resources/support/http/resources/login.html b/src/main/resources/support/http/resources/login.html new file mode 100644 index 0000000000..a146f6f751 --- /dev/null +++ b/src/main/resources/support/http/resources/login.html @@ -0,0 +1,99 @@ + + + + + druid 监控 + + + + + + +
+
+
+ +
+
+
+ + + \ No newline at end of file