10
10
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11
11
* specific language governing permissions and limitations under the License.
12
12
*/
13
+
13
14
package org .springframework .test .web .mock .servlet .samples .context ;
14
15
15
16
import static org .springframework .test .web .mock .servlet .request .MockMvcRequestBuilders .get ;
34
35
import org .springframework .security .web .context .HttpSessionSecurityContextRepository ;
35
36
import org .springframework .test .context .ContextConfiguration ;
36
37
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
38
+ import org .springframework .test .context .web .WebAppConfiguration ;
37
39
import org .springframework .test .web .mock .servlet .MockMvc ;
38
40
import org .springframework .test .web .mock .servlet .MvcResult ;
39
41
import org .springframework .test .web .mock .servlet .ResultMatcher ;
44
46
/**
45
47
* Basic example that includes Spring Security configuration.
46
48
*
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.
51
53
*
52
54
* <p>This also demonstrates a custom {@link RequestPostProcessor} which authenticates
53
55
* a user to a particular {@link HttpServletRequest}.
54
56
*
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
- *
59
57
* @author Rob Winch
60
58
* @author Rossen Stoyanchev
59
+ * @author Sam Brannen
61
60
* @see SecurityRequestPostProcessors
62
61
*/
63
62
@ 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" })
70
65
public class SpringSecurityTests {
71
66
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 ;
73
68
74
69
@ Autowired
75
70
private FilterChainProxy springSecurityFilterChain ;
@@ -79,57 +74,67 @@ public class SpringSecurityTests {
79
74
80
75
private MockMvc mockMvc ;
81
76
77
+
82
78
@ Before
83
79
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 ();
86
83
}
87
84
88
85
@ Test
89
86
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" ));
92
89
}
93
90
94
91
@ Test
95
92
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" ));
99
97
}
100
98
101
99
@ Test
102
100
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 ());
105
104
}
106
105
107
106
@ Test
108
107
public void userAuthenticates () throws Exception {
109
108
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
+ });
119
121
}
120
122
121
123
@ Test
122
124
public void userAuthenticateFails () throws Exception {
123
125
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
+ });
133
138
}
134
139
135
140
}
0 commit comments