title: securing rest web service with digest authentication java
body: <p>I'm developing a rest client, with apache httpclient 4.3.3, which supports HTTP Basic and Digest authentication. <strong>I need an example of rest webservice with digest authentication to test my client.</strong> Can anyone help me, even an online rest web service could be very appreciated.</p>
  
  <p>this is the client code for digest authentication:</p>
  
  <pre><code>final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
      final CredentialsProvider credsProvider = new BasicCredentialsProvider();
      credsProvider.setCredentials(AuthScope.ANY,
              new UsernamePasswordCredentials(user, password));
  
      // Create AuthCache instance
      final AuthCache authCache = new BasicAuthCache();
      // Generate DIGEST scheme object and add it to the local auth cache
      DigestScheme digestAuth = new DigestScheme();
      // Suppose we already know the realm name
      digestAuth.overrideParamter("realm", "some-realm");
      // Suppose we already know the expected nonce value
      digestAuth.overrideParamter("nonce", "some-nonce");
      authCache.put(targetHost, digestAuth);
  
      // Add AuthCache to the execution context
      HttpClientContext context = HttpClientContext.create();
      context.setAuthCache(authCache);
  </code></pre>
  
  <p>after that, I can call:</p>
  
  <pre><code>RestClient genericRestClient = new GenericRestClient.Builder(METHOD_URL)
              .setUser(DEFAULT_USER).setPassword(DEFAULT_PASS)
              .setAuthType(AuthenticationType.DIGEST_AUTH)
              .setHttpVersion(HTTPVersion.HTTP_1_1).build();
      genericRestClient.doGet();
  </code></pre>
  
  <p>I need to cosume a rest web service with digest authentication to test my client.</p>
  
  <p>I have begun with this web service:</p>
  
  <pre><code>@GET
  @Path("/get")
  @Produces("application/json")
  public Product getProduct(@Context HttpHeaders headers) {
      .....
  
      Product product = new Product();
      product.setName("Product 1");
      product.setQty(50);
  
      return product;
  </code></pre>
  
  <p>thank you for any help you can provide</p>