java项目同一浏览器下限制用户重复登录
此内容有借鉴原文http://blog.csdn.net/huiwenjie168/article/details/7021293
//第一步
// 此监听器用来监听用户在对session做操作的时候执行相应的方法
import java.util.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.wxt.webManager.pojo.User;
public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener
{
public static Map<String,HttpSession> loginUser = new HashMap<String,HttpSession>();
public static String SESSION_LOGIN_NAME = "user";
User user=new User();
public void sessionCreated(HttpSessionEvent e)
{
HttpSession session = e.getSession();
session.setMaxInactiveInterval(600);
}
public void sessionDestroyed(HttpSessionEvent e)
{
try{
loginUser.remove(e.getSession());
}catch(Exception et){
et.printStackTrace();
}
}
public void attributeAdded(HttpSessionBindingEvent e)
{
if(e.getName().equals(SESSION_LOGIN_NAME)){
loginUser.put(user.getNickname(),e.getSession());
}
}
public void attributeRemoved(HttpSessionBindingEvent e)
{
if(e.getName().equals(SESSION_LOGIN_NAME)){
try{
System.out.println("session 移除!"+e.getSession());
loginUser.remove(e.getSession());
}catch(Exception et){
}
}
}
public void attributeReplaced(HttpSessionBindingEvent e)
{
if(e.getName().equals(SESSION_LOGIN_NAME)){
loginUser.put(user.getNickname(),e.getSession());System.out.println("session 替换!");
}
}
//判断用户是否已经登陆
public boolean isLogonUser(HttpSession userSession) {Set<String> keys = MySessionListener.loginUser.keySet();
for (String key : keys) {
if (loginUser.get(key).equals(userSession)) {
System.out.println("该用户已登录");
return true;
}
}
System.out.println("该用户未登录");
return false;
}
}
//到的web.xml中去配置listener
<listener>
<listener-class>com.wxt.webManager.listeners.MySessionListener</listener-class>
</listener>
//第二步
//验证用户名、密码都正确后,再调用isLogonUser方法,参数为用户session;true则表示该用户已经登陆
public String execute() throws Exception
{
if (this.imgcode.equalsIgnoreCase(ServletActionContext.getRequest() .getSession().getAttribute("rand").toString())) {
String pwd = new MD5Code().getMD5ofStr(this.user.getPassword());
Map map = this.userService.isLogin(this.user.getNickname(), pwd);
Boolean bool = new MySessionListener().isLogonUser(ServletActionContext.getRequest().getSession());
System.out.println("状态:"+bool);
User u = (User)map.get("user");
if (u == null) {
super.addActionError("用户名或密码错误");
return "input";
}
if(bool){
super.addActionError("该用户已登录");
return "input";
}else{
ServletActionContext.getRequest().getSession().setAttribute("user", u);
}
//第三步
//用户窗口关闭/或者用户退出的时候,*一定要 request.getSession().invalidate()
//用户窗口关闭js
//关闭窗口时调用此方法
function window.onunload(){
if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey)
{
//清除当前session,使用jquery 提供的方法
$.post("${base}/ClearSession.wp");
// [ ${base}/ClearSession.wp ]这是一个请求,
//请求到自己写的ClearSessionServlet
// 在此ClearSessionServlet中重写doPost方法,
// 内容为 request.getSession().invalidate()
}
//写完此代码,就知道如何实现 一个用户登陆 踢掉之前登陆的用户了
暂还不能实现不同浏览器或不同机器访问,限值用户重复登录。 或许可以找些关于腾讯QQ登录限制的文章继续研究,这个不是很容易做好的。
- 上一篇: python常用的十进制、16进制、字符串、字节串之间的转换
- 下一篇: Vue中v-for的数据分组
