Skip to content

Commit 359a73e

Browse files
authored
Merge pull request #5260 from jzheaux/gh-4939-FormLoginBeanDefinitionParserTests
FormLoginBeanDefinitionParserTests groovy->java
2 parents 52d9577 + 65326b1 commit 359a73e

9 files changed

+374
-154
lines changed

config/src/test/groovy/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests.groovy

-153
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.config.http;
17+
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.security.config.test.SpringTestRule;
22+
import org.springframework.security.web.WebAttributes;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
25+
import static org.hamcrest.core.IsNot.not;
26+
import static org.hamcrest.core.IsNull.nullValue;
27+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
32+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
33+
34+
35+
/**
36+
*
37+
* @author Luke Taylor
38+
* @author Josh Cummings
39+
*/
40+
public class FormLoginBeanDefinitionParserTests {
41+
private static final String CONFIG_LOCATION_PREFIX =
42+
"classpath:org/springframework/security/config/http/FormLoginBeanDefinitionParserTests";
43+
44+
@Rule
45+
public final SpringTestRule spring = new SpringTestRule();
46+
47+
@Autowired
48+
MockMvc mvc;
49+
50+
@Test
51+
public void getLoginWhenAutoConfigThenShowsDefaultLoginPage()
52+
throws Exception {
53+
54+
this.spring.configLocations(this.xml("Simple")).autowire();
55+
56+
String expectedContent =
57+
"<html><head><title>Login Page</title></head><body onload='document.f.username.focus();'>\n" +
58+
"<h3>Login with Username and Password</h3><form name='f' action='/login' method='POST'>\n" +
59+
"<table>\n" +
60+
" <tr><td>User:</td><td><input type='text' name='username' value=''></td></tr>\n" +
61+
" <tr><td>Password:</td><td><input type='password' name='password'/></td></tr>\n" +
62+
" <tr><td colspan='2'><input name=\"submit\" type=\"submit\" value=\"Login\"/></td></tr>\n" +
63+
"</table>\n" +
64+
"</form></body></html>";
65+
66+
this.mvc.perform(get("/login")).andExpect(content().string(expectedContent));
67+
}
68+
69+
@Test
70+
public void getLoginWhenConfiguredWithCustomAttributesThenLoginPageReflects()
71+
throws Exception {
72+
73+
this.spring.configLocations(this.xml("WithCustomAttributes")).autowire();
74+
75+
String expectedContent =
76+
"<html><head><title>Login Page</title></head><body onload='document.f.custom_user.focus();'>\n" +
77+
"<h3>Login with Username and Password</h3><form name='f' action='/signin' method='POST'>\n" +
78+
"<table>\n" +
79+
" <tr><td>User:</td><td><input type='text' name='custom_user' value=''></td></tr>\n" +
80+
" <tr><td>Password:</td><td><input type='password' name='custom_pass'/></td></tr>\n" +
81+
" <tr><td colspan='2'><input name=\"submit\" type=\"submit\" value=\"Login\"/></td></tr>\n" +
82+
"</table>\n" +
83+
"</form></body></html>";
84+
85+
this.mvc.perform(get("/login")).andExpect(content().string(expectedContent));
86+
}
87+
88+
@Test
89+
public void getLoginWhenConfiguredForOpenIdThenLoginPageReflects()
90+
throws Exception {
91+
92+
this.spring.configLocations(this.xml("WithOpenId")).autowire();
93+
94+
String expectedContent =
95+
"<html><head><title>Login Page</title></head><body onload='document.f.username.focus();'>\n" +
96+
"<h3>Login with Username and Password</h3><form name='f' action='/login' method='POST'>\n" +
97+
"<table>\n" +
98+
" <tr><td>User:</td><td><input type='text' name='username' value=''></td></tr>\n" +
99+
" <tr><td>Password:</td><td><input type='password' name='password'/></td></tr>\n" +
100+
" <tr><td colspan='2'><input name=\"submit\" type=\"submit\" value=\"Login\"/></td></tr>\n" +
101+
"</table>\n" +
102+
"</form><h3>Login with OpenID Identity</h3><form name='oidf' action='/login/openid' method='POST'>\n" +
103+
"<table>\n" +
104+
" <tr><td>Identity:</td><td><input type='text' size='30' name='openid_identifier'/></td></tr>\n" +
105+
" <tr><td colspan='2'><input name=\"submit\" type=\"submit\" value=\"Login\"/></td></tr>\n" +
106+
"</table>\n" +
107+
"</form></body></html>";
108+
109+
this.mvc.perform(get("/login")).andExpect(content().string(expectedContent));
110+
}
111+
112+
@Test
113+
public void getLoginWhenConfiguredForOpenIdWithCustomAttributesThenLoginPageReflects()
114+
throws Exception {
115+
116+
this.spring.configLocations(this.xml("WithOpenIdCustomAttributes")).autowire();
117+
118+
String expectedContent =
119+
"<html><head><title>Login Page</title></head><body onload='document.f.username.focus();'>\n" +
120+
"<h3>Login with Username and Password</h3><form name='f' action='/login' method='POST'>\n" +
121+
"<table>\n" +
122+
" <tr><td>User:</td><td><input type='text' name='username' value=''></td></tr>\n" +
123+
" <tr><td>Password:</td><td><input type='password' name='password'/></td></tr>\n" +
124+
" <tr><td colspan='2'><input name=\"submit\" type=\"submit\" value=\"Login\"/></td></tr>\n" +
125+
"</table>\n" +
126+
"</form><h3>Login with OpenID Identity</h3><form name='oidf' action='/signin' method='POST'>\n" +
127+
"<table>\n" +
128+
" <tr><td>Identity:</td><td><input type='text' size='30' name='openid_identifier'/></td></tr>\n" +
129+
" <tr><td colspan='2'><input name=\"submit\" type=\"submit\" value=\"Login\"/></td></tr>\n" +
130+
"</table>\n" +
131+
"</form></body></html>";
132+
133+
this.mvc.perform(get("/login")).andExpect(content().string(expectedContent));
134+
}
135+
136+
@Test
137+
public void failedLoginWhenConfiguredWithCustomAuthenticationFailureThenForwardsAccordingly()
138+
throws Exception {
139+
140+
this.spring.configLocations(this.xml("WithAuthenticationFailureForwardUrl")).autowire();
141+
142+
this.mvc.perform(post("/login")
143+
.param("username", "bob")
144+
.param("password", "invalidpassword"))
145+
.andExpect(status().isOk())
146+
.andExpect(forwardedUrl("/failure_forward_url"))
147+
.andExpect(request().attribute(WebAttributes.AUTHENTICATION_EXCEPTION, not(nullValue())));
148+
}
149+
150+
@Test
151+
public void successfulLoginWhenConfiguredWithCustomAuthenticationSuccessThenForwardsAccordingly()
152+
throws Exception {
153+
154+
this.spring.configLocations(this.xml("WithAuthenticationSuccessForwardUrl")).autowire();
155+
156+
this.mvc.perform(post("/login")
157+
.param("username", "user")
158+
.param("password", "password"))
159+
.andExpect(status().isOk())
160+
.andExpect(forwardedUrl("/success_forward_url"));
161+
}
162+
163+
private String xml(String configName) {
164+
return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml";
165+
}
166+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2002-2018 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://www.springframework.org/schema/security"
21+
xsi:schemaLocation="
22+
http://www.springframework.org/schema/security
23+
http://www.springframework.org/schema/security/spring-security.xsd
24+
http://www.springframework.org/schema/beans
25+
http://www.springframework.org/schema/beans/spring-beans.xsd">
26+
27+
<http auto-config="true">
28+
<csrf disabled="true"/>
29+
</http>
30+
31+
<b:import resource="userservice.xml"/>
32+
</b:beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2002-2018 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://www.springframework.org/schema/security"
21+
xsi:schemaLocation="
22+
http://www.springframework.org/schema/security
23+
http://www.springframework.org/schema/security/spring-security.xsd
24+
http://www.springframework.org/schema/beans
25+
http://www.springframework.org/schema/beans/spring-beans.xsd">
26+
27+
<http auto-config="true">
28+
<form-login
29+
authentication-failure-forward-url="/failure_forward_url"/>
30+
31+
<csrf disabled="true"/>
32+
</http>
33+
34+
<b:import resource="userservice.xml"/>
35+
</b:beans>

0 commit comments

Comments
 (0)