<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Learn LMS - Login</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/font-awesome.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/login-page.css">
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="main">
<div id="container">
<!-- 1. Logo section -->
<div id="logo_center" width="80%">
<br>
<p><br><br>
<font color="#1c6299" size="7" face="Arial"><b>Le</b></font><font color="#15a2b3" size="7" face="Helvetica"><b>tech</b></font>
<font color="#5d5b5d" size="6" face="Calibri"> Smart Lighting Management System</font>
</p>
</div>
<!-- 2. Green divider section -->
<p><br>
<div id="green_bar"></div>
</p>
<!-- 3. Login section -->
<br><br>
<div id="login" style="margin: 0 auto; display: table; ">
<form action="${pageContext.request.contextPath}/user/loginUser.action" method="post" autocomplete="off">
<fieldset class="clearfix">
<p>
<span class="fa fa-user"></span>
<input type="text" Placeholder="Username" id="userName" name="userName" required>
</p>
<!-- JS because of IE support; better: placeholder="Username" -->
<p>
<span class="fa fa-lock"></span>
<input type="password" Placeholder="Password" id="userPassword" name="userPassword" required> ${loginInfo }
</p>
<p>
<input type="submit" value="Sign In" id="login_button">
</p>
</fieldset>
</form>
</div>
</div>
<!-- 4. Bottom section -->
<div id="bottom">
<p><br>
<font color="#999">Copyright © 1986 - 2018</font> <b>Learn Group Ltd. All rights reserved.</b>
</p>
</div>
</body>
</html>
package com.learn.controller;
import com.learn.entity.BlockDTO;
import com.learn.entity.UserAccount;
import com.learn.service.LoginService;
import java.io.IOException;
import java.io.PrintStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController
{
@Autowired
private LoginService loginService;
@RequestMapping({"/user/loginUser"})
public String loginUser(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
HttpSession session = request.getSession();
String userName = request.getParameter("userName");
userName = userName != null ? userName.trim() : "";
String userPassword = request.getParameter("userPassword");
userPassword = userPassword != null ? userPassword.trim() : "";
Map map = new HashMap();
map.put("userName", userName);
map.put("userPassword", md5(userPassword));
UserAccount currentUser = this.loginService.login(map);
ModelAndView mav = new ModelAndView();
if ((currentUser != null) && (userName != "") && (userPassword != ""))
{
session.setAttribute("currentUser", currentUser);
String userId = currentUser.getUserId().toString();
userId = userId != null ? userId.trim() : "";
String ip = request.getHeader("X-Forwarded-For");
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getHeader("Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
ip = request.getRemoteAddr();
}
ip = ip != null ? ip.trim() : "";
System.out.println("userID: " + userId + ", username: " + userName + ", fromIP: " + ip);
Map map2 = new HashMap();
map2.put("userId", userId);
map2.put("fromIp", ip);
return "redirect:/community/dashboard.action";
}
mav.setViewName("login");
request.setAttribute("loginInfo", "Username or password is wrong!");
return "forward:/login_bl.jsp";
}
@RequestMapping({"/user/logout.action"})
public String loginout(HttpServletRequest request)
{
HttpSession session = request.getSession();
session.setAttribute("currentUser", null);
session.invalidate();
return "forward:/login_bl.jsp";
}
@RequestMapping({"/smartControl.action"})
public void getSmartControlData(HttpServletRequest request, HttpServletResponse response)
{
List rows = new ArrayList();
BlockDTO block = new BlockDTO();
block.setBlockId("1");
rows.add(block);
BlockDTO block2 = new BlockDTO();
block2.setBlockId("2");
rows.add(block2);
request.setAttribute("rows", rows);
try {
request.getRequestDispatcher("/sps100.jsp").forward(request, response);
} catch (ServletException|IOException e) {
e.printStackTrace();
}
}
private String md5(String s) {
String type = "MD5";
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte[] messageDigest = digest.digest();
StringBuilder hexStr = new StringBuilder();
for (byte md : messageDigest) {
StringBuilder temp = new StringBuilder(Integer.toHexString(0xFF & md));
while (temp.length() < 2)
temp.insert(0, "0");
hexStr.append(temp);
}
return hexStr.toString().toUpperCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
}