parent
ce2f669245
commit
7a204a5f58
|
@ -186,13 +186,14 @@ class LogoutConfigurerTests extends BaseSpringSpec {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def "LogoutConfigurer content negotiation default redirects"() {
|
def "LogoutConfigurer content negotiation text/html redirects"() {
|
||||||
setup:
|
setup:
|
||||||
loadConfig(LogoutHandlerContentNegotiation)
|
loadConfig(LogoutHandlerContentNegotiation)
|
||||||
when:
|
when:
|
||||||
login()
|
login()
|
||||||
request.method = 'POST'
|
request.method = 'POST'
|
||||||
request.servletPath = '/logout'
|
request.servletPath = '/logout'
|
||||||
|
request.addHeader('Accept', 'text/html')
|
||||||
springSecurityFilterChain.doFilter(request,response,chain)
|
springSecurityFilterChain.doFilter(request,response,chain)
|
||||||
then:
|
then:
|
||||||
response.status == 302
|
response.status == 302
|
||||||
|
|
|
@ -18,25 +18,19 @@ package org.springframework.security.samples.config;
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*;
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*;
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
|
||||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*;
|
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*;
|
||||||
|
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
import javax.servlet.Filter;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.security.samples.mvc.config.WebMvcConfiguration;
|
import org.springframework.security.samples.mvc.config.WebMvcConfiguration;
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.TestExecutionListeners;
|
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
|
||||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
|
||||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
|
||||||
import org.springframework.test.context.web.ServletTestExecutionListener;
|
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||||
|
@ -50,25 +44,18 @@ import org.springframework.web.context.WebApplicationContext;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { RootConfiguration.class, WebMvcConfiguration.class })
|
@ContextConfiguration(classes = { RootConfiguration.class, WebMvcConfiguration.class })
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class,
|
|
||||||
DependencyInjectionTestExecutionListener.class,
|
|
||||||
DirtiesContextTestExecutionListener.class,
|
|
||||||
TransactionalTestExecutionListener.class,
|
|
||||||
WithSecurityContextTestExecutionListener.class })
|
|
||||||
public class SecurityConfigTests {
|
public class SecurityConfigTests {
|
||||||
private MockMvc mvc;
|
private MockMvc mvc;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext context;
|
private WebApplicationContext context;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Filter springSecurityFilterChain;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
mvc = MockMvcBuilders.webAppContextSetup(context)
|
mvc = MockMvcBuilders.webAppContextSetup(context)
|
||||||
.addFilters(springSecurityFilterChain)
|
.apply(springSecurity())
|
||||||
.defaultRequest(get("/").with(testSecurityContext())).build();
|
.defaultRequest(get("/").accept(MediaType.TEXT_HTML)).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -16,17 +16,16 @@
|
||||||
package org.springframework.security.samples.config;
|
package org.springframework.security.samples.config;
|
||||||
|
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*;
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*;
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
|
|
||||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*;
|
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*;
|
||||||
|
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
import javax.servlet.Filter;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.security.samples.mvc.config.WebMvcConfiguration;
|
import org.springframework.security.samples.mvc.config.WebMvcConfiguration;
|
||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
@ -49,14 +48,11 @@ public class SecurityConfigTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext context;
|
private WebApplicationContext context;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Filter springSecurityFilterChain;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
mvc = MockMvcBuilders.webAppContextSetup(context)
|
mvc = MockMvcBuilders.webAppContextSetup(context)
|
||||||
.addFilters(springSecurityFilterChain)
|
.apply(springSecurity())
|
||||||
.defaultRequest(get("/").with(testSecurityContext())).build();
|
.defaultRequest(get("/").accept(MediaType.TEXT_HTML)).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -84,7 +80,8 @@ public class SecurityConfigTests {
|
||||||
@Test
|
@Test
|
||||||
@WithMockUser
|
@WithMockUser
|
||||||
public void logoutSuccess() throws Exception {
|
public void logoutSuccess() throws Exception {
|
||||||
mvc.perform(logout()).andExpect(redirectedUrl("/login?logout"))
|
mvc.perform(logout())
|
||||||
|
.andExpect(redirectedUrl("/login?logout"))
|
||||||
.andExpect(unauthenticated());
|
.andExpect(unauthenticated());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,6 +90,7 @@ public final class SecurityMockMvcRequestBuilders {
|
||||||
@Override
|
@Override
|
||||||
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
|
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
|
||||||
MockHttpServletRequest request = post(this.logoutUrl)
|
MockHttpServletRequest request = post(this.logoutUrl)
|
||||||
|
.accept(MediaType.TEXT_HTML, MediaType.ALL)
|
||||||
.buildRequest(servletContext);
|
.buildRequest(servletContext);
|
||||||
return this.postProcessor.postProcessRequest(request);
|
return this.postProcessor.postProcessRequest(request);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.core.userdetails.User;
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
@ -53,7 +54,10 @@ public class AuthenticationTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
mvc = MockMvcBuilders.webAppContextSetup(context)
|
||||||
|
.apply(springSecurity())
|
||||||
|
.defaultRequest(get("/").accept(MediaType.TEXT_HTML))
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue