6 changed files with 233 additions and 47 deletions
@ -1,47 +0,0 @@ |
|||||||
package org.springframework.security.config.http |
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException |
|
||||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException |
|
||||||
import org.springframework.security.web.access.AccessDeniedHandlerImpl |
|
||||||
import org.springframework.security.web.access.ExceptionTranslationFilter |
|
||||||
|
|
||||||
/** |
|
||||||
* |
|
||||||
* @author Luke Taylor |
|
||||||
*/ |
|
||||||
class AccessDeniedConfigTests extends AbstractHttpConfigTests { |
|
||||||
def invalidAccessDeniedUrlIsDetected() { |
|
||||||
when: |
|
||||||
httpAutoConfig() { |
|
||||||
'access-denied-handler'('error-page':'noLeadingSlash') |
|
||||||
} |
|
||||||
createAppContext(); |
|
||||||
then: |
|
||||||
thrown(BeanCreationException) |
|
||||||
} |
|
||||||
|
|
||||||
def accessDeniedHandlerIsSetCorectly() { |
|
||||||
httpAutoConfig() { |
|
||||||
'access-denied-handler'(ref: 'adh') |
|
||||||
} |
|
||||||
bean('adh', AccessDeniedHandlerImpl) |
|
||||||
createAppContext(); |
|
||||||
|
|
||||||
def filter = getFilter(ExceptionTranslationFilter.class); |
|
||||||
def adh = appContext.getBean("adh"); |
|
||||||
|
|
||||||
expect: |
|
||||||
filter.accessDeniedHandler == adh |
|
||||||
} |
|
||||||
|
|
||||||
def void accessDeniedHandlerPageAndRefAreMutuallyExclusive() { |
|
||||||
when: |
|
||||||
httpAutoConfig { |
|
||||||
'access-denied-handler'('error-page': '/go-away', ref: 'adh') |
|
||||||
} |
|
||||||
createAppContext(); |
|
||||||
bean('adh', AccessDeniedHandlerImpl) |
|
||||||
then: |
|
||||||
thrown(BeanDefinitionParsingException) |
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,101 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2018 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.security.config.http; |
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpStatus; |
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.springframework.beans.factory.BeanCreationException; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; |
||||||
|
import org.springframework.security.access.AccessDeniedException; |
||||||
|
import org.springframework.security.config.test.SpringTestContext; |
||||||
|
import org.springframework.security.config.test.SpringTestRule; |
||||||
|
import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners; |
||||||
|
import org.springframework.security.test.context.support.WithMockUser; |
||||||
|
import org.springframework.security.web.access.AccessDeniedHandler; |
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||||
|
import org.springframework.test.web.servlet.MockMvc; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @author Luke Taylor |
||||||
|
* @author Josh Cummings |
||||||
|
*/ |
||||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||||
|
@SecurityTestExecutionListeners |
||||||
|
public class AccessDeniedConfigTests { |
||||||
|
private static final String CONFIG_LOCATION_PREFIX = |
||||||
|
"classpath:org/springframework/security/config/http/AccessDeniedConfigTests"; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
MockMvc mvc; |
||||||
|
|
||||||
|
@Rule |
||||||
|
public final SpringTestRule spring = new SpringTestRule(); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void configureWhenAccessDeniedHandlerIsMissingLeadingSlashThenException() { |
||||||
|
SpringTestContext context = this.spring.configLocations(this.xml("NoLeadingSlash")); |
||||||
|
|
||||||
|
assertThatThrownBy(() -> context.autowire()) |
||||||
|
.isInstanceOf(BeanCreationException.class) |
||||||
|
.hasMessageContaining("errorPage must begin with '/'"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
@WithMockUser |
||||||
|
public void configureWhenAccessDeniedHandlerRefThenAutowire() |
||||||
|
throws Exception { |
||||||
|
|
||||||
|
this.spring.configLocations(this.xml("AccessDeniedHandler")).autowire(); |
||||||
|
|
||||||
|
this.mvc.perform(get("/")) |
||||||
|
.andExpect(status().is(HttpStatus.GONE_410)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void configureWhenAccessDeniedHandlerUsesPathAndRefThenException() { |
||||||
|
SpringTestContext context = this.spring.configLocations(this.xml("UsesPathAndRef")); |
||||||
|
|
||||||
|
assertThatThrownBy(() -> context.autowire()) |
||||||
|
.isInstanceOf(BeanDefinitionParsingException.class) |
||||||
|
.hasMessageContaining("attribute error-page cannot be used together with the 'ref' attribute"); |
||||||
|
} |
||||||
|
|
||||||
|
private String xml(String configName) { |
||||||
|
return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml"; |
||||||
|
} |
||||||
|
|
||||||
|
public static class GoneAccessDeniedHandler implements AccessDeniedHandler { |
||||||
|
|
||||||
|
@Override |
||||||
|
public void handle(HttpServletRequest request, |
||||||
|
HttpServletResponse response, |
||||||
|
AccessDeniedException accessDeniedException) { |
||||||
|
|
||||||
|
response.setStatus(HttpStatus.GONE_410); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,36 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<!-- |
||||||
|
~ Copyright 2002-2018 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. |
||||||
|
--> |
||||||
|
|
||||||
|
<b:beans xmlns:b="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns="http://www.springframework.org/schema/security" |
||||||
|
xsi:schemaLocation=" |
||||||
|
http://www.springframework.org/schema/security |
||||||
|
http://www.springframework.org/schema/security/spring-security.xsd |
||||||
|
http://www.springframework.org/schema/beans |
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||||
|
|
||||||
|
<http auto-config="true" use-expressions="true"> |
||||||
|
<access-denied-handler ref="adh"/> |
||||||
|
<intercept-url pattern="/**" access="denyAll"/> |
||||||
|
</http> |
||||||
|
|
||||||
|
<b:bean name="adh" |
||||||
|
class="org.springframework.security.config.http.AccessDeniedConfigTests.GoneAccessDeniedHandler"/> |
||||||
|
|
||||||
|
<b:import resource="userservice.xml"/> |
||||||
|
</b:beans> |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<!-- |
||||||
|
~ Copyright 2002-2018 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. |
||||||
|
--> |
||||||
|
|
||||||
|
<b:beans xmlns:b="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns="http://www.springframework.org/schema/security" |
||||||
|
xsi:schemaLocation=" |
||||||
|
http://www.springframework.org/schema/security |
||||||
|
http://www.springframework.org/schema/security/spring-security.xsd |
||||||
|
http://www.springframework.org/schema/beans |
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||||
|
|
||||||
|
<http auto-config="true"> |
||||||
|
<access-denied-handler error-page="noLeadingSlash"/> |
||||||
|
</http> |
||||||
|
|
||||||
|
<b:import resource="userservice.xml"/> |
||||||
|
</b:beans> |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<!-- |
||||||
|
~ Copyright 2002-2018 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. |
||||||
|
--> |
||||||
|
|
||||||
|
<b:beans xmlns:b="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns="http://www.springframework.org/schema/security" |
||||||
|
xsi:schemaLocation=" |
||||||
|
http://www.springframework.org/schema/security |
||||||
|
http://www.springframework.org/schema/security/spring-security.xsd |
||||||
|
http://www.springframework.org/schema/beans |
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||||
|
|
||||||
|
<http auto-config="true"> |
||||||
|
<access-denied-handler error-page="/go-away" ref="adh"/> |
||||||
|
</http> |
||||||
|
|
||||||
|
<b:bean name="adh" |
||||||
|
class="org.springframework.security.config.http.AccessDeniedConfigTests.GoneAccessDeniedHandler"/> |
||||||
|
|
||||||
|
<b:import resource="userservice.xml"/> |
||||||
|
</b:beans> |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<!-- |
||||||
|
~ Copyright 2002-2018 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. |
||||||
|
--> |
||||||
|
|
||||||
|
<b:beans xmlns:b="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns="http://www.springframework.org/schema/security" |
||||||
|
xsi:schemaLocation=" |
||||||
|
http://www.springframework.org/schema/security |
||||||
|
http://www.springframework.org/schema/security/spring-security.xsd |
||||||
|
http://www.springframework.org/schema/beans |
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd"> |
||||||
|
<user-service> |
||||||
|
<user name="user" password="password" authorities="ROLE_USER"/> |
||||||
|
</user-service> |
||||||
|
</b:beans> |
||||||
Loading…
Reference in new issue