2

I have a Spring Boot app that normally runs on tomcat (as a normal WAR), and for some URL patterns I have Spring-Security setup to force it to be HTTPS. All the HTTPS/certificates stuff is done outside of my application, so all this step (at least appears) to be doing is redirecting to the HTTPS address (and port).

My security configuration:

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

It all works as expected on tomcat, and I get redirected correctly.

Now I want to write some Spring @IntegrationTests, but am not able to get it to make requests to the HTTPS URL - I suspect I am missing something fairly obvious, but not having any joy with my attempts.

My basic test case:

@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 )
    }

I have tried the approach outlined here https://stackoverflow.com/a/34411279/258813 and https://stackoverflow.com/a/24491820/258813

But no joy - It still seems to be failing because the port it is attempting to access is the HTTP port, so I basically get:

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?

Am I missing something obvious? Is there another way to get the HTTPS port (or tell spring to use one)?

Community
  • 1
  • 1
rhinds
  • 9,976
  • 13
  • 68
  • 111
  • 1
    I read that error message as "You're telling me to use HTTPS but your Boot app only speaks plain HTTP." Are you able to use a plain http URL, `new URL("http://localhost:" + port + "/api")`? If not, could you run the integration tests in a Spring profile that doesn't enable the requiresSecure policy? – bdkosher Mar 09 '16 at 22:19
  • @bdkosher OK, I can try the different profile, am hoping not to have to pollute my config with test stuff to much, do you know if it's possible to have the test config outside of the main source set? – rhinds Mar 10 '16 at 07:15

0 Answers0