Spring MVC 与 Spring Security 的集成测试

Posted

技术标签:

【中文标题】Spring MVC 与 Spring Security 的集成测试【英文标题】:Spring MVC integration tests with Spring Security 【发布时间】:2013-01-11 17:51:46 【问题描述】:

我正在尝试使用 mvc-test 测试我的登录页面。 在添加 spring 安全性之前,我的工作非常好。

我的代码是:

 mockMvc.perform(
     post("j_spring_security_check")
                    .param(LOGIN_FORM_USERNAME_FIELD, testUsernameValue)
                    .param(LOGIN_FORM_PASSWORD_FIELD, testPasswordValue))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(model().attribute(LOGIN_PAGE_STATUS_VALUE, LOGIN_PAGE_STATUS_FALSE_INDICATOR));

测试类添加了正确的注释:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "classpath:security-context.xml", "classpath:applicationContext.xml", "classpath:test-contexts/test-context.xml" )

我的过滤器已定义(在 web.xml 中):

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

当我尝试在 @ContextConfiguration 中添加 web.xml 时,它失败了,当我删除它时,我得到一个异常:

java.lang.AssertionError: Status expected:<200> but was:<405>

有什么方法可以添加 DelegatingProxyFilter 来测试上下文,并在我的 security-context.xml 中定义配置以使其正常工作?我尝试了一些关于注入 FilterProxyChain 的教程,但在我的情况下它不起作用。

有人可以帮我吗? 提前致谢

【问题讨论】:

不要认为你可以通过这种方式测试安全过滤器,因为它不是托管的完整的 http 服务器,它只是解决 spring-mvc-test 提供的 rls 的一种方法。您可能只需要对身份验证位中的各个部分进行单元测试。 【参考方案1】:

更新:Spring Security 4+ 提供开箱即用的integration with MockMvc。为了使用它,请确保您使用apply(springSecurity()),如下所示:

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class MockMvcSecurityTests 

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;

    @Before
    public void setup() 
        mvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
    
    ...

原答案

我不确定“当我尝试在 @ContextConfiguration 中添加 web.xml 时失败”是什么意思,但是,您可以使用 Spring Test MVC 来验证 Spring Security。有一个很好的example outlined in the spring-test-mvc project。

基本轮廓如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "classpath:security-context.xml", "classpath:applicationContext.xml", "classpath:test-contexts/test-context.xml" )
public class MyTests 

    @Autowired
    private FilterChainProxy springSecurityFilterChain;

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() 
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac)
            .addFilters(this.springSecurityFilterChain).build();
    

这个想法是你@AutowireFilterChainProxyDelegatingProxyFilter 代表什么)并指示MockMvc 使用FilterChainProxy

注意 spring-test-mvc 已集成到 spring-test-3.2+ 和 Spring 3.1.x 的单独项目中,因此您可以相当互换地使用该示例(spring-test-mvc 确实不支持@WebAppConfiguration,必须改用WebContextLoader)。

【讨论】:

很棒的 gihub 链接!谢谢! 注意。 spring-security 的 spring boot starter POM 不包括 spring-security-test。我必须将它添加到我的主 POM 以获取 springSecurity() Boot 使用 spring security bom,它在 4.1.0+ 中包含 spring-security-test @Rob 我有 spring-boot-starter-security、spring-boot-starter-test 和 spring-security-test (4.1.0.RELEASE) 和 Spring Boot 1.3.5。如果我删除 spring-security-test 那么它对我不起作用。 对不起,我误会了。是的,您需要包括 spring-security-test。但是,您不需要提供版本。【参考方案2】:

在 pom.xml 中添加

<repository>
    <id>spring-snaspho</id>
    <url>http://repo.springsource.org/libs-milestone/</url>
</repository>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <version>4.0.0.M1</version>
</dependency>

并使用 org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors 进行授权请求。请参阅https://github.com/rwinch/spring-security-test-blog (https://jira.spring.io/browse/SEC-2592) 上的示例用法

【讨论】:

以上是关于Spring MVC 与 Spring Security 的集成测试的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC 与 Spring Security 的集成测试

这几天做spring与mybatis整合,配置文件到是会用了,但是否很明白spring.xml 与spring.mvc.xml区别?

spring boot与spring mvc的区别

spring boot与spring mvc的区别是什么?

深入分析Spring 与 Spring MVC容器

Spring MVC(异步)与 Spring WebFlux