By default a lookup of {@link UserDetailsService} is performed by type. This - * can be problematic if multiple {@link UserDetailsService} beans are declared. - */ - public UserDetailsRequestPostProcessor userDetailsServiceBeanId(String userDetailsServiceBeanId) { - this.userDetailsServiceBeanId = userDetailsServiceBeanId; - return this; - } - - public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { - UsernamePasswordAuthenticationToken authentication = authentication(request.getServletContext()); - save(authentication,request); - return request; - } - - private UsernamePasswordAuthenticationToken authentication(ServletContext servletContext) { - ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); - UserDetailsService userDetailsService = userDetailsService(context); - UserDetails userDetails = userDetailsService.loadUserByUsername(this.username); - return new UsernamePasswordAuthenticationToken( - userDetails, userDetails.getPassword(), userDetails.getAuthorities()); - } - - private UserDetailsService userDetailsService(ApplicationContext context) { - if(this.userDetailsServiceBeanId == null) { - return context.getBean(UserDetailsService.class); - } - return context.getBean(this.userDetailsServiceBeanId, UserDetailsService.class); - } - } - - private SecurityRequestPostProcessors() {} - -} diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/SpringSecurityTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/SpringSecurityTests.java deleted file mode 100644 index 6622d4972df..00000000000 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/context/SpringSecurityTests.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2002-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package org.springframework.test.web.servlet.samples.context; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.samples.context.SecurityRequestPostProcessors.user; -import static org.springframework.test.web.servlet.samples.context.SecurityRequestPostProcessors.userDeatilsService; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; - -import junit.framework.Assert; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.web.FilterChainProxy; -import org.springframework.security.web.context.HttpSessionSecurityContextRepository; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.ResultMatcher; -import org.springframework.test.web.servlet.request.RequestPostProcessor; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -/** - * Basic example that includes Spring Security configuration. - * - *
Note that currently there are no {@linkplain ResultMatcher ResultMatchers} - * built specifically for asserting the Spring Security context. However, it's - * quite easy to put them together as shown below, and Spring Security extensions - * will become available in the near future. - * - *
This also demonstrates a custom {@link RequestPostProcessor} which authenticates
- * a user to a particular {@link HttpServletRequest}.
- *
- * @author Rob Winch
- * @author Rossen Stoyanchev
- * @author Sam Brannen
- * @see SecurityRequestPostProcessors
- */
-@RunWith(SpringJUnit4ClassRunner.class)
-@WebAppConfiguration("src/test/resources/META-INF/web-resources")
-@ContextConfiguration({ "security.xml", "servlet-context.xml" })
-public class SpringSecurityTests {
-
- private static final String SEC_CONTEXT_ATTR = HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY;
-
- @Autowired
- private FilterChainProxy springSecurityFilterChain;
-
- @Autowired
- private WebApplicationContext wac;
-
- private MockMvc mockMvc;
-
-
- @Before
- public void setup() {
- this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)//
- .addFilters(this.springSecurityFilterChain)//
- .build();
- }
-
- @Test
- public void requiresAuthentication() throws Exception {
- mockMvc.perform(get("/user")).//
- andExpect(redirectedUrl("http://localhost/spring_security_login"));
- }
-
- @Test
- public void accessGranted() throws Exception {
- this.mockMvc.perform(get("/").//
- with(userDeatilsService("user"))).//
- andExpect(status().isOk()).//
- andExpect(forwardedUrl("/WEB-INF/layouts/standardLayout.jsp"));
- }
-
- @Test
- public void accessDenied() throws Exception {
- this.mockMvc.perform(get("/")//
- .with(user("user").roles("DENIED")))//
- .andExpect(status().isForbidden());
- }
-
- @Test
- public void userAuthenticates() throws Exception {
- final String username = "user";
- mockMvc.perform(post("/j_spring_security_check").//
- param("j_username", username).//
- param("j_password", "password")).//
- andExpect(redirectedUrl("/")).//
- andExpect(new ResultMatcher() {
-
- public void match(MvcResult mvcResult) throws Exception {
- HttpSession session = mvcResult.getRequest().getSession();
- SecurityContext securityContext = (SecurityContext) session.getAttribute(SEC_CONTEXT_ATTR);
- Assert.assertEquals(securityContext.getAuthentication().getName(), username);
- }
- });
- }
-
- @Test
- public void userAuthenticateFails() throws Exception {
- final String username = "user";
- mockMvc.perform(post("/j_spring_security_check").//
- param("j_username", username).//
- param("j_password", "invalid")).//
- andExpect(redirectedUrl("/spring_security_login?login_error")).//
- andExpect(new ResultMatcher() {
-
- public void match(MvcResult mvcResult) throws Exception {
- HttpSession session = mvcResult.getRequest().getSession();
- SecurityContext securityContext = (SecurityContext) session.getAttribute(SEC_CONTEXT_ATTR);
- Assert.assertNull(securityContext);
- }
- });
- }
-
-}
diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java
index a1e033dd4ee..80a0d138f34 100644
--- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java
+++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java
@@ -19,23 +19,14 @@ package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import java.nio.charset.Charset;
-import java.util.Collections;
-import java.util.Map;
-
-import javax.xml.bind.annotation.XmlRootElement;
import org.junit.Before;
import org.junit.Test;
-import org.springframework.hateoas.Link;
-import org.springframework.hateoas.ResourceSupport;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
@@ -116,20 +107,6 @@ public class ContentAssertionTests {
.andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
}
- @Test
- public void testSpringHateoasJsonLink() throws Exception {
- this.mockMvc.perform(get("/handle").accept(MediaType.APPLICATION_JSON))
- .andExpect(jsonPath("$.links[?(@.rel == 'self')].href").value("http://myhost/people"));
- }
-
- @Test
- public void testSpringHateoasXmlLink() throws Exception {
- Map