multiple markers at this line
package com.hzzykj.jcfz.web;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.session.HttpSessionEventPublisher;
/**
* 扩展的HttpSessionEventPublisher 支持在线人数统计
*
*/
public class EnhancedHttpSessionEventPublisher extends
HttpSessionEventPublisher implements ServletRequestListener {
private static int activeSessions = 0;
private HttpServletRequest request;
public static List<String> OnlineUserList = new ArrayList<String>();
@Override
public void sessionCreated(HttpSessionEvent event) {
// 将用户加入到在线用户列表中
// Object name = event.getSession().getAttributeNames();
// saveOrDeleteOnlineUser(event, Type.SAVE);
String sessionId = event.getSession().getId();
Timestamp createTime = new Timestamp(System.currentTimeMillis());
String loginIp = request.getRemoteAddr();
boolean rs = true;
if(OnlineUserList.size() > 0){
for(int i = 0;i < OnlineUserList.size(); i ++){
if(loginIp.equals(OnlineUserList.get(i))){
rs = false;
}
}
}
if(rs){ //如果队列中存在相同的IP 则SESSION不增加
OnlineUserList.add(loginIp);
System.out.println("ipList队列新增ip: "+loginIp);
activeSessions++;
System.out.println("新增SESSION,sessionId = " + sessionId +"; createTime = " + createTime
+ "; loginIp = " + loginIp +"; 当前总SESSION值为 "+activeSessions);
}
super.sessionCreated(event);
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
// 将用户从在线用户列表中移除
// saveOrDeleteOnlineUser(event, Type.DELETE);
String sessionId = event.getSession().getId();
Timestamp overTime = new Timestamp(System.currentTimeMillis());
String loginIp = request.getRemoteAddr();
if(activeSessions>0){
if(OnlineUserList.size() > 0){
for(int i = 0;i < OnlineUserList.size(); i ++){
if(loginIp.equals(OnlineUserList.get(i))){
OnlineUserList.remove(i);
System.out.println("ipList队列移除ip: "+loginIp);
}
}
}
activeSessions--; //在用户销毁的时候,从队列中踢出这个IP
System.out.println("销毁SESSION,sessionId = " + sessionId +"; overTime = " + overTime
+ "; loginIp = " + loginIp +"; 当前总SESSION值为 "+activeSessions);
}
super.sessionDestroyed(event);
}
public void saveOrDeleteOnlineUser(HttpSessionEvent event, Type type) {
Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
event.getSession();
if (auth != null) {
Object principal = auth.getPrincipal();
if (principal instanceof User) {
User user = (User) principal;
switch (type) {
case SAVE:
System.out.println("创建用户"+user.getUsername());
OnlineUserList.add(user.getUsername());
break;
case DELETE:
System.out.println("删除用户"+user.getUsername());
OnlineUserList.remove(user.getUsername());
break;
}
}
}
}
public void requestDestroyed(ServletRequestEvent rre) {
// TODO Auto-generated method stub
}
public void requestInitialized(ServletRequestEvent rre) {
// TODO Auto-generated method stub
request=(HttpServletRequest)rre.getServletRequest();
}
/**
* 定义一个简单的内部枚举
*/
private static enum Type {
SAVE, DELETE;
}
--------------------编程问答--------------------
如果你用的是Myeclipse,那么你就可以忽视它,这是系统bug,右键那个文件Myeclipse—>exclude from validation;然后右键Myeclipse—>run validation,
补充:Java , Eclipse