Example usage for javax.servlet.http HttpServletResponse addCookie

List of usage examples for javax.servlet.http HttpServletResponse addCookie

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse addCookie.

Prototype

public void addCookie(Cookie cookie);

Source Link

Document

Adds the specified cookie to the response.

Usage

From source file:io.interface21.CsrfHeaderFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
    if (csrf != null) {
        Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
        String token = csrf.getToken();
        if (cookie == null || token != null && !token.equals(cookie.getValue())) {
            // Angular is capable to handle CSRF protection and expected a token with name XSRF-TOKEN
            cookie = new Cookie("XSRF-TOKEN", token);
            cookie.setPath("/");
            response.addCookie(cookie);
        }/*from   w w w.ja  v  a2  s . c om*/
    }
    filterChain.doFilter(request, response);
}

From source file:com.junly.service.helper.TicketHelper.java

/** <p class="detail">
* cookie/* w ww.  jav  a2s.com*/
* </p>
* @author junly
* @date 2016422 
* @param response
* @param ticket    
*/
public void setCookie(HttpServletRequest request, HttpServletResponse response, String ticket) {
    // ?
    Cookie cookie = new Cookie(ViewContants.LOGIN_TICKET_KEY, ticket);

    // ? ???
    cookie.setDomain(request.getServerName());
    // path
    cookie.setPath("/");
    // ??
    cookie.setMaxAge(ViewContants.TRUST_COOKIE_TIME); // 
    response.addCookie(cookie);
}

From source file:com.basicservice.controller.AdminController.java

private void prepareContext(Map map, HttpServletResponse response) {
    Map constants = service.getConstants();
    String csrf_token = "<secret changing key>"; // use SecureRandom to generate a random token
    Locale locale = LocaleContextHolder.getLocale();
    map.put("locale", locale.getLanguage());
    map.put("constants", constants);
    if (response != null) {
        map.put("csrf_token", csrf_token);
        Cookie cookie = new Cookie(Constants.CSRF_COOKIE_NAME, csrf_token);
        cookie.setPath("/");
        cookie.setMaxAge(-1);//  ww  w  .ja  va2s  .  c  o m
        response.addCookie(cookie);
    }
}

From source file:com.yahoo.yos.YahooFilter.java

public void clearSession(HttpServletRequest req, HttpServletResponse res) {
    if (logger.isDebugEnabled()) {
        logger.debug("clear session requested");
    }//from   w w  w . j  ava2s .  c o m
    Cookie at = new Cookie("yosdk_at", "");
    at.setMaxAge(0);
    Cookie rt = new Cookie("yosdk_rt", "");
    rt.setMaxAge(0);
    res.addCookie(at);
    res.addCookie(rt);
    req.setAttribute("yahooSession", null);
    req.setAttribute("yahooRedirect", null);
}

From source file:com.persistent.cloudninja.controller.CloudNinjaAuthFilter.java

private void removeCookie(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        String authCookieName) {/*from   w  ww  .ja va  2 s  .c  o m*/

    //   Cookie cookieToBeRemoved = AuthFilterUtils.checkForPreExistentCookie(httpServletRequest, authCookieName);
    Cookie cookie = new Cookie(authCookieName, null);
    cookie.setMaxAge(0);
    cookie.setPath("/");
    httpServletResponse.addCookie(cookie);
}

From source file:org.ngrinder.user.controller.UserController.java

/**
 * Switch user identity.//from w w  w.j  a  va  2s. co  m
 *
 * @param model    model
 * @param to       the user to whom a user will switch
 * @param response response
 * @return redirect:/perftest/
 */
@RequestMapping("/switch")
public String switchUser(@RequestParam(required = false, defaultValue = "") String to,
        HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    Cookie cookie = new Cookie("switchUser", to);
    cookie.setPath("/");
    // Delete Cookie if empty switchUser
    if (StringUtils.isEmpty(to)) {
        cookie.setMaxAge(0);
    }

    response.addCookie(cookie);
    model.clear();
    final String referer = request.getHeader("referer");
    return "redirect:" + StringUtils.defaultIfBlank(referer, "/");
}

From source file:com.mmj.app.common.checkcode.CheckCodeManager.java

public byte[] create(CookieManager cookieManager, CookieNameEnum maimaijunCheckcode,
        HttpServletResponse response) {
    if (initException != null) {// ??
        setup();/*from  ww w  .  ja v  a 2  s  . c  o m*/
    }
    CheckCodeInfo createCheckCodeInfo = CheckCodeTools.createCheckCodeInfo();
    if (createCheckCodeInfo != null) {
        Cookie cookie = new Cookie("_cc_", EncryptBuilder.getInstance().encrypt(createCheckCodeInfo.getCode()));
        cookie.setMaxAge(CookieMaxAge.FOREVER);
        cookie.setDomain(CookieDomain.DOT_MAIMAIJUN_COM.getDomain());
        cookie.setPath("/");
        response.addCookie(cookie);
        return createCheckCodeInfo.getBytes();
    }
    return null;
}

From source file:com.silverpeas.authentication.AuthenticationServlet.java

/**
 * Write session cookie.//from www  . java  2 s  .  c o m
 *
 * @return
 */
private void writeSessionCookie(HttpServletResponse response, HttpSession session, boolean secured) {
    Cookie cookie = new Cookie("JSESSIONID", session.getId());
    cookie.setMaxAge(-1);
    cookie.setPath(session.getServletContext().getContextPath());
    cookie.setHttpOnly(true);
    if (secured) {
        cookie.setSecure(secured);
    }
    response.addCookie(cookie);
}

From source file:org.zaizi.sensefy.auth.LoginConfig.java

private Filter csrfHeaderFilter() {
    return new OncePerRequestFilter() {

        @Override/*from   w  w w. j  a  v  a2s  .  c om*/
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                FilterChain filterChain) throws ServletException, IOException {
            CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
            if (csrf != null) {

                Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                String token = csrf.getToken();
                if (cookie == null || token != null && !token.equals(cookie.getValue())) {
                    cookie = new Cookie("XSRF-TOKEN", token);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    // response.setHeader("Access-Control-Allow-Origin",
                    // "*");
                    // response.setHeader("Access-Control-Allow-Methods",
                    // "POST, GET, OPTIONS, DELETE");
                    // response.setHeader("Access-Control-Max-Age",
                    // "3600");
                    // response.setHeader("Access-Control-Allow-Headers",
                    // "x-requested-with");
                }

            }
            filterChain.doFilter(request, response);
        }
    };
}

From source file:org.uaa.security.core.LoginSuccessHandler.java

public void handle(UsernamePasswordToken token, HttpServletRequest request, HttpServletResponse response) {
    // set cookie
    Map<String, Object> session = new LinkedHashMap<String, Object>();
    session.put("uid", token.getUid());
    session.put("username", token.getUsername());
    session.put("password", token.getPassword());
    session.put("roles", token.getRoles());
    session.put("isAuthenticated", token.isAuthenticated());

    try {/*from ww w .j a va 2 s .  c o m*/
        String sessionId = Crypto.encrypt(JsonFactory.toJson(session));
        log.debug(sessionId);
        Cookie cookie = new Cookie(ConfigUtil.getValue("SESSION_ID"), sessionId);
        cookie.setMaxAge(7 * 24 * 3600);
        response.addCookie(cookie);
    } catch (IOException e) {
        e.printStackTrace();
    }
}