javax.servlet.http.HttpSession.invalidate()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(213)

本文整理了Java中javax.servlet.http.HttpSession.invalidate()方法的一些代码示例,展示了HttpSession.invalidate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpSession.invalidate()方法的具体详情如下:
包路径:javax.servlet.http.HttpSession
类名称:HttpSession
方法名:invalidate

HttpSession.invalidate介绍

[英]Invalidates this session then unbinds any objects bound to it.
[中]使该会话无效,然后解除绑定到该会话的所有对象。

代码示例

代码示例来源:origin: igniterealtime/Openfire

/**
 * Invalidates and recreates session (do this on login/logout).
 */
public HttpSession invalidateSession() {
  session.invalidate();
  session = request.getSession(true);
  return session;
}

代码示例来源:origin: gocd/gocd

public static void recreateSessionWithoutCopyingOverSessionState(HttpServletRequest request) {
  LOGGER.debug("Creating new session.");
  request.getSession().invalidate();
  request.getSession();
}

代码示例来源:origin: stackoverflow.com

@WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"})
public class LogoutServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 HttpSession session = request.getSession(false);
 // Destroys the session for this user.
 if (session != null)
   session.invalidate();
 // Redirects back to the initial page.
 response.sendRedirect(request.getContextPath());
}
}

代码示例来源:origin: apache/geode

@Override
 public void call(HttpServletRequest request, HttpServletResponse response)
   throws IOException {
  HttpSession session = request.getSession();
  session.invalidate();
  response.getWriter().write("done");
 }
};

代码示例来源:origin: apache/drill

@GET
 @Path("/logout")
 public Viewable logout(@Context HttpServletRequest req,
   @Context HttpServletResponse resp) throws Exception {
  final HttpSession session = req.getSession();
  if (session != null) {
   session.invalidate();
  }
  req.getRequestDispatcher("/login").forward(req, resp);
  return null;
 }
}

代码示例来源:origin: ctripcorp/apollo

@Override
 public void logout(HttpServletRequest request, HttpServletResponse response) {
  //将session销毁
  HttpSession session = request.getSession(false);
  if (session != null) {
   session.invalidate();
  }

  Cookie cookie = new Cookie("memCacheAssertionID", null);
  //将cookie的有效期设置为0,命令浏览器删除该cookie
  cookie.setMaxAge(0);
  cookie.setPath(request.getContextPath() + "/");
  response.addCookie(cookie);

  //重定向到SSO的logout地址
  String casServerUrl = portalConfig.casServerUrlPrefix();
  String serverName = portalConfig.portalServerName();

  try {
   response.sendRedirect(casServerUrl + "/logout?service=" + serverName);
  } catch (IOException e) {
   throw new RuntimeException(e);
  }
 }
}

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest,
      HttpServletResponse wrappedResponse) throws IOException {
    wrappedRequest.getSession().invalidate();
  }
});

代码示例来源:origin: oblac/jodd

HttpSession session = httpRequest.getSession(false);
  session.invalidate();		// clear session if session id in URL

代码示例来源:origin: cloudfoundry/uaa

protected void handleRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession(false);
    if (session!=null) {
      session.invalidate();
    }
    strategy.sendRedirect(request, response, getRedirectUrl());
  }
}

代码示例来源:origin: cloudfoundry/uaa

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  boolean reAuthenticationRequired = false;
  HashMap<String, String[]> requestParams = new HashMap<>(request.getParameterMap());
  if ("login".equals(request.getParameter("prompt"))) {
    reAuthenticationRequired = true;
    requestParams.remove("prompt");
  }
  if (request.getParameter("max_age") != null && SecurityContextHolder.getContext().getAuthentication() instanceof UaaAuthentication) {
    UaaAuthentication auth = (UaaAuthentication) SecurityContextHolder.getContext().getAuthentication();
    if ((System.currentTimeMillis() - auth.getAuthenticatedTime()) > (Long.valueOf(request.getParameter("max_age"))*1000)) {
      reAuthenticationRequired = true;
      requestParams.remove("max_age");
    }
  }
  if (reAuthenticationRequired) {
    request.getSession().invalidate();
    sendRedirect(request.getRequestURL().toString(), requestParams, request, response);
  } else {
    filterChain.doFilter(request, response);
  }
}

代码示例来源:origin: DeemOpen/zkui

@Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
      logger.debug("Logout Action!");
      Properties globalProps = (Properties) getServletContext().getAttribute("globalProps");
      String zkServer = globalProps.getProperty("zkServer");
      String[] zkServerLst = zkServer.split(",");
      ZooKeeper zk = ServletUtil.INSTANCE.getZookeeper(request, response, zkServerLst[0],globalProps);
      request.getSession().invalidate();
      zk.close();
      response.sendRedirect("/login");
    } catch (InterruptedException ex) {
      logger.error(Arrays.toString(ex.getStackTrace()));
      ServletUtil.INSTANCE.renderError(request, response, ex.getMessage());
    }

  }
}

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    HttpSession session = wrappedRequest.getSession();
    session.invalidate();
    // no exception
    session.getId();
  }
});

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest,
      HttpServletResponse wrappedResponse) {
    wrappedRequest.getSession().invalidate();
    wrappedRequest.getSession();
  }
});

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    HttpSession session = wrappedRequest.getSession();
    session.invalidate();
    // no exception
    session.getServletContext();
  }
});

代码示例来源:origin: javaee-samples/javaee7-samples

HttpSession session = request.getSession(false);
if (session != null) {
  session.invalidate();

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    HttpSession session = wrappedRequest.getSession();
    session.invalidate();
    try {
      session.invalidate();
      fail("Expected Exception");
    }
    catch (IllegalStateException ignored) {
    }
  }
});

代码示例来源:origin: spring-projects/spring-session

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    wrappedRequest.getSession().setAttribute(ATTR_NAME, ATTR_VALUE);
    wrappedRequest.getSession().invalidate();
    wrappedRequest.getSession().setAttribute(ATTR_NAME2, ATTR_VALUE2);
  }
});

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

protected void abortUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
  SecurityContextHolder.clearContext();
  cookieUtils.invalidateCookie(response, SessionFixationProtectionCookie.COOKIE_NAME);
  if (BLCRequestUtils.isOKtoUseSession(new ServletWebRequest(request))) {
    request.getSession().invalidate();
  }
  response.sendRedirect("/");
}

代码示例来源:origin: apache/geode

session = request.getSession();
 session.setAttribute(param, value);
 break;
case SET_MAX_INACTIVE:
 session = request.getSession();
 session.setMaxInactiveInterval(Integer.valueOf(value));
 break;
case GET:
 session = request.getSession();
 String val = (String) session.getAttribute(param);
 if (val != null) {
 session = request.getSession();
 session.invalidate();
 break;
case CALLBACK:

代码示例来源:origin: linkedin/cruise-control

/**
 * Close the session for the given request.
 * @param request the request to close its session.
 * @param hasError whether the session is closed due to an error or not.
 */
synchronized void closeSession(HttpServletRequest request, boolean hasError) {
 // Response associated with this request has already been flushed; hence, do not attempt to create a new session.
 HttpSession session = request.getSession(false);
 if (session == null) {
  return;
 }
 SessionInfo info = _inProgressSessions.remove(session);
 if (info != null && info.lastFuture().isDone()) {
  LOG.info("Closing session {}", session);
  session.invalidate();
  _sessionLifetimeTimer.update(System.nanoTime() - info.requestStartTimeNs(), TimeUnit.NANOSECONDS);
  if (!hasError && info.executionTime() > 0) {
   _successfulRequestExecutionTimer.get(info.endPoint()).update(info.executionTime(), TimeUnit.NANOSECONDS);
  }
 }
}

相关文章