目前大部分系统都会显示当前系统的在线用户信息,本人做了一个拦截器实现
1、定义拦截器 拦截 session失效
2、用户登录把用户信息保存在内存(map《sessionID,user》)
1、定于拦截器类
/**
* 监听在线用户上线下线 add by lizw
*/
public class OnlineListener implements ServletContextListener,HttpSessionListener {
private static ApplicationContext ctx = null;
public OnlineListener() {
}
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
//
ClientManager.getInstance().removeClinet(httpSessionEvent.getSession().getId());
}
/**
* 服务器初始化
*/
public void contextInitialized(ServletContextEvent evt) {
ctx = WebApplicationContextUtils.getWebApplicationContext(evt.getServletContext());
}
public static ApplicationContext getCtx() {
return ctx;
}
public void contextDestroyed(ServletContextEvent paramServletContextEvent) {
}
}
2、web.xml添加拦截
<!-- 在线用户监听器 add by lizw -->
<listener>
<listener-class>org.jeecgframework.web.system.listener.OnlineListener</listener-class>
</listener>
3、登录的时候保存
Client client = new Client();
client.setIp(IpUtil.getIpAddr(req));
client.setLogindatetime(new Date());
client.setUser(user);
ClientManager.getInstance().addClinet(session.getId(), client);
/**
* 对在线用户的管理
* @author lizw
* @date 2013-9-28
* @version 1.0
*/
public class ClientManager {
private static ClientManager instance = new ClientManager();
private ClientManager(){
}
public static ClientManager getInstance(){
return instance;
}
private Map<String,Client> map = new HashMap<String, Client>();
/**
*
* @param sessionId
* @param client
*/
public void addClinet(String sessionId,Client client){
map.put(sessionId, client);
}
/**
* sessionId
*/
public void removeClinet(String sessionId){
map.remove(sessionId);
}
/**
*
* @param sessionId
* @return
*/
public Client getClient(String sessionId){
return map.get(sessionId);
}
/**
*
* @return
*/
public Client getClient(){
return map.get(ContextHolderUtils.getSession().getId());
}
/**
*
* @return
*/
public Collection<Client> getAllClient(){
return map.values();
}
}
在线用户对象
/**
* 在线用户对象
*
* @author lizw
* @date 2013-9-28
* @version 1.0
*/
public class Client implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private TSUser user;
private Map<String, TSFunction> functions;
/**
* 用户IP
*/
private java.lang.String ip;
/**
*登录时间
*/
private java.util.Date logindatetime;
public TSUser getUser() {
return user;
}
public void setUser(TSUser user) {
this.user = user;
}
public Map<String, TSFunction> getFunctions() {
return functions;
}
public void setFunctions(Map<String, TSFunction> functions) {
this.functions = functions;
}
public java.lang.String getIp() {
return ip;
}
public void setIp(java.lang.String ip) {
this.ip = ip;
}
public java.util.Date getLogindatetime() {
return logindatetime;
}
public void setLogindatetime(java.util.Date logindatetime) {
this.logindatetime = logindatetime;
}
}