Search in sources :

Example 31 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieCsrfTokenRepositoryTests method saveTokenCustomPath.

@Test
public void saveTokenCustomPath() {
    String customPath = "/custompath";
    this.repository.setCookiePath(customPath);
    CsrfToken token = this.repository.generateToken(this.request);
    this.repository.saveToken(token, this.request, this.response);
    Cookie tokenCookie = this.response.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
    assertThat(tokenCookie.getPath()).isEqualTo(this.repository.getCookiePath());
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 32 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieCsrfTokenRepositoryTests method loadToken.

@Test
public void loadToken() {
    CsrfToken generateToken = this.repository.generateToken(this.request);
    this.request.setCookies(new Cookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, generateToken.getToken()));
    CsrfToken loadToken = this.repository.loadToken(this.request);
    assertThat(loadToken).isNotNull();
    assertThat(loadToken.getHeaderName()).isEqualTo(generateToken.getHeaderName());
    assertThat(loadToken.getParameterName()).isEqualTo(generateToken.getParameterName());
    assertThat(loadToken.getToken()).isNotEmpty();
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 33 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieCsrfTokenRepositoryTests method saveTokenNullCustomPath.

@Test
public void saveTokenNullCustomPath() {
    String customPath = null;
    this.repository.setCookiePath(customPath);
    CsrfToken token = this.repository.generateToken(this.request);
    this.repository.saveToken(token, this.request, this.response);
    Cookie tokenCookie = this.response.getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME);
    assertThat(tokenCookie.getPath()).isEqualTo(this.request.getContextPath());
}
Also used : Cookie(jakarta.servlet.http.Cookie) Test(org.junit.jupiter.api.Test)

Example 34 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieRequestCache method getRequest.

@Override
public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse response) {
    Cookie savedRequestCookie = WebUtils.getCookie(request, COOKIE_NAME);
    if (savedRequestCookie == null) {
        return null;
    }
    String originalURI = decodeCookie(savedRequestCookie.getValue());
    UriComponents uriComponents = UriComponentsBuilder.fromUriString(originalURI).build();
    DefaultSavedRequest.Builder builder = new DefaultSavedRequest.Builder();
    int port = getPort(uriComponents);
    return builder.setScheme(uriComponents.getScheme()).setServerName(uriComponents.getHost()).setRequestURI(uriComponents.getPath()).setQueryString(uriComponents.getQuery()).setServerPort(port).setMethod(request.getMethod()).build();
}
Also used : Cookie(jakarta.servlet.http.Cookie) UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder)

Example 35 with Cookie

use of jakarta.servlet.http.Cookie in project spring-security by spring-projects.

the class CookieRequestCache method saveRequest.

@Override
public void saveRequest(HttpServletRequest request, HttpServletResponse response) {
    if (!this.requestMatcher.matches(request)) {
        this.logger.debug("Request not saved as configured RequestMatcher did not match");
        return;
    }
    String redirectUrl = UrlUtils.buildFullRequestUrl(request);
    Cookie savedCookie = new Cookie(COOKIE_NAME, encodeCookie(redirectUrl));
    savedCookie.setMaxAge(COOKIE_MAX_AGE);
    savedCookie.setSecure(request.isSecure());
    savedCookie.setPath(getCookiePath(request));
    savedCookie.setHttpOnly(true);
    response.addCookie(savedCookie);
}
Also used : Cookie(jakarta.servlet.http.Cookie)

Aggregations

Cookie (jakarta.servlet.http.Cookie)208 Test (org.junit.jupiter.api.Test)147 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)45 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)40 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)30 Locale (java.util.Locale)19 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)19 MvcResult (org.springframework.test.web.servlet.MvcResult)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 Authentication (org.springframework.security.core.Authentication)11 Supplier (java.util.function.Supplier)10 Pattern (java.util.regex.Pattern)10 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)10 Assertions.fail (org.assertj.core.api.Assertions.fail)10 Test (org.junit.Test)10 SameSite (org.springframework.boot.web.server.Cookie.SameSite)10 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)9 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)9 IOException (java.io.IOException)8 LocaleContext (org.springframework.context.i18n.LocaleContext)8