|
| 1 | +package basic; |
| 2 | + |
| 3 | +import org.apache.http.auth.AuthScope; |
| 4 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 5 | +import org.apache.http.client.CredentialsProvider; |
| 6 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 7 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 8 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.beans.factory.annotation.Value; |
| 15 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 16 | +import org.springframework.boot.test.WebIntegrationTest; |
| 17 | +import org.springframework.context.annotation.Bean; |
| 18 | +import org.springframework.context.annotation.Configuration; |
| 19 | +import org.springframework.http.ResponseEntity; |
| 20 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 21 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 22 | +import org.springframework.web.client.RestTemplate; |
| 23 | + |
| 24 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@SpringApplicationConfiguration(classes = BasicSecurityApplication.class) |
| 26 | +@WebIntegrationTest(randomPort = true) |
| 27 | +public class BasicSecurityApplicationTests { |
| 28 | + |
| 29 | + @Value("${local.server.port}") |
| 30 | + private int port; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private RestTemplate restTemplate; |
| 34 | + |
| 35 | + @Test |
| 36 | + public void contextLoaded() throws Throwable { |
| 37 | + ResponseEntity<String> re = restTemplate |
| 38 | + .getForEntity("http://localhost:" + port + "/hi", String.class); |
| 39 | + String actual = re.getBody().trim().toLowerCase(); |
| 40 | + Assert.assertTrue(actual.contains("hello")); |
| 41 | + System.out.println("received: " + actual); |
| 42 | + } |
| 43 | + |
| 44 | + @Configuration |
| 45 | + public static class BasicAuthRestTemplateConfig { |
| 46 | + |
| 47 | + private String user = "pwebb", password = "boot"; |
| 48 | + |
| 49 | + @Bean |
| 50 | + RestTemplate auth() { |
| 51 | + // credentials |
| 52 | + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( |
| 53 | + this.user, this.password); |
| 54 | + |
| 55 | + CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 56 | + credsProvider.setCredentials(AuthScope.ANY, credentials); |
| 57 | + |
| 58 | + // set credentialsProvider on httpClient |
| 59 | + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); |
| 60 | + |
| 61 | + httpClientBuilder.setDefaultCredentialsProvider(credsProvider); |
| 62 | + CloseableHttpClient httpClient = httpClientBuilder.build(); |
| 63 | + |
| 64 | + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory( |
| 65 | + httpClient); |
| 66 | + |
| 67 | + RestTemplate restTemplate = new RestTemplate(); |
| 68 | + restTemplate.setRequestFactory(factory); |
| 69 | + |
| 70 | + return restTemplate; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments