Announcement Announcement Module
Collapse
No announcement yet.
Preemptive Basic Authentication with RestTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Preemptive Basic Authentication with RestTemplate

    I cannot get preeemptive Basic Auth to work with RestTemplate. Always see 2 requests, first 401 Unauthorized, followed by a good one. HttpClient has deprecated the setAuthenticationPreemptive().

    Does someone have an example of how to implement with RestTemplate?

  • #2
    Preemptive behavior with RestTemplate

    Same problem here.
    This is how I'm configuring the RestTemplate for BasicAuth using Spring 3.1

    Code:
    @Configuration
    public class WSClientConfiguration {	
    	@Value("${userId}")
    	private String userId;
    	@Value("${password:secure}")
    	private String password;
    	@Bean
    	public RestTemplate restTemplate(){		
    		DefaultHttpClient httpClient = new DefaultHttpClient();
    		BasicCredentialsProvider credentialsProvider =  new BasicCredentialsProvider();
    		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userId, password));
    		httpClient.setCredentialsProvider(credentialsProvider);
    		HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    		RestTemplate restTemplate =  new RestTemplate(httpComponentsClientHttpRequestFactory);
    		return restTemplate;
    	}
    ...
    However, looking here (http://hc.apache.org/httpcomponents-...ntication.html), I see that
    in 4.1, "preemptivity" is now handled via setting up a local context and passing it in with the request.
    When I create the RestTemplate, I don't have a request as I'm just setting up the template for ALL future requests.

    So... how can I get the behavior of preemptive=true when configuring a RestTemplate?

    Thanks!

    Comment


    • #3
      Got preemptivity to work with RestTemplate

      Hi,
      I found the solution posted here:
      https://jira.springsource.org/browse/SPR-8367

      Arjen P, added a createHttpContext on the HttpComponentsClientHttpRequestFactory.
      So I created a custom factory to implement the createHttpContext method like so:
      Code:
      public class ContextAwareHttpComponentsClientHttpRequestFactory extends
      		HttpComponentsClientHttpRequestFactory {
      	private HttpContext httpContext;
      	
      	public ContextAwareHttpComponentsClientHttpRequestFactory(HttpClient httpClient){
      		super(httpClient);
      	}
      	
          protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
          	//Ignoring the URI and method.
              return httpContext;
          }
      
      	public void setHttpContext(HttpContext httpContext) {
      		this.httpContext = httpContext;
      	}
      }
      Then I created the context like so:
      Code:
      ...
      		ContextAwareHttpComponentsClientHttpRequestFactory customFactory = new ContextAwareHttpComponentsClientHttpRequestFactory(httpClient);
      		
      		HttpHost targetHost = null;
      		if (StringUtils.isEmpty(port)){
      			targetHost = new HttpHost(host); 
      		} else {
      			targetHost = new HttpHost(host, Integer.parseInt(port), "http");
      		}
      		
      
      		// Create AuthCache instance
      		AuthCache authCache = new BasicAuthCache();
      		// Generate BASIC scheme object and add it to the local auth cache
      		BasicScheme basicAuth = new BasicScheme();
      		authCache.put(targetHost, basicAuth);
      
      		// Add AuthCache to the execution context
      		BasicHttpContext localContext = new BasicHttpContext();
      		localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);    
      		
      		customFactory.setHttpContext(localContext);
      		RestTemplate restTemplate =  new RestTemplate(customFactory);
      ...
      Hope this helps someone!

      Comment


      • #4
        Thanks spongybob. That solution works great!

        Comment


        • #5
          Thanks and its been helpful

          Comment

          Working...
          X