spring 利用security 登陆 session 重复创建的问题(高分求助) 我目前有一个spring mvc的项目,用security 登陆,在filter里获取session,取在线人数。我发现系统在运行过程中,有的时候只有我一个人登陆,会忽然创建好多session,然后在线人数就到了5k以上请问这个是什么原因造成的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要先判断session是否为空,如果为空,才需要设置! HttpSession session = event.getSession(); ServletContext context = session.getServletContext(); HashSet sessions = (HashSet) context.getAttribute("sessions"); if (sessions == null) { sessions = new HashSet(); sessions.add(session); context.setAttribute("sessions", sessions); } sessions.add(session); super.sessionCreated(event); super.sessionDestroyed(event); context.setAttribute("sessions", sessions); 这是filter的代码 有高手给解答一下吗?用的是 SessionCounter extends HttpSessionEventPublisher HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); String sID=(String)request.getSession().getAttribute("SESSIONID"); if(!session.getId().equals(sID)){ session.setAttribute("SESSIONID", session.getId()); ... } 可是这里面没有session。setattribute,都是context。setattribute,是spring管理的啊 创建session监听类:SessionListener.javapackage com.jdlx.common;import java.util.Hashtable;import java.util.Iterator;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class SessionListener implements HttpSessionListener{ //创建集合保存session对象 static Hashtable sessionList=new Hashtable(); //session创建触发的方法 public void sessionCreated(HttpSessionEvent event) { sessionList.put(event.getSession().getId(), event.getSession()); } //session过期或者销毁触发的方法 public void sessionDestroyed(HttpSessionEvent event) { sessionList.remove(event.getSession().getId()); } //返回全部session对象集合 static public Iterator getSet(){ return sessionList.values().iterator(); } //根据session对象的id返回session对象 static public HttpSession getSession(String sessionId){ return (HttpSession)sessionList.get(sessionId); }}action类中调用 int count=0; List userList=new ArrayList(); Iterator iterator = SessionListener.getSet(); while(iterator.hasNext()){ HttpSession session=(HttpSession)iterator.next(); User user=(User)session.getAttribute(MachineConstant.LOGIN_KEY); //得到单个session值 if(userList==null){ }else{ userList.add(user.getLoginname()); count++; } } setAttribute("userList",userList); setAttribute("count",count);在web.xml中配置监听 <listener> <listener-class>com.jdlx.common.SessionListener</listener-class> </listener> 谢谢楼上,我这个是用spring的security登录的,所以用的是security的一些方法。目前又出现了一个问题,当单点登录cas注销的时候,仍然要创建一个session,郁闷了。 最好还是用监听器吧,创建和销毁都可以知道,用fiter实在不是个好主意 扣费类 "脏处理" 最效率解决方案 请教自学框架的方法 求救~~~~~~~~~~~~~~~~怎样将EL表达式赋值给java变量? 本人对论坛的小建议 jsp问题 struts2中关于plugin-in的问题? 在jap中No collection found 一般是哪出现问题了啊 一个关于时间的程序......................... 读取文件的问题,高分相送 如何在服务器端解释servlet生成的xml hibernate关于类表继承的问题,有外键关联的子类配置文件怎么设置? 请解答我心中对Hibernate无尽纠结吧
HttpSession session = event.getSession();
ServletContext context = session.getServletContext();
HashSet sessions = (HashSet) context.getAttribute("sessions");
if (sessions == null) {
sessions = new HashSet();
sessions.add(session);
context.setAttribute("sessions", sessions);
}
sessions.add(session);
super.sessionCreated(event);
super.sessionDestroyed(event);
context.setAttribute("sessions", sessions); 这是filter的代码
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
String sID=(String)request.getSession().getAttribute("SESSIONID");
if(!session.getId().equals(sID)){
session.setAttribute("SESSIONID", session.getId());
...
}
import java.util.Iterator;import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionListener implements HttpSessionListener{
//创建集合保存session对象
static Hashtable sessionList=new Hashtable(); //session创建触发的方法
public void sessionCreated(HttpSessionEvent event) {
sessionList.put(event.getSession().getId(), event.getSession());
} //session过期或者销毁触发的方法
public void sessionDestroyed(HttpSessionEvent event) {
sessionList.remove(event.getSession().getId());
}
//返回全部session对象集合
static public Iterator getSet(){
return sessionList.values().iterator();
} //根据session对象的id返回session对象
static public HttpSession getSession(String sessionId){
return (HttpSession)sessionList.get(sessionId);
}
}
action类中调用 int count=0;
List userList=new ArrayList();
Iterator iterator = SessionListener.getSet();
while(iterator.hasNext()){
HttpSession session=(HttpSession)iterator.next();
User user=(User)session.getAttribute(MachineConstant.LOGIN_KEY); //得到单个session值
if(userList==null){
}else{
userList.add(user.getLoginname());
count++;
}
}
setAttribute("userList",userList);
setAttribute("count",count);在web.xml中配置监听 <listener>
<listener-class>com.jdlx.common.SessionListener</listener-class>
</listener>