java确定同一用户登录_java保持同一时间同一账号只能在一处登录

//控制类

package com.zjn.oneLogin.dengLu;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class LoginController{

/**

* 用户和Session绑定关系

*/

public static final Map USER_SESSION = new HashMap();

/**

* seeionId和用户的绑定关系

*/

public static final Map SESSIONID_USER = new HashMap();

@RequestMapping("/login")

@ResponseBody

public MaptoLogin(HttpServletRequest request, HttpServletResponse response) throws Exception{

//获取请求命令

request.setCharacterEncoding("utf-8");

String servletPath = request.getServletPath();//获取请求路径

String uri = servletPath.substring(1, servletPath.lastIndexOf(".do"));

try{

//登录

if ("login".equals(uri)){

HttpSession session = request.getSession();

String userName = request.getParameter("userName");

String password = request.getParameter("password");

if (userName != null && !"".equals(userName.trim())){

//登录成功

if (login(userName, password)){

//处理用户登录(保持同一时间同一账号只能在一处登录)

userLoginHandle(request);

//添加用户与HttpSession的绑定

USER_SESSION.put(userName.trim(), session);

//添加sessionId和用户的绑定

SESSIONID_USER.put(session.getId(), userName);

System.out.println("添加sessionId和用户的绑定 ==="+session.getId());

System.out.println("用户[" + userName + "] 已上线...");

session.setAttribute("userName", userName);

session.removeAttribute("userMsg");

//重定向到首页

response.sendRedirect("main.jsp");

System.out.println("USER_SESSION==="+USER_SESSION);

System.out.println("SESSIONID_USER==="+SESSIONID_USER);

}

//登录失败

else{

System.out.println("用户[" + userName + "] 登录失败...");

request.setAttribute("msg", "登录失败,请重新登录!");

//response.sendRedirect("login.jsp");

request.getRequestDispatcher("login.jsp").forward(request, response);

}

}

else{

System.out.println("用户[" + userName + "] 登录失败...");

request.setAttribute("msg", "登录失败,请重新登录!");

//response.sendRedirect("login.jsp");

request.getRequestDispatcher("login.jsp").forward(request, response);

}

}else if ("reLogin".equals(uri)){

//重新登陆

HttpSession session = request.getSession();

String userName = (String)session.getAttribute("userName");

if (session != null){

//销毁相关session

//USER_SESSION.remove(SESSIONID_USER.remove(session.getId()));

session.invalidate();

}

if (userName != null && !"".equals(userName)){

System.out.println("用户[" + userName + "] 已下线...");

}

//重定向到登录页面

response.sendRedirect("login.jsp");

}

//ajax校验

else if ("getUserMsg".equals(uri)){

HttpSession session = request.getSession();

response.setContentType("text/html");

response.setCharacterEncoding("utf-8");

PrintWriter out = response.getWriter();

out.print(session.getAttribute("userMsg"));

}

}

catch (Exception e){

System.out.println(e.getClass() + e.getMessage());

PrintWriter out = response.getWriter();

out.print("服务器内部错误!");

}

return null;

}

/**

*

* Description:用户登录时的处理

* @param request

* @see

*/

private void userLoginHandle(HttpServletRequest request){

//当前登录的用户

String userName = request.getParameter("userName");

System.out.println("userName=="+userName);

//当前sessionId

//String sessionId = request.getSession().getId();

//删除当前sessionId绑定的用户,用户--HttpSession

//USER_SESSION.remove(SESSIONID_USER.remove(sessionId));

//删除当前登录用户已绑定的HttpSession

System.out.println("Vlue-userName==="+USER_SESSION.get("userName"));

HttpSession session = USER_SESSION.remove(userName);

if (session != null){

//删除已登录的sessionId绑定的用户

SESSIONID_USER.remove(session.getId());

session.removeAttribute("userName");

session.setAttribute("userMsg", "您的账号已经在另一处登录,您被迫下线!");

}

}

/**

*

* Description: 模拟DB登录判断

* @param userName 用户

* @param password 密码

* @return

* @see

*/

private boolean login(String userName, String password){

return ("peizhongxian".equals(userName) && "123456".equals(password));

}

}

//监听类

package com.zjn.oneLogin.dengLu;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

public class MyListener implements HttpSessionListener {

/**

* 实现HttpSessionListener接口监听 监听session的创建事件

*/

public void sessionCreated(HttpSessionEvent se){

String sessionId = se.getSession().getId();

System.out.println("创建session sessionId= " + sessionId);

}

/**

* 实现HttpSessionListener接口监听 监听session的销毁事件

*/

public void sessionDestroyed(HttpSessionEvent se){

String sessionId = se.getSession().getId();

System.out.println("sessionId========="+sessionId);

//当前session销毁时删除当前session绑定的用户信息

//同时删除当前session绑定用户的HttpSession

LoginController.USER_SESSION.remove(LoginController.SESSIONID_USER.remove(sessionId));

System.out.println("销毁session sessionId= " + sessionId);

}

}

标签:userName,java,账号,request,sessionId,session,USER,同一时间,out

来源: https://www.cnblogs.com/xianz666/p/13824331.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值