1010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 
1111 * specific language governing permissions and limitations under the License. 
1212 */ 
13+ 
1314package  org .springframework .test .web .mock .servlet .samples .context ;
1415
1516import  static  org .springframework .test .web .mock .servlet .request .MockMvcRequestBuilders .get ;
3435import  org .springframework .security .web .context .HttpSessionSecurityContextRepository ;
3536import  org .springframework .test .context .ContextConfiguration ;
3637import  org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
38+ import  org .springframework .test .context .web .WebAppConfiguration ;
3739import  org .springframework .test .web .mock .servlet .MockMvc ;
3840import  org .springframework .test .web .mock .servlet .MvcResult ;
3941import  org .springframework .test .web .mock .servlet .ResultMatcher ;
4446/** 
4547 * Basic example that includes Spring Security configuration. 
4648 * 
47-  * <p>Note that currently there are no {@link  ResultMatcher}' built specifically  
48-  * for asserting the Spring Security context. However, it's quite easy to put  
49-  * them together as shown below and Spring Security extensions will become  
50-  * available in the near future. 
49+  * <p>Note that currently there are no {@linkplain  ResultMatcher ResultMatchers}  
50+  * built specifically  for asserting the Spring Security context. However, it's 
51+  * quite easy to put  them together as shown below,  and Spring Security extensions 
52+  * will become  available in the near future. 
5153 * 
5254 * <p>This also demonstrates a custom {@link RequestPostProcessor} which authenticates 
5355 * a user to a particular {@link HttpServletRequest}. 
5456 * 
55-  * <p>Also see the Javadoc of {@link GenericWebContextLoader}, a class that 
56-  * provides temporary support for loading WebApplicationContext by extending 
57-  * the TestContext framework. 
58-  * 
5957 * @author Rob Winch 
6058 * @author Rossen Stoyanchev 
59+  * @author Sam Brannen 
6160 * @see SecurityRequestPostProcessors 
6261 */ 
6362@ RunWith (SpringJUnit4ClassRunner .class )
64- @ ContextConfiguration (
65- 		loader =WebContextLoader .class ,
66- 		value ={
67- 			"classpath:org/springframework/test/web/mock/servlet/samples/context/security.xml" ,
68- 			"classpath:org/springframework/test/web/mock/servlet/samples/servlet-context.xml" 
69- 		})
63+ @ WebAppConfiguration ("src/test/resources/META-INF/web-resources" )
64+ @ ContextConfiguration ({ "security.xml" , "../servlet-context.xml"  })
7065public  class  SpringSecurityTests  {
7166
72- 	private  static  String  SEC_CONTEXT_ATTR  = HttpSessionSecurityContextRepository .SPRING_SECURITY_CONTEXT_KEY ;
67+ 	private  static  final   String  SEC_CONTEXT_ATTR  = HttpSessionSecurityContextRepository .SPRING_SECURITY_CONTEXT_KEY ;
7368
7469	@ Autowired 
7570	private  FilterChainProxy  springSecurityFilterChain ;
@@ -79,57 +74,67 @@ public class SpringSecurityTests {
7974
8075	private  MockMvc  mockMvc ;
8176
77+ 
8278	@ Before 
8379	public  void  setup () {
84- 		this .mockMvc  = MockMvcBuilders .webAppContextSetup (this .wac )
85- 				.addFilters (this .springSecurityFilterChain ).build ();
80+ 		this .mockMvc  = MockMvcBuilders .webAppContextSetup (this .wac )// 
81+ 		.addFilters (this .springSecurityFilterChain )// 
82+ 		.build ();
8683	}
8784
8885	@ Test 
8986	public  void  requiresAuthentication () throws  Exception  {
90- 		mockMvc .perform (get ("/user" ))
91- 			. andExpect (redirectedUrl ("http://localhost/spring_security_login" ));
87+ 		mockMvc .perform (get ("/user" )). // 
88+ 		andExpect (redirectedUrl ("http://localhost/spring_security_login" ));
9289	}
9390
9491	@ Test 
9592	public  void  accessGranted () throws  Exception  {
96- 		this .mockMvc .perform (get ("/" ).with (userDeatilsService ("user" )))
97- 			.andExpect (status ().isOk ())
98- 			.andExpect (forwardedUrl ("/WEB-INF/layouts/standardLayout.jsp" ));
93+ 		this .mockMvc .perform (get ("/" ).// 
94+ 		with (userDeatilsService ("user" ))).// 
95+ 		andExpect (status ().isOk ()).// 
96+ 		andExpect (forwardedUrl ("/WEB-INF/layouts/standardLayout.jsp" ));
9997	}
10098
10199	@ Test 
102100	public  void  accessDenied () throws  Exception  {
103- 		this .mockMvc .perform (get ("/" ).with (user ("user" ).roles ("DENIED" )))
104- 			.andExpect (status ().isForbidden ());
101+ 		this .mockMvc .perform (get ("/" )// 
102+ 		.with (user ("user" ).roles ("DENIED" )))// 
103+ 		.andExpect (status ().isForbidden ());
105104	}
106105
107106	@ Test 
108107	public  void  userAuthenticates () throws  Exception  {
109108		final  String  username  = "user" ;
110- 		mockMvc .perform (post ("/j_spring_security_check" ).param ("j_username" , username ).param ("j_password" , "password" ))
111- 			.andExpect (redirectedUrl ("/" ))
112- 			.andExpect (new  ResultMatcher () {
113- 				public  void  match (MvcResult  mvcResult ) throws  Exception  {
114- 					HttpSession  session  = mvcResult .getRequest ().getSession ();
115- 					SecurityContext  securityContext  = (SecurityContext ) session .getAttribute (SEC_CONTEXT_ATTR );
116- 					Assert .assertEquals (securityContext .getAuthentication ().getName (), username );
117- 				}
118- 			});
109+ 		mockMvc .perform (post ("/j_spring_security_check" ).// 
110+ 		param ("j_username" , username ).// 
111+ 		param ("j_password" , "password" )).// 
112+ 		andExpect (redirectedUrl ("/" )).// 
113+ 		andExpect (new  ResultMatcher () {
114+ 
115+ 			public  void  match (MvcResult  mvcResult ) throws  Exception  {
116+ 				HttpSession  session  = mvcResult .getRequest ().getSession ();
117+ 				SecurityContext  securityContext  = (SecurityContext ) session .getAttribute (SEC_CONTEXT_ATTR );
118+ 				Assert .assertEquals (securityContext .getAuthentication ().getName (), username );
119+ 			}
120+ 		});
119121	}
120122
121123	@ Test 
122124	public  void  userAuthenticateFails () throws  Exception  {
123125		final  String  username  = "user" ;
124- 		mockMvc .perform (post ("/j_spring_security_check" ).param ("j_username" , username ).param ("j_password" , "invalid" ))
125- 			.andExpect (redirectedUrl ("/spring_security_login?login_error" ))
126- 			.andExpect (new  ResultMatcher () {
127- 				public  void  match (MvcResult  mvcResult ) throws  Exception  {
128- 					HttpSession  session  = mvcResult .getRequest ().getSession ();
129- 					SecurityContext  securityContext  = (SecurityContext ) session .getAttribute (SEC_CONTEXT_ATTR );
130- 					Assert .assertNull (securityContext );
131- 				}
132- 			});
126+ 		mockMvc .perform (post ("/j_spring_security_check" ).// 
127+ 		param ("j_username" , username ).// 
128+ 		param ("j_password" , "invalid" )).// 
129+ 		andExpect (redirectedUrl ("/spring_security_login?login_error" )).// 
130+ 		andExpect (new  ResultMatcher () {
131+ 
132+ 			public  void  match (MvcResult  mvcResult ) throws  Exception  {
133+ 				HttpSession  session  = mvcResult .getRequest ().getSession ();
134+ 				SecurityContext  securityContext  = (SecurityContext ) session .getAttribute (SEC_CONTEXT_ATTR );
135+ 				Assert .assertNull (securityContext );
136+ 			}
137+ 		});
133138	}
134139
135140}
0 commit comments