Example usage for javax.servlet.http HttpServletRequest getCookies

List of usage examples for javax.servlet.http HttpServletRequest getCookies

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getCookies.

Prototype

public Cookie[] getCookies();

Source Link

Document

Returns an array containing all of the Cookie objects the client sent with this request.

Usage

From source file:io.gravitee.management.security.config.basic.filter.AuthenticationSuccessFilter.java

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    final HttpServletRequest req = (HttpServletRequest) servletRequest;

    final Optional<Cookie> optionalStringToken;

    if (req.getCookies() == null) {
        optionalStringToken = Optional.empty();
    } else {/*from   w w  w  .  j  av  a2  s .  c  om*/
        optionalStringToken = Arrays.stream(req.getCookies())
                .filter(cookie -> HttpHeaders.AUTHORIZATION.equals(cookie.getName())).findAny();
    }

    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null && !optionalStringToken.isPresent()) {
        // JWT signer
        final Map<String, Object> claims = new HashMap<>();
        claims.put(JWTClaims.ISSUER, jwtIssuer);

        final UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        claims.put(JWTClaims.PERMISSIONS, userDetails.getAuthorities());
        claims.put(JWTClaims.SUBJECT, userDetails.getUsername());
        claims.put(JWTClaims.EMAIL, userDetails.getEmail());
        claims.put(JWTClaims.FIRSTNAME, userDetails.getFirstname());
        claims.put(JWTClaims.LASTNAME, userDetails.getLastname());

        final JWTSigner.Options options = new JWTSigner.Options();
        options.setExpirySeconds(jwtExpireAfter);
        options.setIssuedAt(true);
        options.setJwtId(true);

        final Cookie bearerCookie = jwtCookieGenerator
                .generate("Bearer " + new JWTSigner(jwtSecret).sign(claims, options));
        ((HttpServletResponse) servletResponse).addCookie(bearerCookie);
    }
    filterChain.doFilter(servletRequest, servletResponse);
}

From source file:com.baron.bm.controller.MemberController.java

@RequestMapping("/modify")
public String modifyidentity(String password, HttpServletRequest request) {
    String pass = null;// ww w  . j av  a  2 s.  c o m

    for (Cookie cookie : request.getCookies()) {
        if (cookie.getName().equals("bm_id")) {
            pass = joinService.identify(cookie.getValue());
        }
    }

    return (pass.equals(password)) ? "modifyidentity" : "identifyfail";
}

From source file:hudson.plugins.timestamper.TimestampFormatter.java

/**
 * Create a new {@link TimestampFormatter}.
 * /*from  w w w .j  a va 2 s .c o  m*/
 * @param systemTimeFormat
 *          the system clock time format
 * @param elapsedTimeFormat
 *          the elapsed time format
 * @param request
 *          the current HTTP request
 */
public TimestampFormatter(String systemTimeFormat, String elapsedTimeFormat, HttpServletRequest request) {
    String cookieValue = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("jenkins-timestamper".equals(cookie.getName())) {
                cookieValue = cookie.getValue();
                break;
            }
        }
    }

    if ("elapsed".equalsIgnoreCase(cookieValue)) {
        formatTimestamp = new ElapsedTimeFormatFunction(elapsedTimeFormat);
    } else if ("none".equalsIgnoreCase(cookieValue)) {
        formatTimestamp = new EmptyFormatFunction();
    } else {
        // "system", no cookie, or unrecognised cookie
        formatTimestamp = new SystemTimeFormatFunction(systemTimeFormat);
    }
}

From source file:gov.nih.nci.ncicb.cadsr.admintool.struts.action.BaseDispatchAction.java

protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response, String name) throws Exception {

    String user = null;//  ww  w  .  j  av  a 2s.co  m
    Cookie[] cookieArray = request.getCookies();
    if (cookieArray != null) {
        for (int i = 0; i < cookieArray.length; i++) {
            Cookie c = cookieArray[i];
            if (c.getName().equals("ADMIN_TOOL_USER")) {
                user = c.getValue();
                System.out.println("Reading username from cookie :" + user);
                System.out.println("Domain: " + c.getDomain());
                System.out.println("Path: " + c.getPath());
            }
        }
    }
    if (user == null) {
        return mapping.findForward("login");
    }
    return super.dispatchMethod(mapping, form, request, response, name);
}

From source file:com.ctc.storefront.security.evaluator.impl.RequireHardLoginEvaluator.java

protected boolean checkForGUIDCookie(final HttpServletRequest request, final HttpServletResponse response,
        final String guid) {
    if (guid != null && request.getCookies() != null) {
        final String guidCookieName = getCookieGenerator().getCookieName();
        if (guidCookieName != null) {
            return isGuidStoredinCookies(request, response, guid, guidCookieName);
        }//from   ww w  . j  a v a  2  s.c o m
    }

    return false;
}

From source file:eu.semlibproject.annotationserver.managers.CookiesManager.java

/**
 * Remove an annotation server cookie//from w  w w .  ja va 2s.  c o m
 * 
 * @param request
 * @param response
 * @param token 
 */
public void removeASCookie(HttpServletRequest request, HttpServletResponse response, String token) {

    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie cCookie = cookies[i];
            if (SemlibConstants.COOCKIE_NAME.equalsIgnoreCase(cCookie.getName())) {
                String value = cCookie.getValue();
                if (value.equals(token)) {
                    removeASCookie(response, cCookie);
                }
                break;
            }
        }
    }
}

From source file:controllers.Parent_Controller.java

public String[] checkSetCookie2(HttpServletRequest request) {
    Cookie[] cookies2;/*from   w  ww  .  j a  v  a2 s .c  o  m*/
    cookies2 = request.getCookies();
    String creds[] = new String[2];
    int countcookies = 0;
    try {
        for (Cookie cookie1 : cookies2) {
            switch (cookie1.getName()) {
            case "handle":
                System.out.println("Got Handle cookie");
                creds[0] = cookie1.getValue();
                countcookies++;
                break;
            case "uid":
                System.out.println("Got uid cookie");
                creds[1] = cookie1.getValue();
                countcookies++;
                break;
            }
        }
        if (countcookies == 0) {
            creds = null;
            return creds;
        } else {

            return creds;

        }
    } catch (Exception e) {
        System.out.println("Parent_Controller>checksetcookie2() error-->" + e);
        creds = null;
        return creds;
    }
}

From source file:com.sse.abtester.VariantSelectionFilter.java

/**
 * Gets the cookie value./*from  w  ww.  ja  v  a  2  s  .c  o m*/
 *
 * @param request the request
 * @return cookie value attached to VSKEY, or empty String.
 */
private String getCookieValue(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    String cookieValue = "";
    if (cookies == null)
        return cookieValue;

    for (Cookie c : cookies) {
        if (c.getName() == VSKEY) {
            cookieValue = c.getValue();
            break;
        }
    }
    return cookieValue;
}

From source file:com.googlesource.gerrit.plugins.github.oauth.GitHubLogin.java

private String getScopesKeyFromCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null) {
        return null;
    }/*from w  w w .ja va  2 s.co m*/

    for (Cookie cookie : cookies) {
        if (cookie.getName().equalsIgnoreCase("scope")) {
            return cookie.getValue();
        }
    }
    return null;
}

From source file:com.mobileman.projecth.web.util.PersistentCookieHelper.java

public User getUser(UserService userService, HttpServletRequest request, HttpServletResponse response) {
    Cookie[] cookies = request.getCookies();
    boolean removeCookie = false;
    if (cookies != null) {
        for (Cookie c : cookies) {
            if (COOKIE_NAME.equals(c.getName())) {
                removeCookie = true;/*from w w  w.  j a  v a  2 s.c  o  m*/
                String hash = c.getValue();

                //extract id, salt, hash
                if (StringUtils.isNotBlank(hash)) {
                    //if not found remove cookie!!!
                    String[] split = hash.split(SEPARATOR);
                    if (split.length == 3) {
                        try {
                            Long id = Long.parseLong(split[0]);
                            //find user
                            User user = userService.findById(id);
                            if (user != null) {
                                //check hash
                                String h2 = createHash(split[1], user);
                                if (hash.equals(h2)) {
                                    return user;
                                }
                            }
                        } catch (Exception ex) {
                        }
                    }
                }
            }
        }
    }
    if (removeCookie && response != null) {
        removeUser(request, response);
    }
    return null;
}