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

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

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

HttpSession.getAttribute介绍

[英]Returns the object bound with the specified name in this session, or null if no object is bound under the name.
[中]返回在此会话中使用指定名称绑定的对象,如果名称下没有对象绑定,则返回null

代码示例

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

/**
 * Retrieves saved FlashMap instances from the HTTP session, if any.
 */
@Override
@SuppressWarnings("unchecked")
@Nullable
protected List<FlashMap> retrieveFlashMaps(HttpServletRequest request) {
  HttpSession session = request.getSession(false);
  return (session != null ? (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE) : null);
}

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

@SuppressWarnings("unchecked")
Flash(HttpSession session) {
  this.session = session;
  this.value = (T) session.getAttribute(ATTRIBUTE);
  if (this.value != null) {
    session.removeAttribute(ATTRIBUTE);
  }
}

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

HttpSession session = request.getSession();
Integer n = (Integer) session.getAttribute("foo");
// not thread safe
// another thread might be have got stale value between get and set
session.setAttribute("foo", (n == null) ? 1 : n + 1);

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

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {
  HttpServletRequest request = (HttpServletRequest) req;
  HttpServletResponse response = (HttpServletResponse) res;
  if (!request.getRequestURI().contains("/login") && !request.getRequestURI().contains("/acd/appconfig")) {
    RequestDispatcher dispatcher;
    HttpSession session = request.getSession();
    if (session != null) {
      if (session.getAttribute("authName") == null || session.getAttribute("authRole") == null) {
        response.sendRedirect("/login");
        return;
      }
    } else {
      request.setAttribute("fail_msg", "Session timed out!");
      dispatcher = request.getRequestDispatcher("/Login");
      dispatcher.forward(request, response);
      return;
    }
  }
  fc.doFilter(req, res);
}

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

/**
 * Set the timestamp on the session to mark when the authentication happened,
 * useful for calculating authentication age. This gets stored in the sesion
 * and can get pulled out by other components.
 */
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
  Date authTimestamp = new Date();
  HttpSession session = request.getSession();
  session.setAttribute(AUTH_TIMESTAMP, authTimestamp);
  if (session.getAttribute(AuthorizationRequestFilter.PROMPT_REQUESTED) != null) {
    session.setAttribute(AuthorizationRequestFilter.PROMPTED, Boolean.TRUE);
    session.removeAttribute(AuthorizationRequestFilter.PROMPT_REQUESTED);
  }
  logger.info("Successful Authentication of " + authentication.getName() + " at " + authTimestamp.toString());
  super.onAuthenticationSuccess(request, response, authentication);
}

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

HttpServletRequest request = this.getThreadLocalRequest();

HttpSession session = request.getSession();

// in your authentication method
if(isCorrectPassword)
  session.setAttribute("authenticatedUserName", "name");

// later
 if (session.getAttribute("authenticatedUserName") != null)

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

public boolean verify(HttpServletRequest request) {
    String postedToken = request.getParameter(TOKEN);
    String expectedToken = (String) request.getSession().getAttribute(TOKEN);
    return !StringUtils.isBlank(postedToken) && !StringUtils.isBlank(expectedToken) && postedToken.equals(expectedToken);
  }
}

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

HttpSession session = request.getSession();
  chain.doFilter(req, res);
  return;
    session.setAttribute(LOGIN_HINT, loginHint);
  } else {
    session.removeAttribute(LOGIN_HINT);
        chain.doFilter(req, res);
      } else {
        logger.info("Client requested no prompt");
            response.sendRedirect(uriBuilder.toString());
            return;
      if (session.getAttribute(PROMPTED) == null) {
        session.setAttribute(PROMPT_REQUESTED, Boolean.TRUE);
          chain.doFilter(req, res);
        } else {
        session.removeAttribute(PROMPTED);
        chain.doFilter(req, res);
      Date authTime = (Date) session.getAttribute(AuthenticationTimeStamper.AUTH_TIMESTAMP);

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

String action = request.getParameter("action");
String currentPath = request.getParameter("currentPath");
String displayPath = request.getParameter("displayPath");
String authRole = (String) request.getSession().getAttribute("authRole");
      request.getSession().setAttribute("flashMsg", "Node created!");
      dao.insertHistory((String) request.getSession().getAttribute("authName"), request.getRemoteAddr(), "Creating node: " + currentPath + newNode);
    response.sendRedirect("/home?zkPath=" + displayPath);
    break;
  case "Save Property":
      request.getSession().setAttribute("flashMsg", "Property Saved!");
      if (ZooKeeperUtil.INSTANCE.checkIfPwdField(newProperty)) {
        newValue = ZooKeeperUtil.INSTANCE.SOPA_PIPA;
      dao.insertHistory((String) request.getSession().getAttribute("authName"), request.getRemoteAddr(), "Saving Property: " + currentPath + "," + newProperty + "=" + newValue);
    response.sendRedirect("/home?zkPath=" + displayPath);
    break;
  case "Update Property":
      dao.insertHistory((String) request.getSession().getAttribute("authName"), request.getRemoteAddr(), "Updating Property: " + currentPath + "," + newProperty + "=" + newValue);
          dao.insertHistory((String) request.getSession().getAttribute("authName"), request.getRemoteAddr(), "Deleting Property: " + delPropLst.toString());
          dao.insertHistory((String) request.getSession().getAttribute("authName"), request.getRemoteAddr(), "Deleting Nodes: " + delNodeLst.toString());

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

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    assertThat(wrappedRequest.getSession().getAttribute(ATTR))
        .isEqualTo(VALUE);
    wrappedRequest.getSession().removeAttribute(ATTR);
    assertThat(wrappedRequest.getSession().getAttribute(ATTR)).isNull();
  }
});

代码示例来源:origin: elasticjob/elastic-job-lite

@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
  HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
  HttpSession httpSession = httpRequest.getSession();
  if (null == httpSession.getAttribute(RegistryCenterRestfulApi.REG_CENTER_CONFIG_KEY)) {
    loadActivatedRegCenter(httpSession);
  }
  if (null == httpSession.getAttribute(EventTraceDataSourceRestfulApi.DATA_SOURCE_CONFIG_KEY)) {
    loadActivatedEventTraceDataSource(httpSession);
  }
  filterChain.doFilter(servletRequest, servletResponse);
}

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

public class Logout extends HttpServlet {
 public Logout() {
   super();
 }
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   HttpSession session = request.getSession();
   if(session.getAttribute("user") != null){
     session.removeAttribute("user");
     response.sendRedirect("login.jsp");
   }
 }

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

@Override
  public void doFilter(HttpServletRequest wrappedRequest) {
    wrappedRequest.getSession().setAttribute(ATTR, VALUE);
    assertThat(wrappedRequest.getSession().getAttribute(ATTR))
        .isEqualTo(VALUE);
    assertThat(
        Collections.list(wrappedRequest.getSession().getAttributeNames()))
            .containsOnly(ATTR);
  }
});

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

DiscoveryInformation discovered = (DiscoveryInformation) request.getSession()
    .getAttribute(DISCOVERY_INFO_KEY);
    .getSession().getAttribute(ATTRIBUTE_LIST_KEY);
request.getSession().removeAttribute(DISCOVERY_INFO_KEY);
request.getSession().removeAttribute(ATTRIBUTE_LIST_KEY);

代码示例来源:origin: nutzam/nutz

for (Enumeration<String> en = session.getAttributeNames(); en.hasMoreElements();) {
    String tem = en.nextElement();
    session_attr.put(tem, session.getAttribute(tem));
for (Object o : Lang.enum2collection(req.getParameterNames(), new ArrayList<String>())) {
  String key = (String) o;
  String value = req.getParameter(key);
  p.put(key, value);

代码示例来源:origin: jenkinsci/jenkins

@Override
  public boolean toggleCollapsed(String paneId) {
    final HttpSession session = Stapler.getCurrentRequest().getSession();
    final String property = format(attribute, paneId);
    final Object collapsed = session.getAttribute(property);
    if (collapsed == null) {
      session.setAttribute(property, true);
      return true;
    }
    session.removeAttribute(property);
    return false;
  }
}

代码示例来源:origin: nutzam/nutz

public void clear() {
  synchronized (session) {
    Enumeration<String> ems = session.getAttributeNames();
    List<String> keys = new ArrayList<String>();
    while (ems.hasMoreElements()) {
      String key = ems.nextElement();
      if (null == key)
        continue;
      Object value = session.getAttribute(key);
      if (value instanceof ObjectProxy) {
        keys.add(key);
        ((ObjectProxy) value).depose();
      }
    }
    for (String key : keys) {
      session.removeAttribute(key);
    }
  }
}

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

@Override
protected void doFilterInternal(HttpServletRequest request,
                HttpServletResponse response,
                FilterChain filterChain) throws ServletException, IOException {
  if (!SessionUtils.hasAuthenticationToken(request)) {
    LOGGER.debug("Authentication token is not created for the request.");
    filterChain.doFilter(request, response);
    return;
  }
  final AuthenticationToken<?> authenticationToken = SessionUtils.getAuthenticationToken(request);
  Assert.notNull(authenticationToken);
  synchronized (request.getSession(false).getId().intern()) {
    long localCopyOfLastChangedTime = lastChangedTime;//This is so that the volatile variable is accessed only once.
    Long previousLastChangedTime = (Long) request.getSession().getAttribute(SECURITY_CONFIG_LAST_CHANGE);
    if (previousLastChangedTime == null) {
      request.getSession().setAttribute(SECURITY_CONFIG_LAST_CHANGE, localCopyOfLastChangedTime);
    } else if (previousLastChangedTime < localCopyOfLastChangedTime) {
      request.getSession().setAttribute(SECURITY_CONFIG_LAST_CHANGE, localCopyOfLastChangedTime);
      LOGGER.debug("Invalidating existing token {}", authenticationToken);
      authenticationToken.invalidate();
    }
  }
  filterChain.doFilter(request, response);
}

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

private Long lookupSandboxId(HttpServletRequest request) {
  String sandboxIdStr = request.getParameter(SANDBOX_ID_VAR);
  Long sandboxId = null;
  if (sandboxIdStr != null) {
    try {
      sandboxId = Long.valueOf(sandboxIdStr);
      if (LOG.isTraceEnabled()) {
        LOG.trace("SandboxId found on request " + sandboxId);
      }
    } catch (NumberFormatException nfe) {
      LOG.warn("blcSandboxId parameter could not be converted into a Long", nfe);
    }
  }
  if (sandboxId == null) {
    // check the session
    HttpSession session = request.getSession(false);
    if (session != null) {
      sandboxId = (Long) session.getAttribute(SANDBOX_ID_VAR);
      if (LOG.isTraceEnabled()) {
        if (sandboxId != null) {
          LOG.trace("SandboxId found in session " + sandboxId);
        }
      }
    }
  } else {
    HttpSession session = request.getSession();
    session.setAttribute(SANDBOX_ID_VAR, sandboxId);
  }
  return sandboxId;
}

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

/**
 * Adds a session value to the request
 */
protected void addSessionValue(final HttpServletRequest request, final String key,
  final Object value) {
 List l = (List) request.getSession(true).getAttribute(key);
 if (l == null) {
  l = new ArrayList();
 }
 l.add(value);
 request.getSession(true).setAttribute(key, l);
}

相关文章