Skip to content

Commit 9c0f2cc

Browse files
committed
AccessDeniedConfigTests groovy->java
Issue: gh-4939
1 parent f8247fa commit 9c0f2cc

File tree

6 files changed

+233
-47
lines changed

6 files changed

+233
-47
lines changed

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

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.eclipse.jetty.http.HttpStatus;
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.springframework.beans.factory.BeanCreationException;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
25+
import org.springframework.security.access.AccessDeniedException;
26+
import org.springframework.security.config.test.SpringTestContext;
27+
import org.springframework.security.config.test.SpringTestRule;
28+
import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners;
29+
import org.springframework.security.test.context.support.WithMockUser;
30+
import org.springframework.security.web.access.AccessDeniedHandler;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
import org.springframework.test.web.servlet.MockMvc;
33+
34+
import javax.servlet.http.HttpServletRequest;
35+
import javax.servlet.http.HttpServletResponse;
36+
37+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
38+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
39+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40+
41+
/**
42+
*
43+
* @author Luke Taylor
44+
* @author Josh Cummings
45+
*/
46+
@RunWith(SpringJUnit4ClassRunner.class)
47+
@SecurityTestExecutionListeners
48+
public class AccessDeniedConfigTests {
49+
private static final String CONFIG_LOCATION_PREFIX =
50+
"classpath:org/springframework/security/config/http/AccessDeniedConfigTests";
51+
52+
@Autowired
53+
MockMvc mvc;
54+
55+
@Rule
56+
public final SpringTestRule spring = new SpringTestRule();
57+
58+
@Test
59+
public void configureWhenAccessDeniedHandlerIsMissingLeadingSlashThenException() {
60+
SpringTestContext context = this.spring.configLocations(this.xml("NoLeadingSlash"));
61+
62+
assertThatThrownBy(() -> context.autowire())
63+
.isInstanceOf(BeanCreationException.class)
64+
.hasMessageContaining("errorPage must begin with '/'");
65+
}
66+
67+
@Test
68+
@WithMockUser
69+
public void configureWhenAccessDeniedHandlerRefThenAutowire()
70+
throws Exception {
71+
72+
this.spring.configLocations(this.xml("AccessDeniedHandler")).autowire();
73+
74+
this.mvc.perform(get("/"))
75+
.andExpect(status().is(HttpStatus.GONE_410));
76+
}
77+
78+
@Test
79+
public void configureWhenAccessDeniedHandlerUsesPathAndRefThenException() {
80+
SpringTestContext context = this.spring.configLocations(this.xml("UsesPathAndRef"));
81+
82+
assertThatThrownBy(() -> context.autowire())
83+
.isInstanceOf(BeanDefinitionParsingException.class)
84+
.hasMessageContaining("attribute error-page cannot be used together with the 'ref' attribute");
85+
}
86+
87+
private String xml(String configName) {
88+
return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml";
89+
}
90+
91+
public static class GoneAccessDeniedHandler implements AccessDeniedHandler {
92+
93+
@Override
94+
public void handle(HttpServletRequest request,
95+
HttpServletResponse response,
96+
AccessDeniedException accessDeniedException) {
97+
98+
response.setStatus(HttpStatus.GONE_410);
99+
}
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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" use-expressions="true">
28+
<access-denied-handler ref="adh"/>
29+
<intercept-url pattern="/**" access="denyAll"/>
30+
</http>
31+
32+
<b:bean name="adh"
33+
class="org.springframework.security.config.http.AccessDeniedConfigTests.GoneAccessDeniedHandler"/>
34+
35+
<b:import resource="userservice.xml"/>
36+
</b:beans>
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+
<access-denied-handler error-page="noLeadingSlash"/>
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+
<access-denied-handler error-page="/go-away" ref="adh"/>
29+
</http>
30+
31+
<b:bean name="adh"
32+
class="org.springframework.security.config.http.AccessDeniedConfigTests.GoneAccessDeniedHandler"/>
33+
34+
<b:import resource="userservice.xml"/>
35+
</b:beans>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
<user-service>
27+
<user name="user" password="password" authorities="ROLE_USER"/>
28+
</user-service>
29+
</b:beans>

0 commit comments

Comments
 (0)