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");
}
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);
}
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);
}
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);
}
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();
}
}
Aggregations