2017-02-25 10 views
0

私はSpring RestTemplate.javaを含むspring-web-4.3.6.RELEASE.jarを使用しています。 私は4つのjunitテストケースを作成して、 "https"基本認証のrestfull Webサービスを呼び出しました。私はすでに、証明書をjava keystore cacertsファイルに登録することによって、 "https"問題を処理しました。今私は、私は、Java経由ではなく、あるsoapUIや郵便配達を通じてrestfull Webサービスを呼び出す「HTTP 401権限」エラーになっ:JUnitテストケース2(「testLocalCreateABCVersion2()」)で基本認証401快適なWeb​​サービスを呼び出す不正なエラー

を、私は、カスタムContextAwareHttpComponentsClientHttpRequestFactory.javaクラスを使用していました。私は底に提供しました。

Junitテストケース3( "testLocalCreateABCVersion3()")では、以下に示すカスタムBasicAuthRestTemplate.javaを使用しました。

Junitテストケース4(testLocalCreateABCVersion4())では、org.apache.commons.httpclient.HttpClientを使用し、client.getParams()。setAuthenticationPreemptive(true)をtrueに設定しました。このHttpClientsはcommons-httpClient-3.1.jarから来て、なぜ "setAuthenticationPreemptive"が存在するのですか? org.apache.httpcomponents.httpClient_4.5.3.jarにはsetAuthenticationPreemptiveメソッドが含まれていません。

何か助けやヒントがあれば幸いです!

    import java.util.ArrayList; 
        import java.util.LinkedHashMap; 
        import java.util.List; 

        import org.junit.Test; 
        import org.springframework.http.HttpEntity; 
        import org.springframework.http.HttpHeaders; 
        import org.springframework.http.HttpMethod; 
        import org.springframework.http.ResponseEntity; 
        import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 
        import org.springframework.web.bind.annotation.RequestMethod; 
        import org.springframework.web.client.RestTemplate; 

        import com.fasterxml.jackson.core.JsonGenerationException; 
        import com.fasterxml.jackson.databind.JsonMappingException; 
        import com.fasterxml.jackson.databind.ObjectMapper; 
        import com.journaldev.spring.controller.EmpRestURIConstants; 

        import com.abc.model.Employee; 
        import com.test.TestRequestPost; 
        import com.test.costcontrolservice.AbcResponse; 

        import static org.junit.Assert.*; 
        import org.apache.commons.codeabc.binary.Base64; 
        import org.apache.http.HttpHost; 
        import org.apache.commons.codec.binary.StringUtils; 
        import org.apache.http.HttpHost; 
        import org.apache.http.auth.AuthScope; 
        import org.apache.http.auth.UsernamePasswordCredentials; 
        import org.apache.http.auth.AuthScope; 
        import org.apache.http.client.HttpClient; 
        import org.apache.http.client.protocol.ClientContext; 
        import org.apache.http.client.protocol.HttpClientContext; 
        import org.apache.http.impl.auth.BasicScheme; 
        import org.apache.http.impl.client.BasicAuthCache; 
        import org.apache.http.impl.client.BasicCredentialsProvider; 
        import org.apache.http.impl.client.DefaultHttpClient; 
        import org.apache.http.impl.client.HttpClientBuilder; 
        import org.apache.http.osgi.services.HttpClientBuilderFactory; 
        import org.apache.http.protocol.BasicHttpContext; 

        import javax.net.ssl.*; 
        import java.io.*; 
        import java.security.KeyManagementException; 
        import java.security.KeyStore; 
        import java.security.MessageDigest; 
        import java.security.cert.CertificateException; 
        import java.security.cert.X509Certificate; 
        import org.springframework.http.HttpHeaders; 
        import org.apache.commons.codec.binary.Base64; 
        import org.apache.http.impl.client.HttpClientBuilder; 



        public class TestExample1 
        { 

         public static final String SERVER_URI_ABC_POST ="https://abc/rest/testservice/abc"; 

         @Test 
         public void testLocalCreateABCVersion1() 
         { 
         try 
          { 


          String plainCreds = "idAbc:Password-Test"; 
          byte[] plainCredsBytes = plainCreds.getBytes(); 
          byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); 
          String base64Creds = new String(base64CredsBytes); 
           HttpHeaders headers = new HttpHeaders(); 
           headers.add("Authorization", "Basic"+base64Creds); 
           HttpEntity<String> request = new HttpEntity<String>(headers); 

           RestTemplate restTemplate = new RestTemplate(); 

           TestRequestPost TestRequestPost = new TestRequestPost(); 



           //with basic authentication 
           ResponseEntity<AbcResponse> response = restTemplate.exchange(SERVER_URI_ABC_POST ,HttpMethod.POST,request, AbcResponse.class,TestRequestPost); 

           assertNotNull(response); 
          } 
         catch(Exception e) 
          { 
           System.out.println("e:"+e.getMessage()); 
          } 
         } 

         @Test 
         public void testLocalCreateABCVersion2() 
         { 
         try 
          { 


          DefaultHttpClient client = new DefaultHttpClient(); 
          String userId ="idAbc"; 
          String password = "Password-Test"; 

          BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 
          credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userId, password)); 
          client.setCredentialsProvider(credentialsProvider); 

          ContextAwareHttpComponentsClientHttpRequestFactory customFactory = new ContextAwareHttpComponentsClientHttpRequestFactory(client); 
          String host = "abc/rest/testservice/abc"; 
          int port = 443; 
          HttpHost targetHost = null; 
          targetHost = new HttpHost(host, port, "http"); 



          // Create AuthCache instance 
          BasicAuthCache 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); 


          String plainCreds = "idAbc:Password-Test"; 
          byte[] plainCredsBytes = plainCreds.getBytes(); 
          byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); 
          String base64Creds = new String(base64CredsBytes); 

          byte[] token = java.util.Base64.getEncoder().encode(plainCreds.getBytes()); 
          HttpHeaders headers = new HttpHeaders(); 
          headers.add("Authorization", "Basic"+new String(token)); 
          HttpEntity<String> request = new HttpEntity<String>(headers); 
          TestRequestPost TestRequestPost = new TestRequestPost(); 


          //without basic authentication 
          //AbcResponse response = restTemplate.postForObject(SERVER_URI_ABC_POST , TestRequestPost, AbcResponse.class); 

          //with basic authentication 
          ResponseEntity<AbcResponse> response = restTemplate.exchange(SERVER_URI_ABC_POST ,HttpMethod.POST,request, AbcResponse.class,TestRequestPost); 

          assertNotNull(response); 
          } 
         catch(Exception e) 
          { 
           System.out.println("e:"+e.getMessage()); 
          } 
         } 

         @Test 
         public void testLocalCreateABCVersion3() 
         { 
         try 
          { 


          String plainCreds = "idAbc:Password-Test"; 
          byte[] plainCredsBytes = plainCreds.getBytes(); 
          byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); 
          String base64Creds = new String(base64CredsBytes); 
          HttpHeaders headers = new HttpHeaders(); 
          headers.add("Authorization", "Basic"+base64Creds); 
          HttpEntity<String> request = new HttpEntity<String>(headers); 

          RestTemplate restTemplate = new BasicAuthRestTemplate("idAbc","Password-Test"); 
          TestRequestPost TestRequestPost = new TestRequestPost(); 

          //without basic authentication 
          // AbcResponse response = restTemplate.postForObject(SERVER_URI_ABC_POST , TestRequestPost, AbcResponse.class); 

          //with basic authentication 
          ResponseEntity<AbcResponse> response = restTemplate.exchange(SERVER_URI_ABC_POST ,HttpMethod.POST,request, AbcResponse.class,TestRequestPost); 

          assertNotNull(response); 
          } 
         catch(Exception e) 
          { 
           System.out.println("e:"+e.getMessage()); 
          } 
         } 

         @Test 
         public void testLocalCreateABCVersion4() 
         { 
         try 
          { 


          String plainCreds = "idAbc:Password-Test"; 
          byte[] plainCredsBytes = plainCreds.getBytes(); 
          byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); 
          String base64Creds = new String(base64CredsBytes); 
          HttpHeaders headers = new HttpHeaders(); 
          headers.add("Authorization", "Basic"+base64Creds); 

          org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient(); 
          client.getParams().setAuthenticationPreemptive(true); 
          UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("idAbc","Password-Test"); 
          client.getState().setCredentials(new AuthScope("abc",433,AuthScope.ANY_REALM),credentials); 
          CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client); 
          RestTemplate restTemplate = new RestTemplate(commons); 

          HttpEntity<String> request = new HttpEntity<String>(headers); 


          TestRequestPost TestRequestPost = new TestRequestPost(); 

          ResponseEntity<CostControlResponse> response = restTemplate.exchange(SERVER_URI_ABC_POST ,HttpMethod.POST,request, AbcResponse.class.class,TestRequestPost); 
          assertNotNull(response); 
          } 
         catch(Exception e) 
          { 
           System.out.println("e:"+e.getMessage()); 
          } 
         } 


        } 

ContextAwareHttpComponentsClientHttpRequestFactory.java:

  import java.net.URI; 



     import org.apache.http.client.HttpClient; 
     import org.apache.http.protocol.HttpContext; 
     import org.springframework.http.HttpMethod; 
     import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 
     //import org.apache.commons.httpclient.HttpClient; 
     public class ContextAwareHttpComponentsClientHttpRequestFactory extends 
     HttpComponentsClientHttpRequestFactory { 
     private HttpContext httpContext; 

     public ContextAwareHttpComponentsClientHttpRequestFactory(HttpClient httpClient){ 
     super(httpClient); 
     } 

     protected org.apache.http.protocol.HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { 
     //Ignoring the URI and method. 
     return httpContext; 
     } 

     public void setHttpContext(HttpContext httpContext) { 
     this.httpContext = httpContext; 
     } 
     } 

BasicAuthRestTemplate.java:

   import org.springframework.http.HttpRequest; 
      import org.springframework.http.client.ClientHttpRequestExecution; 
      import org.springframework.http.client.ClientHttpRequestInterceptor; 
      import org.springframework.http.client.ClientHttpResponse; 
      import org.springframework.http.client.InterceptingClientHttpRequestFactory; 
      import org.springframework.web.client.RestTemplate; 

      import java.io.IOException; 
      //import java.util.Base64; original 
      import java.util.Collections; 
      import java.util.List; 
      import org.apache.commons.codec.binary.Base64; //new 

      public class BasicAuthRestTemplate extends RestTemplate { 

       public BasicAuthRestTemplate(String username, String password) { 
        addAuthentication(username, password); 
       } 

       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)); 
       } 

       private static class BasicAuthorizationInterceptor implements 
         ClientHttpRequestInterceptor { 

        private final String username; 

        private final String password; 

        public BasicAuthorizationInterceptor(String username, String password) { 
         this.username = username; 
         this.password = (password == null ? "" : password); 
        } 

        @Override 
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, 
          ClientHttpRequestExecution execution) throws IOException 
        { 
         /* original code 
         byte[] token = Base64.getEncoder().encode(
           (this.username + ":" + this.password).getBytes()); 
         request.getHeaders().add("Authorization", "Basic " + new String(token)); 
         return execution.execute(request, body); 
         */ 
         String plainCreds = "idAbc:Password-Test"; 
        byte[] plainCredsBytes = plainCreds.getBytes(); 
        byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); 
        String base64Creds = new String(base64CredsBytes); 
        request.getHeaders().add("Authorization", "Basic " + new String(base64Creds)); 
        return execution.execute(request, body); 

        } 

       } 

      } 
+0

I、誰もが基本とRESTful Webサービスを呼び出すことができジャージまたはApache CXFやスプリングRestTemplateあるいは単なるApacheのClient.javaでrestfull RESTサービスクライアントへのリンクを持っているん春の休憩を使用する必要はありません認証。どんな助けやヒントもそれに感謝しています! – user1272936

答えて

0

リンクの下に、それはRestClient

http://howtodoinjava.com/spring/spring-restful/spring-restful-client-resttemplate-example/

で私を助けてみてください

ここに私がそれをする方法があります。 TODOをあなたのデータに置き換えてください。

1)Sprint RestTemplateから新しいインスタンスを作成してください。 Spring beanとしても設定できます

RestTemplate restTemplate = new RestTemplate();

2))HttpEntityエンティティ=新しいHttpEntity <>(TODO、addSecurityHeader())

protected MultiValueMap<String, String> addSecurityHeader() { 
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); 
    headers.add("Accept", MediaType.APPLICATION_JSON.toString()); 
    headers.add("Content-Type", MediaType.APPLICATION_JSON.toString()); 
    headers.add("Authorization", TODO_BasicAuthCode); 

    return headers; 
} 

3セキュリティヘッダを設定します。

ResponseEntity<String> response = restTemplate.exchange(getTicketUrl, HttpMethod.PUT, entity, String.class); 
+0

ありがとうございます。大変感謝しています!しかし、基本的な認証に関する内容はリンクには載っていません。私は春のRestTemplateを使ってRestFul Webサービスのためのvs postを渡す方法の例を見ています。 – user1272936

関連する問題