Search in sources :

Example 6 with Cookie

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

the class RememberMeConfigTests method requestWithRememberMeWhenUsingCustomUserDetailsServiceThenInvokesThisUserDetailsService.

@Test
public void requestWithRememberMeWhenUsingCustomUserDetailsServiceThenInvokesThisUserDetailsService() throws Exception {
    this.spring.configLocations(xml("WithUserDetailsService")).autowire();
    UserDetailsService userDetailsService = this.spring.getContext().getBean(UserDetailsService.class);
    given(userDetailsService.loadUserByUsername("user")).willAnswer((invocation) -> new User("user", "{noop}password", Collections.emptyList()));
    MvcResult result = rememberAuthentication("user", "password").andReturn();
    Cookie cookie = rememberMeCookie(result);
    // @formatter:off
    this.mvc.perform(get("/authenticated").cookie(cookie)).andExpect(status().isOk());
    // @formatter:on
    verify(userDetailsService, atLeastOnce()).loadUserByUsername("user");
}
Also used : Cookie(jakarta.servlet.http.Cookie) User(org.springframework.security.core.userdetails.User) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 7 with Cookie

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

the class RememberMeConfigTests method requestWithRememberMeWhenUsingAuthenticationSuccessHandlerThenInvokesHandler.

@Test
public void requestWithRememberMeWhenUsingAuthenticationSuccessHandlerThenInvokesHandler() throws Exception {
    this.spring.configLocations(xml("WithAuthenticationSuccessHandler")).autowire();
    TestDataSource dataSource = this.spring.getContext().getBean(TestDataSource.class);
    JdbcTemplate template = new JdbcTemplate(dataSource);
    template.execute(JdbcTokenRepositoryImpl.CREATE_TABLE_SQL);
    // @formatter:off
    MvcResult result = rememberAuthentication("user", "password").andExpect(cookie().secure(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, false)).andReturn();
    // @formatter:on
    Cookie cookie = rememberMeCookie(result);
    // @formatter:off
    this.mvc.perform(get("/authenticated").cookie(cookie)).andExpect(redirectedUrl("/target"));
    // @formatter:on
    int count = template.queryForObject("select count(*) from persistent_logins", int.class);
    assertThat(count).isEqualTo(1);
}
Also used : Cookie(jakarta.servlet.http.Cookie) TestDataSource(org.springframework.security.TestDataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 8 with Cookie

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

the class RememberMeConfigTests method requestWithRememberMeWhenUsingCustomDataSourceThenAutomaticallyReauthenticates.

@Test
public void requestWithRememberMeWhenUsingCustomDataSourceThenAutomaticallyReauthenticates() throws Exception {
    this.spring.configLocations(xml("WithDataSource")).autowire();
    TestDataSource dataSource = this.spring.getContext().getBean(TestDataSource.class);
    JdbcTemplate template = new JdbcTemplate(dataSource);
    template.execute(JdbcTokenRepositoryImpl.CREATE_TABLE_SQL);
    // @formatter:off
    MvcResult result = rememberAuthentication("user", "password").andExpect(cookie().secure(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, false)).andReturn();
    // @formatter:on
    Cookie cookie = rememberMeCookie(result);
    // @formatter:off
    this.mvc.perform(get("/authenticated").cookie(cookie)).andExpect(status().isOk());
    // @formatter:on
    int count = template.queryForObject("select count(*) from persistent_logins", int.class);
    assertThat(count).isEqualTo(1);
}
Also used : Cookie(jakarta.servlet.http.Cookie) TestDataSource(org.springframework.security.TestDataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 9 with Cookie

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

the class RememberMeConfigTests method requestWithRememberMeWhenUsingCustomTokenRepositoryThenAutomaticallyReauthenticates.

@Test
public void requestWithRememberMeWhenUsingCustomTokenRepositoryThenAutomaticallyReauthenticates() throws Exception {
    this.spring.configLocations(xml("WithTokenRepository")).autowire();
    // @formatter:off
    MvcResult result = rememberAuthentication("user", "password").andExpect(cookie().secure(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, false)).andReturn();
    // @formatter:on
    Cookie cookie = rememberMeCookie(result);
    // @formatter:off
    this.mvc.perform(get("/authenticated").cookie(cookie)).andExpect(status().isOk());
    // @formatter:on
    JdbcTemplate template = this.spring.getContext().getBean(JdbcTemplate.class);
    int count = template.queryForObject("select count(*) from persistent_logins", int.class);
    assertThat(count).isEqualTo(1);
}
Also used : Cookie(jakarta.servlet.http.Cookie) MvcResult(org.springframework.test.web.servlet.MvcResult) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Example 10 with Cookie

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

the class CookieClearingLogoutHandlerTests method passedInCookiesAreCleared.

@Test
public void passedInCookiesAreCleared() {
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/foo/bar");
    Cookie cookie1 = new Cookie("my_cookie", null);
    cookie1.setPath("/foo");
    cookie1.setMaxAge(0);
    Cookie cookie2 = new Cookie("my_cookie_too", null);
    cookie2.setPath("/foo");
    cookie2.setMaxAge(0);
    CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler(cookie1, cookie2);
    handler.logout(request, response, mock(Authentication.class));
    assertThat(response.getCookies()).hasSize(2);
    for (Cookie c : response.getCookies()) {
        assertThat(c.getPath()).isEqualTo("/foo");
        assertThat(c.getMaxAge()).isZero();
    }
}
Also used : Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Cookie (jakarta.servlet.http.Cookie)197 Test (org.junit.jupiter.api.Test)137 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 Authentication (org.springframework.security.core.Authentication)11 Test (org.junit.Test)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 SimpleLocaleContext (org.springframework.context.i18n.SimpleLocaleContext)8 SimpleTimeZoneAwareLocaleContext (org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext)8 TimeZoneAwareLocaleContext (org.springframework.context.i18n.TimeZoneAwareLocaleContext)8 Map (java.util.Map)6 SavedCookie (org.springframework.security.web.savedrequest.SavedCookie)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6