Search in sources :

Example 1 with UserAgentRequestInterceptor

use of com.okta.spring.boot.oauth.http.UserAgentRequestInterceptor in project okta-spring-boot by okta.

the class OktaOAuth2ResourceServerAutoConfig method restTemplate.

static RestTemplate restTemplate(OktaOAuth2Properties oktaOAuth2Properties) {
    Proxy proxy;
    OktaOAuth2Properties.Proxy proxyProperties = oktaOAuth2Properties.getProxy();
    Optional<BasicAuthenticationInterceptor> basicAuthenticationInterceptor = Optional.empty();
    if (proxyProperties != null && Strings.hasText(proxyProperties.getHost()) && proxyProperties.getPort() > 0) {
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyProperties.getHost(), proxyProperties.getPort()));
        if (Strings.hasText(proxyProperties.getUsername()) && Strings.hasText(proxyProperties.getPassword())) {
            basicAuthenticationInterceptor = Optional.of(new BasicAuthenticationInterceptor(proxyProperties.getUsername(), proxyProperties.getPassword()));
        }
    } else {
        proxy = Proxy.NO_PROXY;
    }
    RestTemplate restTemplate = new RestTemplate(Arrays.asList(new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter(), new StringHttpMessageConverter()));
    restTemplate.getInterceptors().add(new UserAgentRequestInterceptor());
    basicAuthenticationInterceptor.ifPresent(restTemplate.getInterceptors()::add);
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setProxy(proxy);
    restTemplate.setRequestFactory(requestFactory);
    return restTemplate;
}
Also used : FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) Proxy(java.net.Proxy) UserAgentRequestInterceptor(com.okta.spring.boot.oauth.http.UserAgentRequestInterceptor) InetSocketAddress(java.net.InetSocketAddress) RestTemplate(org.springframework.web.client.RestTemplate) BasicAuthenticationInterceptor(org.springframework.http.client.support.BasicAuthenticationInterceptor) OAuth2AccessTokenResponseHttpMessageConverter(org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter) OktaOAuth2Properties(com.okta.spring.boot.oauth.config.OktaOAuth2Properties) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 2 with UserAgentRequestInterceptor

use of com.okta.spring.boot.oauth.http.UserAgentRequestInterceptor in project okta-spring-boot by okta.

the class OktaOAuth2UserService method restOperations.

private RestOperations restOperations() {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
    restTemplate.getInterceptors().add(new UserAgentRequestInterceptor());
    return restTemplate;
}
Also used : OAuth2ErrorResponseErrorHandler(org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler) UserAgentRequestInterceptor(com.okta.spring.boot.oauth.http.UserAgentRequestInterceptor) RestTemplate(org.springframework.web.client.RestTemplate)

Aggregations

UserAgentRequestInterceptor (com.okta.spring.boot.oauth.http.UserAgentRequestInterceptor)2 RestTemplate (org.springframework.web.client.RestTemplate)2 OktaOAuth2Properties (com.okta.spring.boot.oauth.config.OktaOAuth2Properties)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)1 BasicAuthenticationInterceptor (org.springframework.http.client.support.BasicAuthenticationInterceptor)1 FormHttpMessageConverter (org.springframework.http.converter.FormHttpMessageConverter)1 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)1 OAuth2ErrorResponseErrorHandler (org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler)1 OAuth2AccessTokenResponseHttpMessageConverter (org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter)1