JavaWeb-实现多ip、异地 同时登录踢人下线

public static synchronized HttpSession getSession(String session_id) {

if (session_id == null)

return null;

return (HttpSession)mymap.get(session_id);

}

public static HashMap<String, HttpSession> getMymap() {

return mymap;

}

}

Session监听器

public class SessionCounter implements HttpSessionListener {

private static int activeSessions = 0;

public void sessionCreated(HttpSessionEvent se) {

MySessionContext.AddSession(se.getSession());

activeSessions++;

System.out.println(“++++++++玩家上线了++++++++”);

}

public void sessionDestroyed(HttpSessionEvent se) {

if(activeSessions > 0)

activeSessions–;

HttpSession session = se.getSession();

MySessionContext.DelSession(session);

}

public static int getActiveSessions() {

return activeSessions;

}

}

踢人下线过滤器核心代码

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request =  (HttpServletRequest)req;

HttpServletResponse response =  (HttpServletResponse)resp;

String localIp = request.getRemoteAddr();//获取本地ip

HttpSession session = null;

String user_id = (String)request.getParameter(“userId”); //登录请求时填写的

if(StringUtils.isNotBlank(user_id))

{

session = getLoginedUserSession(user_id);

}

String loginedIp = null;

if(session!=null)

{

loginedIp = (String)session.getAttribute(ESessionKey.LoginIP.key);//获取已登录者ip(如果有)

}

if(StringUtils.isNotBlank(loginedIp) && !localIp.equals(loginedIp)){

MySessionContext.DelSession(session);//踢人–找到并销毁Session

request.setAttribute(“msg”, “您的账号在其它ip登录,您被踢下线了!”);

request.getRequestDispatcher(“/login.jsp”).forward(request, response);

}else{

chain.doFilter(request, response);//放行

}

}

Session过期过滤器 核心代码

@Override

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

System.out.println(“过滤请求…”);

HttpServletRequest request =  (HttpServletRequest)req;

HttpServletResponse response =  (HttpServletResponse)resp;

//session中获取用户名信息

String userId = (String)request.getSession().getAttribute(“userId”);

String admin = (String)request.getSession().getAttribute(“admin”);

//普通用户登录过滤,如果用户名为空说明带有登录标记的Session过期了

if (userId==null||“”.equals(userId.toString()) ) {

//超级管理员过滤

if(admin==null||“”.equals(admin.toString())){

response.sendRedirect(request.getContextPath()+ADMIN_URL);

return ;

}

//如果普通用户和超级管理员都没有登陆内容,说明登录过期

System.out.println(“登录过期,请重新登录!”);

response.sendRedirect(request.getContextPath()+LOGIN_URL);

PrintWriter printWriter = response.getWriter();

String relogin = “登录过期,请重新登录!”;

printWriter.write(relogin);

printWriter.flush();

printWriter.close();

return ;

}

//过滤通过,放行

chain.doFilter(request, response);

System.out.println(“过滤响应!”);

}

web.xml配置

TickFronterFilter

com.fengyun.web.filter.TickFronterFilter

TickFronterFilter

/login.html

   

com.fengyun.web.filter.SessionCounter

Loginfilter

com.fengyun.web.filter.LoginOverdueFilter

Loginfilter

/material/*

…等等需要过滤的url地址…当然可以使用通配方式写(此处不详述)

/operate_editeCompact.html

REQUEST

FORWARD

最后

无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。

针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。

最后

无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。

针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。

[外链图片转存中…(img-oMwUuuwi-1714327425027)]

[外链图片转存中…(img-i38CXmep-1714327425028)]

[外链图片转存中…(img-lTIDhAtI-1714327425028)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值