一、简介(使用的Java监听器)
1、使用HttpSessionAttributeListener , HttpSessionListener 来显示的在线统计和计数功能。使用的 springboot框架!
2、使用ServletContext容器,对在线统计、登录统计、登录信息进行存储。
3、确保了只有一个账号是登录状态,其他电脑登录,会将当前登出。
4、确保一台计算机只能同时登录一个账号。登录其他person,会将当前的person登出。
二、自定义的控制层(controller层)
1、person登录
@ApiOperation(value = "person登录!")
@PostMapping(value = "/loginPerson")
public Result<Person> loginPerson(String username , String password , HttpSession session){
Result<Person> result = personService.selectByUsername(username.trim());
Person person = result.getData();
if(person != null){
Person inP = new Person();
inP.setPersonId(person.getPersonId());
inP.setUsername(username.trim());
inP.setPassword(password.trim());
Person outP = EncryptUserUtil.encryPerson(inP);
if(!outP.getPassword().equals(person.getPassword())){
return new Result("-1","密码错误!");
}
}else {
return new Result("-1","该用户名不存在!");
}
ConcurrentHashMap<String , HttpSession> personL = (ConcurrentHashMap<String , HttpSession>)session.getServletContext().getAttribute("personL");
Person pe = (Person) session.getAttribute("person");
if(pe != null){
if(personL != null && personL.containsKey(pe.getUsername().trim())){
personL.remove(pe.getUsername().trim());
session.getServletContext().setAttribute("personL" , personL);
System.out.println(pe.getUsername() + ":out!");
}
session.removeAttribute("person");
}
if(personL != null && personL.containsKey(username.trim())){
personL.get(username.trim()).invalidate();
System.out.println(username.trim() + ":out!");
}
Integer count = (Integer)session.getServletContext().getAttribute("login");
if(count!= null && count>= 3){
return new Result<>("-5","登录人数已满,请耐心等候,或者刷新登录");
}
if(personL == null){
personL = new ConcurrentHashMap<String , HttpSession>();
}
session.setAttribute("person" , person);
return new Result<Person>(person);
}
2、person退出。
@ApiOperation(value = "用户退出登录。")
@GetMapping("/lostPerson")
public Result lostPerson(HttpSession session){
Person person = (Person)session.getAttribute("person");
if(person != null){
HashMap<String , HttpSession> personL = (HashMap<String , HttpSession>)session.getServletContext().getAttribute("personL");
if(personL != null && personL.containsKey(person.getUsername().trim())){
personL.remove(person.getUsername().trim());
session.getServletContext().setAttribute("personL" , personL);
}
session.removeAttribute("person");
}
return new Result();
}
三、自定义的监听器
package com.cc.listener;
import com.cc.model.Person;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletContext;
import javax.servlet.http.*;
import java.util.concurrent.ConcurrentHashMap;
@Data
@Slf4j
public class MyLoginListener implements HttpSessionAttributeListener , HttpSessionListener {
private static int activeSessions = 0;
private static int activeLoginSessions = 0;
private static ConcurrentHashMap<String , HttpSession> personL;
public void attributeAdded(HttpSessionBindingEvent se) {
String name = se.getName();
if("person".equalsIgnoreCase(name)){
HttpSession session = se.getSession();
Person person = (Person)session.getAttribute("person");
ServletContext app = session.getServletContext();
personL = (ConcurrentHashMap<String , HttpSession>)app.getAttribute("personL");
if(personL == null){
personL = new ConcurrentHashMap<String , HttpSession>();
}
personL.put(person.getUsername() , session);
activeLoginSessions++;
app.setAttribute("login" , activeLoginSessions);
app.setAttribute("personL" , personL);
System.out.println(person.getUsername() + ":login!");
}
}
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("------------测试销毁调用!-----------");
String name = se.getName();
if("person".equalsIgnoreCase(name)){
HttpSession session = se.getSession();
ServletContext app = session.getServletContext();
if(activeLoginSessions > 0){
activeLoginSessions--;
}
app.setAttribute("login" , activeLoginSessions);
System.out.println("----outPerson----");
}
}
public void attributeReplaced(HttpSessionBindingEvent se) {
}
public void sessionCreated(HttpSessionEvent se){
activeSessions++;
ServletContext servletContext = se.getSession().getServletContext();
servletContext.setAttribute("online" , activeSessions);
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > 0){
activeSessions--;
}
ServletContext servletContext = se.getSession().getServletContext();
servletContext.setAttribute("online" , activeSessions);
HttpSession session = se.getSession();
Person person = (Person)session.getAttribute("person");
if(person != null ){
personL = (ConcurrentHashMap<String , HttpSession>)servletContext.getAttribute("personL");
if(personL != null){
personL.remove(person.getUsername());
}else {
personL = new ConcurrentHashMap<String , HttpSession>();
}
servletContext.setAttribute("personL" , personL);
}
System.out.println("--------destroy session--------:" + person);
}
}
四、监听器注入web容器中
@Configuration
@Data
public class MyLoginLisConfig {
final MyLoginListener myLoginListener;
public MyLoginLisConfig (){
this.myLoginListener = new MyLoginListener();
}
@Bean
public ServletListenerRegistrationBean<MyLoginListener> listenerRegistrationBean() {
ServletListenerRegistrationBean<MyLoginListener> registrationBean = new ServletListenerRegistrationBean<>();
registrationBean.setListener(myLoginListener);
return registrationBean;
}
}