Example usage for javax.servlet.http Cookie setPath

List of usage examples for javax.servlet.http Cookie setPath

Introduction

In this page you can find the example usage for javax.servlet.http Cookie setPath.

Prototype

public void setPath(String uri) 

Source Link

Document

Specifies a path for the cookie to which the client should return the cookie.

Usage

From source file:com.arya.latihan.config.SecurityConfiguration.java

/**
 * Method untuk menyimpan CSRF TOKEN di cookie browser.
 * Token disimpan dengan nama XSRF-TOKEN karena AngularJS mengenal CSRF sebagai XSRF
 * @return Filter//from   w w w  . jav a  2s  .  co  m
 */
private Filter csrfHeaderFilter() {
    return new OncePerRequestFilter() {

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                FilterChain filterChain) throws ServletException, IOException {
            CsrfToken csrfToken = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
            if (csrfToken != null) {
                String token = csrfToken.getToken();
                Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");// angular js menamai CSRF dengan XSRF
                if (cookie == null || token != null && !token.equals(cookie.getValue())) {
                    cookie = new Cookie("XSRF-TOKEN", token);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
            filterChain.doFilter(request, response);
        }
    };
}

From source file:org.josso.gateway.signon.RememberMeLoginAction.java

@Override
protected boolean onFatalError(Exception e, HttpServletRequest request, HttpServletResponse response) {

    logger.debug("Removing cookie with 'JOSSO_REMEMBERME_TOKEN' (fatal error)");

    // Clear the remember me cookie
    Cookie ssoCookie = new Cookie(
            Constants.JOSSO_REMEMBERME_TOKEN + "_" + SSOContext.getCurrent().getSecurityDomain().getName(),
            "-");
    ssoCookie.setMaxAge(0);/*  ww  w . jav a2  s .c  om*/
    ssoCookie.setSecure(true);
    ssoCookie.setPath("/");

    response.addCookie(ssoCookie);

    return super.onFatalError(e, request, response);
}

From source file:net.yacy.cora.protocol.ResponseHeader.java

/**
 * Sets Cookie on the client machine./*from  w  w  w.j  a  v a2 s.  c om*/
 *
 * @param name Cookie name
 * @param value Cookie value
 * @param maxage time to live in seconds, none negative number, according to https://tools.ietf.org/html/rfc2109, 0=discard in https://tools.ietf.org/html/rfc2965
 * @param path Path the cookie belongs to. Default - "/". Can be <b>null</b>.
 * @param domain Domain this cookie belongs to. Default - domain name. Can be <b>null</b>.
 * @param secure If true cookie will be send only over safe connection such as https
 * @see further documentation: <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
 */
public void setCookie(final String name, final String value, final Integer maxage, final String path,
        final String domain, final boolean secure) {
    /*
    * TODO:Here every value can be validated for correctness if needed
    * For example semicolon should be not in any of the values
    * However an exception in this case would be an overhead IMHO.
    */
    if (!name.isEmpty()) {
        if (this.cookieStore == null)
            this.cookieStore = new ArrayList<Cookie>();
        Cookie c = new Cookie(name, value);
        if (maxage != null && maxage >= 0)
            c.setMaxAge(maxage);
        if (path != null)
            c.setPath(path);
        if (domain != null)
            c.setDomain(domain);
        if (secure)
            c.setSecure(secure);
        this.cookieStore.add(c);
    }
}

From source file:com.tenduke.example.scribeoauth.SessionManager.java

/**
 * Creates an authenticated session./* ww w  .  ja v a 2  s  . c o m*/
 * @param request Client HTTP request.
 * @param response HTTP response.
 * @param user User information as a JSON object.
 */
public void createSession(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    //
    final String sessionId = UUID.randomUUID().toString();
    final SessionInformation sessionInfo = new SessionInformation(sessionId, user);
    //
    final String cookieValue = MessageFormat.format(SESSION_COOKIE_PATTERN, sessionId,
            resolveUserProfileId(user));
    //
    setSessionInformation(sessionInfo);
    //
    final Cookie cookie = new Cookie(SIGNED_SESSION_COOKIE_NAME, cookieValue);
    cookie.setMaxAge(COOKIE_MAX_AGE);
    cookie.setPath("/");
    response.addCookie(cookie);
}

From source file:org.josso.gateway.signon.RememberMeLoginAction.java

@Override
protected boolean onLoginAuthenticationException(AuthenticationFailureException e, HttpServletRequest request,
        HttpServletResponse response, Credential[] credentials) throws IOException {

    logger.debug("Removing cookie with 'JOSSO_REMEMBERME_TOKEN' (login auth exception)");

    // Clear the remember me cookie
    Cookie ssoCookie = new Cookie(org.josso.gateway.Constants.JOSSO_REMEMBERME_TOKEN + "_"
            + SSOContext.getCurrent().getSecurityDomain().getName(), "-");
    ssoCookie.setMaxAge(0);/*from  w ww .ja  v  a  2 s. c o  m*/
    ssoCookie.setSecure(true);
    ssoCookie.setPath("/");

    response.addCookie(ssoCookie);

    return super.onLoginAuthenticationException(e, request, response, credentials);

}

From source file:com.tenduke.example.webadmin.SessionManager.java

/**
 * Creates an authenticated session.//  www . j  av a 2  s .  c o  m
 * @param request Client HTTP request.
 * @param response HTTP response.
 * @param user User information as a JSON object.
 * @return Session information object as result.
 */
public SessionInformation createSession(final HttpServletRequest request, final HttpServletResponse response,
        final JSONObject user) {
    //
    final String sessionId = UUID.randomUUID().toString();
    final SessionInformation sessionInfo = new SessionInformation(sessionId, user);
    //
    final String cookieValue = MessageFormat.format(SESSION_COOKIE_PATTERN, sessionId,
            resolveUserProfileId(user));
    //
    setSessionInformation(sessionInfo);
    //
    final Cookie cookie = new Cookie(SIGNED_SESSION_COOKIE_NAME, cookieValue);
    cookie.setMaxAge(COOKIE_MAX_AGE);
    cookie.setPath("/");
    response.addCookie(cookie);
    //
    return sessionInfo;
}

From source file:org.craftercms.commons.http.CookieManager.java

/**
 * Add a "delete" cookie to the response to indicate the that the stored cookie should be deleted.
 *
 * @param name the name of the cookie/*from  ww  w  .  j  av  a2s.c  om*/
 */
public void deleteCookie(String name, HttpServletResponse response) {
    Cookie cookie = new Cookie(name, null);
    cookie.setHttpOnly(httpOnly);
    cookie.setSecure(secure);
    if (StringUtils.isNotEmpty(domain)) {
        cookie.setDomain(domain);
    }
    if (StringUtils.isNotEmpty(path)) {
        cookie.setPath(path);
    }

    cookie.setMaxAge(0);

    response.addCookie(cookie);

    logger.debug(LOG_KEY_DELETED_COOKIE, name);
}

From source file:org.craftercms.commons.http.CookieManager.java

/**
 * Add a new cookie, using the configured domain, path and max age, to the response.
 *
 * @param name  the name of the cookie/*from  w w w . ja  v  a 2  s.c o  m*/
 * @param value the value of the cookie
 */
public void addCookie(String name, String value, HttpServletResponse response) {
    Cookie cookie = new Cookie(name, value);
    cookie.setHttpOnly(httpOnly);
    cookie.setSecure(secure);
    if (StringUtils.isNotEmpty(domain)) {
        cookie.setDomain(domain);
    }
    if (StringUtils.isNotEmpty(path)) {
        cookie.setPath(path);
    }
    if (maxAge != null) {
        cookie.setMaxAge(maxAge);
    }

    response.addCookie(cookie);

    logger.debug(LOG_KEY_ADDED_COOKIE, name);
}

From source file:org.craftercms.cstudio.share.servlet.CookieManagerImpl.java

public void putCookieValue(HttpServletRequest request, HttpServletResponse response, String path, String key,
        int age, Serializable value) throws CStudioException {

    String cookieValue = null;/*from w ww  . j  ava  2 s .  c  o  m*/

    if (_encryptCookies) {
        cookieValue = getEncryptedObjectAsString(value);
    } else {
        cookieValue = value.toString();
    }

    Cookie cookie = new Cookie(key, cookieValue);
    if (!StringUtils.isEmpty(path)) {
        cookie.setPath(path);
    } else {
        cookie.setPath("/");
    }
    cookie.setMaxAge(age);

    //if(request.getServerName().indexOf(".") != -1) {
    //   String validForDomain = request.getServerName().substring(request.getServerName().indexOf("."));
    //   cookie.setDomain(validForDomain);
    //}

    if (_cookieDomain != null) {
        cookie.setDomain(_cookieDomain);
    }

    response.addCookie(cookie);
}

From source file:org.jsecurity.web.attr.CookieAttribute.java

public void onStoreValue(T value, ServletRequest servletRequest, ServletResponse servletResponse) {

    HttpServletRequest request = toHttp(servletRequest);
    HttpServletResponse response = toHttp(servletResponse);

    String name = getName();/*from   w w w .  jav a2  s  .co m*/
    int maxAge = getMaxAge();
    String path = getPath() != null ? getPath() : request.getContextPath();

    String stringValue = toStringValue(value);
    Cookie cookie = new Cookie(name, stringValue);
    cookie.setMaxAge(maxAge);
    cookie.setPath(path);
    if (isSecure()) {
        cookie.setSecure(true);
    }

    response.addCookie(cookie);
    if (log.isTraceEnabled()) {
        log.trace("Added Cookie [" + name + "] to path [" + path + "] with value [" + stringValue
                + "] to the HttpServletResponse.");
    }
}