BAEL-2262 Demo Spring Boot App for HTTPS enabled. (#5513)

* BAEL-1979 Added examples for SnakeYAML Library

* BAEL-1979 Moved the snakeyaml related code to libraries module

* BAEL-1979 Removed the System.out.println() statements and converted the assertTrue to assertEquals wherever possible.

* BAEL-1979 Removed println statements, small formatting fix in pom.xml

* BAEL-1466 Added a new module for apache-geode

* BAEL-1466 Updated the Integration Tests.

* BAEL-1466 Updated the Integration Tests.

* BAEL-1466 Updated the Integration Tests.

* BAEL-1466 Removed the Unnecessary code.

* BAEL-2262 Added code for demonstration of HTTPS enabled Spring Boot Application
This commit is contained in:
sandy03934
2018-10-22 01:11:25 +05:30
committed by maibin
parent c0132660e3
commit d67ad2151b
8 changed files with 158 additions and 1 deletions

View File

@@ -0,0 +1,67 @@
package org.baeldung.web;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.baeldung.ssl.HttpsEnabledApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.util.Base64;
import static org.junit.Assert.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HttpsEnabledApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("ssl")
public class HttpsApplicationIntegrationTest {
private static final String WELCOME_URL = "https://localhost:8443/welcome";
@Value("${trust.store}")
private Resource trustStore;
@Value("${trust.store.password}")
private String trustStorePassword;
@Test
public void whenGETanHTTPSResource_thenCorrectResponse() throws Exception {
ResponseEntity<String> response = restTemplate().exchange(WELCOME_URL, HttpMethod.GET, new HttpEntity<String>(withAuthorization("memuser", "pass")), String.class);
assertEquals("<h1>Welcome to Secured Site</h1>", response.getBody());
assertEquals(HttpStatus.OK, response.getStatusCode());
}
RestTemplate restTemplate() throws Exception {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray())
.build();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
HttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(factory);
}
HttpHeaders withAuthorization(String userName, String password) {
return new HttpHeaders() {
{
String auth = userName + ":" + password;
String authHeader = "Basic " + new String(Base64.getEncoder()
.encode(auth.getBytes()));
set("Authorization", authHeader);
}
};
}
}