现在的位置: 首页 > 编程语言 > 正文

SpringSecurity测试实战

2020年02月13日 编程语言 ⁄ 共 2518字 ⁄ 字号 评论关闭

引言

试题管理系统的安全模块使用Spring Security,代码从原华软仓库移植,在移植的过程中,发现原测试编写的不好,遂在新系统中对安全模块测试进行了重构。

Spring 测试

添加@SpringBootTest注解,意为这是一个基于SpringBoot的单元测试。

SpringBoot在官方的Guide中提供了多种测试方式。

@SpringBootTest注解中的webEnvironment属性可以配置测试环境,默认为MOCK环境。

/** * The type of web environment to create when applicable. Defaults to * {@link WebEnvironment#MOCK}. * @return the type of web environment */WebEnvironment webEnvironment() default WebEnvironment.MOCK;

模拟环境测试

启用Spring Security后,单元测试中对api的测试会被Spring Security的Filter进行拦截,所以测试之前需要进行用户登录操作。

之前都是使用比较笨重的方法,写一个@Before,@Before里进行登录,之后再执行测试方法。

最近在阅读Spring Security Test文档之后,终于找到一种模拟登录十分简便的方法,@WithMockUser。

test method with mock user - spring security test

引入Spring Security Test依赖:

<!-- Spring Security Test --><dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope></dependency>

示例代码如下:

@SpringBootTest@RunWith(SpringRunner.class)@AutoConfigureMockMvc@WithMockUser(username = "admin", password = "admin")public class ControllerTest { @Autowired protected MockMvc mockMvc; @Test void contextLoads() { }}

注:@RunWith(SpringRunner.class)表示当前测试使用org.springframework.test.context.junit4.SpringRunner类来执行,最新的SpringBoot版本中已经全面启用junit5,不推荐使用junit4.SpringRunner,因为未经过内部学习与测试,未在生产项目中使用。

真实环境测试

为了减少学习与沟通的成本,之前,所有的测试规定都在MOCK环境下,使用MockMVC进行api测试。

虽然MOCK环境能解决大部分的问题,并且可以在不启动Server的情况下直接进行测试,但在某些场景下,仍需要真实环境下的HTTP服务与请求测试。

启用Spring Security后,MockMVC是直接测试控制器,并非在真实的HTTP服务器下进行测试,MOCK环境中使用的是MockHttpSession,这不是标准的Session实现,没有加入对COOKIE的支持,所以在测试安全模块时,无法像浏览器一样测试COOKIE等认证信息。

spring mockmvc doesn't contain cookies - stackoverflow

去StackOverflow上也没有解决方案,答案推荐使用TestRestTemplate+真实的服务器环境进行单元测试。

将webEnvironment配置为SpringBootTest.WebEnvironment.RANDOM_PORT,即表示当前测试在一个随机端口的真实Web环境下运行。

@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)class AuthControllerTest { @Autowired private TestRestTemplate restTemplate;}

测试时使用TestRestTemplate进行网络请求的发送,真实模拟Web服务器环境。

示例代码如下:

logger.debug("3: 测试用户名密码正确");username = RIGHT_USERNAME;password = RIGHT_PASSWORD;response = this.restTemplate .withBasicAuth(username, password) .getForEntity(CONFIG_LOGIN, Void.class);logger.debug("断言: 状态码为200");assertThat(response.getStatusCode().value()).isEqualTo(HttpStatus.OK.value());logger.debug("获取 response 的 Set-Cookie 信息,并断言");String setCookie = response.getHeaders().getFirst(HttpHeaders.SET_COOKIE);assertThat(setCookie).isNotNull();

总结

两个各有优点,之前我们一直使用简单方便的Mock环境进行测试,但当我们有一天,发现这个Mock环境测试下的MockHttpSession无法满足需求的时候,我们开始探索其他的测试方案。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: SpringSecurity 测试实战

以上就上有关SpringSecurity测试实战的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.