在RESTEasy实现中获取所有活动的HttpSession

时间:2016-11-24 16:46:41

标签: java rest jax-rs

我需要获取所有活动Session的列表,以便我可以管理它们。基本上我需要管理应用程序中所有登录的用户。 使用HttpServletRequest req我能够获得当前会话 需要获得所有会议

这样的事情:

     public EmployeeTO getEmployeeById(int id, HttpServletRequest req) {
        EmployeeTO employeeTO = null;
        try{

            HttpSession session = req.getSession();
            HttpSessionContext httpSessionContext = session.getSessionContext();


        }catch(Exception e){
            e.printStackTrace();
        }
        return employeeTO;
    }

使用RESTFul实现与JASS进行登录

1 个答案:

答案 0 :(得分:1)

  

我有一个显示所有活动用户列表的屏幕。如果我检查一个   用户并单击关闭会话。我需要终止该用户会话。   要做到这一点,我需要在某处可以访问会话。

使用HttpServletRequest,您只能获得当前请求的(用户)session对象。但是,如果您想跟踪所有session个对象,可以通过实施HttpSessionListener来实现,如下所示:

  public class MyProjectSessionListenerAndInvalidator 
                                 implements HttpSessionListener {

     private static Map<String,Session> sessions = new HashMap<>();

      @Override
      public void sessionCreated(HttpSessionEvent event) {
          //add your code here, 
          //this will be invoked whenever there is a new session object created
           //Get the newly created session
           Session session = event.getSession();
           //get userId or unique field from session
           sessions.put(userId, session);
      }

      @Override
      public void sessionDestroyed(HttpSessionEvent event) {
         //add your code here
         //this will be invoked whenever there is a new session object removed

          //Get the removed session
          Session session = event.getSession();
          //get userId or unique field from session
          sessions.remove(userId);
      }

      public R getSessions() {
         //add code here
      }

      public void invalidateSession(String userId) {
         //add code here
      }
 }

N.B。:我建议您谨慎使用getSessions()invalidateSession()