web项目踢出用户登陆操作
程序员文章站
2022-07-03 16:58:36
...
@WebServlet("/TickOurSvl")
public class TickOurSvl extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TickOurSvl() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String uno=request.getParameter("uno");
String sessionid=LoginSvl.loginedUsers.get(uno);
if(sessionid!=null) {
LogTask.logInfo(uno+"没有对应的会话id,请登录");
}else {
HttpSession session= OnlineUserListener.users.get(sessionid);
if(session!=null) {
LogTask.logInfo(sessionid+"没有对应的会话");
}else {
session.invalidate();
LogTask.logInfo(uno + "已被踢下线...");
}
}
}
}
@WebServlet("/LoginSvl")
public class LoginSvl extends HttpServlet {
private static final long serialVersionUID = 1L;
public static Map<String,String> loginedUsers = new MyHashMap<String, String>();
/**
* @see HttpServlet#HttpServlet()
*/
public LoginSvl() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String uno=request.getParameter("uno");
String password=request.getParameter("password");
UserBiz biz=new UserBiz();
try {
User user=biz.isExist(uno, password);
if(user==null) {
request.setAttribute("msg","用户或密码错误");
request.getRequestDispatcher("/main/login.jsp").forward(request, response);
}else {
//用户登录成功,写入cookie
String ckValue=user.getUno()+":"+user.getPassword();
Cookie ck=new Cookie("user", ckValue);
ck.setMaxAge(7*24*3600);
response.addCookie(ck);
loginedUsers.put(user.getUno(), request.getSession().getId());
request.getSession().setAttribute("user", user);
request.getRequestDispatcher("/MainSvl").forward(request, response);
}
}catch (UniqueNotException e) {
// TODO: handle exception
request.setAttribute("msg", "该用户名已在其他地方登录");
request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.logger.error(e.getMessage(),e);
throw new ServletException(e.getMessage());
}
}
}
public class MyHashMap<K,V> extends HashMap<K,V> {
@Override
public V put(K key, V value) {
// TODO Auto-generated method stub
V v=this.get(key);
if(v!=null) {
throw new UniqueNotException("用户名已经存在");
}
return super.put(key, value);
}
}
@WebListener
public class OnlineUserListener implements HttpSessionListener {
public static Map<String, HttpSession> users=new ConcurrentHashMap<>(); //所有在线用户
/**
* Default constructor.
*/
public OnlineUserListener() {
LogTask.logInfo("OnlineUserListener构造");
}
@Override
public void sessionCreated(HttpSessionEvent se) {
String sessionid=se.getSession().getId();
users.put(sessionid, se.getSession());
se.getSession().setMaxInactiveInterval(15*60);
LogTask.logInfo(sessionid+"上线");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
User user=(User) se.getSession().getAttribute("user");
LoginSvl.loginedUsers.remove(user.getUno());
LogTask.logInfo(user.getUno() + "被移除....");
String sessionid=se.getSession().getId();
users.remove(sessionid);
LogTask.logInfo(sessionid+"下线....");
}
}
实现踢人操作
/利用map唯一性的特性,进行重写封装。
由于已经做过自动登陆操作,所以需要在踢出前清楚掉用户的session。
利用用户名得出sessionid,再通过sessionid得出session进行删除。
public static Map<String,String> loginedUsers = new MyHashMap<String, String>();
loginedUsers.put(user.getUno(), request.getSession().getId());
public static Map<String, HttpSession> users=new ConcurrentHashMap<>(); 所有在线用户
users.put(sessionid, se.getSession());
以上两段代码只是项目中关于存储用户名 sessionid 和session的有关代码
。
至于使用 ConcurrentHashMap<>();的原因是由于他是线程安全,而且采用了分段锁,即安全又快捷的原因
上一篇: typescript——泛型
推荐阅读
-
新手小白Linux(Centos6.5)部署java web项目(mongodb4.0.2安装及相关操作)
-
Linux中基本的模式切换与用户登陆操作讲解
-
在Tomcat中部署Web项目的操作方法(必看篇)
-
通过登陆IP记录Linux所有用户登录所操作日志的方法
-
shiro同一用户后登陆踢出前面的用户
-
mysql常用操作:登陆、添加新用户、分配权限及相关操作命令
-
YII2框架中自定义用户认证模型,完成登陆和注册操作示例
-
spring boot thymeleaf 图片上传web项目根目录操作步骤
-
Java web 项目中的注册功能代码(用户注册信息写入数据库)
-
web项目中使用JS验证用户实名信息(含免费版和收费版)