Example usage for javax.servlet.http Cookie getValue

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

Introduction

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

Prototype

public String getValue() 

Source Link

Document

Gets the current value of this Cookie.

Usage

From source file:com.zk.common.waf.request.WafRequestWrapper.java

/**
 * @Description Cookie//w  w w.  j  ava 2 s .co  m
 * @return
 */
@Override
public Cookie[] getCookies() {
    Cookie[] existingCookies = super.getCookies();
    if (existingCookies != null) {
        for (int i = 0; i < existingCookies.length; ++i) {
            Cookie cookie = existingCookies[i];
            cookie.setValue(filterParamString(cookie.getValue()));
        }
    }
    return existingCookies;
}

From source file:com.music.web.util.AutoLoginInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

    // don't handle ajax or resource requests
    String requestedWith = request.getHeader("X-Requested-With");
    if (handler instanceof ResourceHttpRequestHandler
            || (requestedWith != null && requestedWith.equals("XMLHttpRequest"))) {
        return true;
    }/*from  w  w  w .  j  a  v  a 2s  .c  om*/

    if (userContext.getUser() == null && request.getCookies() != null) {
        Cookie[] cookies = request.getCookies();

        String authToken = null;
        String series = null;

        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(SocialSignInAdapter.AUTH_TOKEN_COOKIE_NAME)) {
                authToken = cookie.getValue();
            } else if (cookie.getName().equals(SocialSignInAdapter.AUTH_TOKEN_SERIES_COOKIE_NAME)) {
                series = cookie.getValue();
            }
        }

        if (authToken != null && series != null) {
            User user = userService.rememberMeLogin(authToken, series);
            if (user != null) {
                adapter.signIn(user, response, false);
            }
        }
    }
    return true;
}

From source file:org.perconsys.controllers.BlogManagementController.java

@RequestMapping(value = { "/create" }, method = RequestMethod.GET)
public ModelAndView createForm() {
    User user = ((User) getFromSession("user"));
    if (user == null) {
        // trying login by cookie
        Cookie authCookie = cookies.get("authKey");
        if (authCookie != null) {
            String authKey = authCookie.getValue();
            user = userDao.getByKey(authKey);
        }//from  ww  w .jav a2s  .  c  o  m
        // if no user - redirect to login page
        if (user == null) {
            return new ModelAndView("auth/login_form", "user", new User());
        }
    }

    Blog blog = new Blog("new blog", "");
    blog.setUser(user);
    blog = blogDao.create(blog);
    return new ModelAndView("blog/edit_form", "blog", blog);
}

From source file:docs.security.RememberMeSecurityConfigurationTests.java

@Test
public void authenticateWhenSpringSessionRememberMeEnabledThenCookieMaxAgeAndSessionExpirationSet()
        throws Exception {
    // @formatter:off
    MvcResult result = this.mockMvc.perform(formLogin()).andReturn();
    // @formatter:on

    Cookie cookie = result.getResponse().getCookie("SESSION");
    assertThat(cookie.getMaxAge()).isEqualTo(Integer.MAX_VALUE);
    T session = this.sessions.getSession(cookie.getValue());
    assertThat(session.getMaxInactiveIntervalInSeconds()).isEqualTo((int) TimeUnit.DAYS.toSeconds(30));

}

From source file:com.pokerweb.Area.CheckBetArea.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from   w  w w .  j  a v a  2 s.c  om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String Token = "";
    for (Cookie object : request.getCookies())
        if (object.getName().equals("JSESSIONID"))
            Token = object.getValue();
    if (Token.length() <= 0)
        return;
    JSONObject js = new JSONObject();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    long UserId = DBManager.GetInstance().GetCurrentUserId(auth.getName());
    for (Map.Entry<Long, StatisticBet> object : TableStatus.GetInstance().RequestStatisticBet.entrySet())
        if (object.getValue().ToketUserRequest.equals(Token) && object.getKey() == UserId)
            if (TableStatus.GetInstance().StatisticBetCurrentUser.containsKey(object.getValue().IdBet)) {
                try {
                    Game GMData = new Game();
                    String data = data = GMData.GetDateFromBet(object.getValue().IdBet);
                    TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet).put("date",
                            data);
                    TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet).put("round",
                            GMData.GetRoundFromBet(object.getValue().IdBet));
                    js.put("StatisticCurrentUser",
                            TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet));
                    TableStatus.GetInstance().StatisticBetCurrentUser.remove(object.getValue().IdBet);
                    TableStatus.GetInstance().RequestStatisticBet.remove(
                            TableStatus.GetInstance().StatisticBetCurrentUser.get(object.getValue().IdBet));
                } catch (JSONException ex) {
                    Logger.getLogger(CheckBetArea.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    response.setContentType("application/json; charset=utf-8");
    response.setHeader("Cache-Control", "no-cache");
    response.getWriter().write(js.toString());
}

From source file:org.tonguetied.web.CookieUtilsTest.java

/**
 * Test method for {@link org.tonguetied.web.CookieUtils#createCookie(HttpServletRequest, String, String)}.
 *//*w ww  .jav  a 2  s . c  o m*/
@Test
public final void testCreateCookie() {
    Cookie cookie = CookieUtils.createCookie(request, "name", "value");
    assertEquals("name", cookie.getName());
    assertEquals("value", cookie.getValue());
    assertEquals("/test", cookie.getPath());
    assertEquals(-1, cookie.getMaxAge());
}

From source file:org.perconsys.controllers.BlogManagementController.java

@RequestMapping(value = { "edit/{id}" }, method = RequestMethod.GET)
public ModelAndView editForm(@PathVariable("id") long id, HttpServletRequest request) {

    User user = ((User) request.getSession(true).getAttribute("user"));
    if (user == null) {
        // trying login by cookie
        Cookie authCookie = cookies.get("authKey");
        if (authCookie != null) {
            String authKey = authCookie.getValue();
            user = userDao.getByKey(authKey);
        }//w w  w  .  java  2  s  . c om
        // if no user - redirect to login page
        if (user == null) {
            return new ModelAndView("auth/login_form", "user", new User());
        }
    }

    Blog blog = blogDao.getById(id);
    if (blog == null) {
        // if no blog bi this id
        return new ModelAndView("redirect:/editblog/create");
    } else {
        if (user.getId() != blog.getUser().getId()) {
            // if user is not an owner
            return new ModelAndView("redirect:/blog/" + id);
        }
    }
    blog.setUser(user);
    return new ModelAndView("blog/edit_form", "blog", blog);
}

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 . j a va2 s. com
 */
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:com.ms.commons.summer.security.web.DefaultSecurityFormResolver.java

/**
 * @param request/*from   w  ww.  j a  va  2 s .co  m*/
 * @param response
 * @throws InvalidTokenException
 */
public void validCSRFToken(final HttpServletRequest request, final HttpServletResponse response)
        throws InvalidTokenException {
    Cookie[] cookies = request.getCookies();
    String ctoken = null;
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (SESSION_TOKEN.equals(cookie.getName())) {
                ctoken = cookie.getValue();
                break;
            }
        }
    }
    String rtoken = request.getParameter(SESSION_TOKEN);
    if (rtoken == null || rtoken.length() == 0) {
        throw new InvalidTokenException("can't find csrf token in request");
    }
    if (ctoken == null || ctoken.length() == 0) {
        throw new InvalidTokenException("can't find csrf token in cookie");
    }
    if (!ctoken.equals(rtoken)) {
        throw new InvalidTokenException("failed to check for csrf token in request");
    }
}

From source file:com.wikipy.security.AuthenticationFilter.java

public String getCookieTicket(HttpServletRequest httpReq) {

    Cookie[] cookies = httpReq.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(ARG_TICKET)) {
                return cookie.getValue();
            }/*from w  ww . ja  v a  2s .  c o  m*/
        }
    }
    return httpReq.getParameter(ARG_TICKET);
}