/** * Init */ @PostConstruct protected void init() { restTemplateForAuthenticationFlow = new CookieStoreRestTemplate(); cookieStore = restTemplateForAuthenticationFlow.getCookieStore(); logger.debug("Inject cookie store used in the rest template for authentication flow into the authRestTemplate so that they will match"); authRestTemplate.restTemplate.setCookieStoreAndUpdateRequestFactory(cookieStore); List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList(new ClientHttpRequestInterceptor() { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { if (latestCsrfToken != null) { // At the beginning of auth flow, there's no token yet injectCsrfTokenIntoHeader(request, latestCsrfToken); } return execution.execute(request, body); } }); restTemplateForAuthenticationFlow.setRequestFactory(new InterceptingClientHttpRequestFactory(restTemplateForAuthenticationFlow.getRequestFactory(), interceptors)); }
@Override public ClientHttpRequestFactory getRequestFactory() { ClientHttpRequestFactory delegate = super.getRequestFactory(); if (!CollectionUtils.isEmpty(getInterceptors())) { return new InterceptingClientHttpRequestFactory(delegate, getInterceptors()); } else { return delegate; } }
private void addAuthentication() { if (StringUtils.isEmpty(username)) { throw new RuntimeException("Username is mandatory for Basic Auth"); } List<ClientHttpRequestInterceptor> interceptors = Collections .singletonList(new BasicAuthInterceptor(username, password)); setRequestFactory(new InterceptingClientHttpRequestFactory(getRequestFactory(), interceptors)); }
private RestTemplate getRawRestTemplate() { RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add( new ByteArrayHttpMessageConverter()); restTemplate.setRequestFactory( new InterceptingClientHttpRequestFactory( restTemplate.getRequestFactory(), Collections.singletonList( new BasicAuthorizationInterceptor("1", "1")))); return restTemplate; }
@Override protected void interceptorsIntegration(List<ClientHttpRequestInterceptor> lInterceptors, Object sslConfiguration) { this.setInterceptors(lInterceptors); SimpleClientHttpRequestFactory chrf = new SimpleClientHttpRequestFactory(); chrf.setOutputStreaming(false); this.setRequestFactory( new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(chrf), lInterceptors ) ); }
/** * hard to check the logs provided by the interceptor when there's no error * however this unit test garantees the interceptor does not alter the reply * from the rest service. */ @Test public void testInterceptor() { List<ClientHttpRequestInterceptor> lInterceptors = new ArrayList<>(); //spring boot default log level is info lInterceptors.add(new LoggingRequestInterceptor(StandardCharsets.ISO_8859_1, 100, Level.ERROR)); SimpleClientHttpRequestFactory chrf = new SimpleClientHttpRequestFactory(); chrf.setOutputStreaming(false); rt.getRestTemplate().setRequestFactory(new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(chrf), lInterceptors )); ResponseEntity<String> resp = rt.getForEntity(MockedControllers.TEST_URL_GET, String.class); assertThat(resp.getBody()).isEqualTo(MockedControllers.TEST_RETURN_VALUE); }
/** * Initialize the internal restTemplate instance */ @PostConstruct protected void init() { logger.debug("Create the RestTemplate instance that will be wrapped"); makeRestTemplateWithCustomObjectMapper(restTemplate); logger.debug("Set interceptor for authentication"); List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList(formLoginAuthenticationCsrfTokenInterceptor); restTemplate.setRequestFactory(new InterceptingClientHttpRequestFactory(restTemplate.getRequestFactory(), interceptors)); }
private void addAuthentication(RestTemplate restTemplate, String username, String password) { if (username == null) { return; } List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList( new BasicAuthorizationInterceptor(username, password)); restTemplate.setRequestFactory(new InterceptingClientHttpRequestFactory( restTemplate.getRequestFactory(), interceptors)); }
private void addAuthentication(String username, String password) { if (username == null) { return; } List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList( new BasicAuthorizationInterceptor(username, password)); setRequestFactory(new InterceptingClientHttpRequestFactory(getRequestFactory(), interceptors)); }
@Bean public ClientHttpRequestFactory clientHttpRequestFactory() { List<ClientHttpRequestInterceptor> interceptors = Arrays .asList(getSecurityInterceptor()); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy = this.properties.getRemote().getProxy(); if (proxy.getHost() != null && proxy.getPort() != null) { requestFactory.setProxy(new java.net.Proxy(Type.HTTP, new InetSocketAddress(proxy.getHost(), proxy.getPort()))); } return new InterceptingClientHttpRequestFactory(requestFactory, interceptors); }
public RestService(String url, String username, String password) { this.url = url; template = new RestTemplate(); List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); if (username != null) { interceptors.add(new BasicAuthenticationInterceptor(username, password)); } interceptors.add(new LoggingInterceptor()); template.setRequestFactory( new InterceptingClientHttpRequestFactory(template.getRequestFactory(), interceptors)); prismContext = ProxyCreator.getProxy(PrismContext.class, () -> { try { PrismContextFactory factory = new MidPointPrismContextFactory() { @Override protected void registerExtensionSchemas(SchemaRegistryImpl schemaRegistry) throws SchemaException, FileNotFoundException { super.registerExtensionSchemas(schemaRegistry); RestService.this.registerExtensionSchemas(schemaRegistry); } }; return factory.createPrismContext(); } catch (SchemaException | FileNotFoundException ex) { throw new NinjaException("Couldn't load prism context", ex); } }); }
private RamlRestTemplate(RamlChecker ramlChecker, boolean notSending, ReportStore reportStore, ClientHttpRequestFactory requestFactory) { this.ramlChecker = ramlChecker; this.notSending = notSending; this.reportStore = reportStore; this.originalRequestFactory = requestFactory; final RamlRequestInterceptor interceptor = new RamlRequestInterceptor(ramlChecker, notSending, reportStore); setRequestFactory(new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(requestFactory), Collections.<ClientHttpRequestInterceptor>singletonList(interceptor))); }
@Test public void authenticated() { assertThat(new TestRestTemplate("user", "password").getRestTemplate() .getRequestFactory()) .isInstanceOf(InterceptingClientHttpRequestFactory.class); }
@Test public void authenticated() { assertTrue(new TestRestTemplate("user", "password") .getRequestFactory() instanceof InterceptingClientHttpRequestFactory); }