测试安全的spring mvc restful webservice

时间:2018-04-17 21:21:41

标签: spring spring-mvc spring-boot spring-security spring-test

我有一个安全的网络服务,我一直在尝试测试。该服务看起来像这样:

@GetMapping(value= {"/v1/getSomeStuff"})
public SomeResp getSomeStuff(final UsernamePasswordAuthenticationToken token, @RequestParam(name="searchString", required=true) String request) {
    //do some stuff in here
}

使用Spring Security保护服务,我正在尝试编写一个IT测试,它将提供所需的UsernamePasswordAuthenticationToken。我试过这样做:

@Test
@Sql(scripts = { "/db-scripts/company-script.sql","/db-scripts/user-script.sql" })
@WithUserDetails(value="testuser")
public void testGetSomeStuff() {
    //test some stuff
    getRestTemplate().getForEntity("http://localhost:" + port + "/v1/getSomeStuff?searchString=foo", SomeResp.class);
}

正在执行Sql脚本并且正在调用用户详细信息服务,但是令牌未传递给服务。我理解(现在)@WithUserDetails用于测试方法安全性,不会做我想要的。那么我的问题是,是否有另一种使用各种弹簧测试类测试安全Web服务的方法?

0 个答案:

没有答案