SEC-2686: Create SecurityMockMvcConfigurer
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.test.web.servlet.setup;
|
||||
|
||||
import org.springframework.security.config.BeanIds;
|
||||
import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
||||
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcConfigurerAdapter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.testSecurityContext;
|
||||
|
||||
/**
|
||||
* Configures Spring Security by adding the springSecurityFilterChain and adding the {@link org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors#testSecurityContext()}.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 4.0
|
||||
*/
|
||||
final class SecurityMockMvcConfigurer extends MockMvcConfigurerAdapter {
|
||||
private Filter springSecurityFilterChain;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*/
|
||||
SecurityMockMvcConfigurer() {}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the provided {@link javax.servlet.Filter}
|
||||
* @param springSecurityFilterChain the {@link javax.servlet.Filter} to use
|
||||
*/
|
||||
SecurityMockMvcConfigurer(Filter springSecurityFilterChain) {
|
||||
this.springSecurityFilterChain = springSecurityFilterChain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder, WebApplicationContext context) {
|
||||
String securityBeanId = BeanIds.SPRING_SECURITY_FILTER_CHAIN;
|
||||
if(springSecurityFilterChain == null && context.containsBean(securityBeanId)) {
|
||||
springSecurityFilterChain = context.getBean(securityBeanId, Filter.class);
|
||||
}
|
||||
|
||||
if(springSecurityFilterChain == null) {
|
||||
throw new IllegalStateException("springSecurityFilterChain cannot be null. Ensure a Bean with the name "+ securityBeanId + " implementing Filter is present or inject the Filter to be used.");
|
||||
}
|
||||
|
||||
builder.addFilters(springSecurityFilterChain);
|
||||
|
||||
return testSecurityContext();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.test.web.servlet.setup;
|
||||
|
||||
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
/**
|
||||
* Provides Security related {@link org.springframework.test.web.servlet.setup.MockMvcConfigurer} implementations.
|
||||
*
|
||||
* @since 4.0
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public final class SecurityMockMvcConfigurers {
|
||||
/**
|
||||
* Configures the MockMvcBuilder for use with Spring Security. Specifically the configurer adds the Spring Bean
|
||||
* named "springSecurityFilterChain" as a Filter. It will also ensure that the TestSecurityContextHolder is leveraged
|
||||
* for each request by applying {@link org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors#testSecurityContext()}.
|
||||
*
|
||||
* @return the {@link org.springframework.test.web.servlet.setup.MockMvcConfigurer} to use
|
||||
*/
|
||||
public static MockMvcConfigurer springSecurity() {
|
||||
return new SecurityMockMvcConfigurer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the MockMvcBuilder for use with Spring Security. Specifically the configurer adds the provided Filter. It will also ensure that the
|
||||
* TestSecurityContextHolder is leveraged for each request by applying
|
||||
* {@link org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors#testSecurityContext()}.
|
||||
*
|
||||
* @param springSecurityFilterChain the Filter to be added
|
||||
*
|
||||
* @return the {@link org.springframework.test.web.servlet.setup.MockMvcConfigurer} to use
|
||||
*/
|
||||
public static MockMvcConfigurer springSecurity(Filter springSecurityFilterChain) {
|
||||
Assert.notNull(springSecurityFilterChain, "springSecurityFilterChain cannot be null");
|
||||
return new SecurityMockMvcConfigurer(springSecurityFilterChain);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user