Example usage for javax.servlet.http Cookie getMaxAge

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

Introduction

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

Prototype

public int getMaxAge() 

Source Link

Document

Gets the maximum age in seconds of this Cookie.

Usage

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginClearsCookieIfSignatureBlocksDoesNotMatchExpectedValue() throws Exception {
    udsWillReturnUser();//from  w w  w .j  a v a 2s  .  com
    Cookie cookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, generateCorrectCookieContentForToken(
            System.currentTimeMillis() + 1000000, "someone", "password", "WRONG_KEY"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);

    MockHttpServletResponse response = new MockHttpServletResponse();

    assertThat(services.autoLogin(request, response)).isNull();

    Cookie returnedCookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isZero();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginClearsCookieIfTokenDoesNotContainANumberInCookieValue() throws Exception {
    Cookie cookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY,
            new String(Base64.encodeBase64("username:NOT_A_NUMBER:signature".getBytes())));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);//from  w w  w.j a va 2  s .com

    MockHttpServletResponse response = new MockHttpServletResponse();
    assertThat(services.autoLogin(request, response)).isNull();

    Cookie returnedCookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isZero();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void autoLoginClearsCookieIfUserNotFound() throws Exception {
    udsWillThrowNotFound();// w w  w .j  a  v  a2s  . co  m
    Cookie cookie = new Cookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, generateCorrectCookieContentForToken(
            System.currentTimeMillis() + 1000000, "someone", "password", "key"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(cookie);

    MockHttpServletResponse response = new MockHttpServletResponse();

    assertThat(services.autoLogin(request, response)).isNull();

    Cookie returnedCookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(returnedCookie).isNotNull();
    assertThat(returnedCookie.getMaxAge()).isZero();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void loginFailClearsCookie() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    services.loginFail(request, response);

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(cookie).isNotNull();/*from ww  w  . jav a2 s .  c o m*/
    assertThat(cookie.getMaxAge()).isZero();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void loginSuccessNormalWithNonUserDetailsBasedPrincipalSetsExpectedCookie() {
    // SEC-822//from  w w  w. j a v a 2  s  .  c o m
    services.setTokenValiditySeconds(500000000);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(TokenBasedRememberMeServices.DEFAULT_PARAMETER, "true");

    MockHttpServletResponse response = new MockHttpServletResponse();
    services.loginSuccess(request, response, new TestingAuthenticationToken("someone", "password", "ROLE_ABC"));

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    String expiryTime = services.decodeCookie(cookie.getValue())[1];
    long expectedExpiryTime = 1000L * 500000000;
    expectedExpiryTime += System.currentTimeMillis();
    assertThat(Long.parseLong(expiryTime) > expectedExpiryTime - 10000).isTrue();
    assertThat(cookie).isNotNull();
    assertThat(cookie.getMaxAge()).isEqualTo(services.getTokenValiditySeconds());
    assertThat(Base64.isArrayByteBase64(cookie.getValue().getBytes())).isTrue();
    assertThat(new Date().before(new Date(determineExpiryTimeFromBased64EncodedToken(cookie.getValue()))))
            .isTrue();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void loginSuccessNormalWithUserDetailsBasedPrincipalSetsExpectedCookie() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(TokenBasedRememberMeServices.DEFAULT_PARAMETER, "true");

    MockHttpServletResponse response = new MockHttpServletResponse();
    services.loginSuccess(request, response, new TestingAuthenticationToken("someone", "password", "ROLE_ABC"));

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(cookie).isNotNull();//from  w  w  w  .  ja  v a2s . co m
    assertThat(cookie.getMaxAge()).isEqualTo(services.getTokenValiditySeconds());
    assertThat(Base64.isArrayByteBase64(cookie.getValue().getBytes())).isTrue();
    assertThat(new Date().before(new Date(determineExpiryTimeFromBased64EncodedToken(cookie.getValue()))))
            .isTrue();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void negativeValidityPeriodIsSetOnCookieButExpiryTimeRemainsAtTwoWeeks() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(DEFAULT_PARAMETER, "true");

    MockHttpServletResponse response = new MockHttpServletResponse();
    services.setTokenValiditySeconds(-1);
    services.loginSuccess(request, response, new TestingAuthenticationToken("someone", "password", "ROLE_ABC"));

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(cookie).isNotNull();//from  www.j av a2  s .  co  m
    // Check the expiry time is within 50ms of two weeks from current time
    assertThat(determineExpiryTimeFromBased64EncodedToken(cookie.getValue())
            - System.currentTimeMillis() > TWO_WEEKS_S - 50).isTrue();
    assertThat(cookie.getMaxAge()).isEqualTo(-1);
    assertThat(Base64.isArrayByteBase64(cookie.getValue().getBytes())).isTrue();
}