mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
添加登录功能
This commit is contained in:
@@ -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<IPRange> allowList = new ArrayList<IPRange>();
|
||||
private List<IPRange> denyList = new ArrayList<IPRange>();
|
||||
|
||||
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,
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>druid 监控</title>
|
||||
<link href='css/bootstrap/css/bootstrap.min.css' rel="stylesheet" >
|
||||
<script type="text/javascript" src="js/jquery.min.js"></script>
|
||||
<style type="text/css">
|
||||
/* Override some defaults */
|
||||
html, body {
|
||||
background-color: #eee;
|
||||
}
|
||||
body {
|
||||
padding-top: 40px;
|
||||
}
|
||||
.container {
|
||||
width: 300px;
|
||||
}
|
||||
|
||||
/* The white background content wrapper */
|
||||
.container > .content {
|
||||
background-color: #fff;
|
||||
padding: 20px;
|
||||
margin: 0 -20px;
|
||||
-webkit-border-radius: 10px 10px 10px 10px;
|
||||
-moz-border-radius: 10px 10px 10px 10px;
|
||||
border-radius: 10px 10px 10px 10px;
|
||||
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
|
||||
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
|
||||
box-shadow: 0 1px 2px rgba(0,0,0,.15);
|
||||
}
|
||||
|
||||
.login-form {
|
||||
margin-left: 65px;
|
||||
}
|
||||
|
||||
legend {
|
||||
margin-right: -50px;
|
||||
font-weight: bold;
|
||||
color: #404040;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="content">
|
||||
<div class="row">
|
||||
<div class="login-form">
|
||||
<h2>Login</h2>
|
||||
<form id="loginForm" method="post" autocomplete="off" >
|
||||
<fieldset>
|
||||
<div id="alertInfo" class="alert alert-error clearfix" style="margin-bottom: 5px;width: 195px; padding: 2px 15px 2px 10px;display: none;">
|
||||
用户名或密码不正确
|
||||
</div>
|
||||
<div class="clearfix">
|
||||
<input type="text" placeholder="用户名" name="login_username" >
|
||||
</div>
|
||||
<div class="clearfix">
|
||||
<input type="password" placeholder="密码" name="login_password">
|
||||
</div>
|
||||
<button id="loginBtn" class="btn btn-primary" type="button">登 录</button>
|
||||
<button class="btn" type="reset">重 置</button>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div> <!-- /container -->
|
||||
<script type="text/javascript">
|
||||
$.namespace("druid.login");
|
||||
druid.login = function () {
|
||||
return {
|
||||
login : function() {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: "submitLogin",
|
||||
data: $("#loginForm").serialize(),
|
||||
success: function(data) {
|
||||
if("success" == data)
|
||||
location.href = "index.html";
|
||||
else {
|
||||
$("#alertInfo").show();
|
||||
$("#loginForm")[0].reset();
|
||||
}
|
||||
},
|
||||
dataType: "text"
|
||||
});
|
||||
}
|
||||
}
|
||||
}();
|
||||
|
||||
$(document).ready(function() {
|
||||
$("#loginBtn").click(druid.login.login);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user