1. 程式人生 > >基於session的單點登陸

基於session的單點登陸

1、第一次訪問,登入成功

(1)登陸介面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="jquery-3.2.1/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
	function login() {
	
		var userInfo = {
				"uname":$("#userName").val(),
				"upsw":$("#userPwd").val()
				};
		$.getJSON("UserLoginServlet.action",userInfo,function(data){
			if(data.success){
					//需要完善
				  window.location.href = "${pageContext.request.contextPath}/HouseInfoShowServlet.action";
			}else{
				alert("使用者名稱或密碼錯誤,請重新輸入");
				$("#userName").focus();
			}
		});
	}
</script>
<title>Insert title here</title>
</head>
<body>
<div>
	<input type="text" id="userName"/><br/>
	<input type="password" id="userPwd"/><br/>
	<button onclick="login()">登入</button>
</div>
</body>
</html>


2、根據使用者名稱和密碼檢查是否有該session,如果有session中存入已登入資訊,覆蓋session

protected void doPost(HttpServletRequest request, HttpServletResponse response)              throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String name=request.getParameter("Userame");
		String password=request.getParameter("password");
		System.out.println(name);
		System.out.println(password);
		//List<UserInfo> lists = Methods.selectNamePwd();
		UserInfo  info = Methods.selectNamePwd(name, password);
		ResultMsg result;
		if(info!=null) {
			checkUser(request);
			info.setUserName(name);
			HttpSession session = request.getSession();
			session.setAttribute("info", info);
			response.sendRedirect("HouseShow.action");
			result = ResultMsg.success();	
		}else{
			result = ResultMsg.failure("輸入的密碼不正確!請查證!");
			request.getRequestDispatcher("login.html").forward(request, response);
		}
		response.getWriter().write(JSON.toJSONString(result));
		/*for(UserInfo s2:lists) {
			String name1 = s2.getUserName();
			String password2 = s2.getUserPwd();
			if(name.equals(name1)&&password.equals(password2)) {
				response.sendRedirect("HouseShow");
			}else {
				request.getRequestDispatcher("login.html");
			}
		}*/
	}


3、如果沒有新增session

private void checkUser(HttpServletRequest request) {
		String userName = request.getParameter("Userame");
		String userPwd = request.getParameter("password");
		//獲取上次登入的session(如果有的話)
		HttpSession session = SessionInfo.USER_SESSION.get(userName+userPwd);
		if(session!=null) {
			//在上次登入的session中放入一條資訊
			session.setAttribute("msg", ResultMsg.failure("該賬號在另一處登入,是否重新登入"));
		}
		//此處有替換session的功能
		SessionInfo.USER_SESSION.put(userName+userPwd, request.getSession());
	}


4、跳轉到data.jsp介面

request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		UserInfo info =  (UserInfo) session.getAttribute("info");
		System.out.println(info.getUserName());
		String userName = info.getUserName();
		List<HouseInfo> houseInfos  = Methods.selectHouse(userName);
		session.setAttribute("houseInfos",houseInfos);		
		request.getRequestDispatcher("/WEB-INF/data.jsp").forward(request, response);


5、在該介面中迴圈訪問checkLogin,獲取資訊

$(function() {
	setInterval(checkUserOnline, 5*1000);
});
function checkUserOnline() {
	$.getJSON("UserCheckLoginServlet.action",function(data){
		if(!data.success){
			alert(data.msg);
			//登出
			 window.location.href = "${pageContext.request.contextPath}/UserLogoutServlet.action";
		}
	});
}


6、如果發現已登入,跳轉到登出servlet中

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		ResultMsg msg = (ResultMsg) request.getSession().getAttribute("msg");
		//表示未出現重複登入情況
		if(msg == null) {
			msg = ResultMsg.success();
		}
		response.getWriter().write(JSON.toJSONString(msg));
	}


6、在登出servlet中,使session實效,跳轉到login.jsp

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//登出以後把 物件移除
		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute("info");
		if(session == SessionInfo.USER_SESSION.get(userInfo.getUserName()+userInfo.getUserPwd())) {
			SessionInfo.USER_SESSION.remove(userInfo.getUserName()+userInfo.getUserPwd());
		}
		//移除session
		session.invalidate();
		  //重定向到登入頁面login.jsp
        response.sendRedirect(request.getContextPath()+"/login.html");
	}