Grails 集成测试中如何与rest客户端共享spring安全会话

Grails 集成测试中如何与rest客户端共享spring安全会话,grails,spring-security,grails-plugin,Grails,Spring Security,Grails Plugin,我想使用请求映射测试一个由spring安全性保护的RESTAPI。API主要由经过身份验证的用户使用,因此我想用登录用户测试API 在我使用的rest客户端的测试中 testCompile "org.grails:grails-datastore-rest-client:6.0.5.RELEASE" 在调用API之前,必须对用户进行身份验证。我在设置中尝试了以下操作: springSecurityService.reauthenticate "admin" 尽管进行了重新验证

我想使用请求映射测试一个由spring安全性保护的RESTAPI。API主要由经过身份验证的用户使用,因此我想用登录用户测试API

在我使用的rest客户端的测试中

    testCompile "org.grails:grails-datastore-rest-client:6.0.5.RELEASE"
在调用API之前,必须对用户进行身份验证。我在设置中尝试了以下操作:

    springSecurityService.reauthenticate "admin"
尽管进行了重新验证调用,但rest客户端无法检测到任何会话,因此状态代码为302->重定向到登录页面

void "test fetching execution statistic"() {
    given:
    RestBuilder rest = new RestBuilder()
    when:
    //requestHeaders.add("Cookie", "JSESSIONID=" + session.getValue());

    RestResponse response = rest.post("http://localhost:8080/correctionStatistic/executionStatistic") {
        json([
                reportingPeriod_year: 2008,
                reportingPeriod_month: 01
        ])
    }
    then:
    response.status == 200
}

如何与rest客户端共享会话?正如您在注释行中所看到的,一个想法是在请求头中添加会话ID,但是如何在集成测试中请求会话ID。

如果您只是使用
Spring Security Core Plugin
并为受保护的URL(端点)实现传统的有状态链,然后可以使用
Geb
编写功能测试用例

但是如果您正在使用
Spring-Security-Rest插件
并为URL实现无状态链,那么您需要首先点击
api/login
端点并获取访问令牌,然后在请求中使用
授权
头发出进一步的请求

你可以找到详细的指南

我希望这有帮助