Spring Boot - 使用HTTPS进行集成测试

时间:2016-03-09 17:10:07

标签: spring groovy spring-boot integration-testing

我有一个通常在tomcat上运行的Spring Boot应用程序(作为普通的WAR),对于某些URL模式,我有Spring-Security设置强制它为HTTPS。所有的HTTPS /证书都是在我的应用程序之外完成的,所以这一步(至少看起来)要做的就是重定向到HTTPS地址(和端口)。

我的安全配置:

http.requiresChannel().requestMatchers( "/**" ) .requiresSecure()

这一切都在tomcat上按预期工作,我得到了正确的重定向。

现在我想写一些Spring @IntegrationTest,但我无法让它向HTTPS URL发出请求 - 我怀疑我错过了一些相当明显的东西,但对我的尝试没有任何喜悦。

我的基本测试用例:

@RunWith( SpringJUnit4ClassRunner )
@SpringApplicationConfiguration( classes = Application )
@WebAppConfiguration
@IntegrationTest( [ "server.port:0" ] )
class MultipleUserAuthenticationTest {

    @Value('${local.server.port}') private int port

    private URL base
    private RestTemplate template

    @Before public void setUp() throws Exception {
        this.base = new URL("https://localhost:" + port + "/api")
        template = new RestTemplate()
    }

    @Test public void apiRequest() throws Exception {
        HttpHeaders headers = //set custom headers
        HttpEntity<String> entity = new HttpEntity<String>( headers )
        ResponseEntity<String> exchange = template.exchange( base.toString(), HttpMethod.GET, entity, String )
    }

我尝试了此处列出的方法https://stackoverflow.com/a/34411279/258813https://stackoverflow.com/a/24491820/258813

但没有快乐 - 它似乎仍然失败,因为它试图访问的端口是HTTP端口,所以我基本上得到:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:52002/api":Unrecognized SSL message, plaintext connection?; nested exception is javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

我错过了一些明显的东西吗?有没有其他方法来获取HTTPS端口(或告诉spring使用它)?

0 个答案:

没有答案