2017-08-31 12 views
0

次のコードを使用してSpring RestアプリケーションでMicrosoft Flow POST URLを1つヒットしようとしていますが、401エラーが発生しています。 マイコード:承認ヘッダーで送信すると、Spring RestTemplateが401 Unauthorizedエラーを返します

@RequestMapping(value = "/hookslistner", method = RequestMethod.POST) 
    public ResponseEntity<Void> recieveWebhook(@RequestBody InventorySystemModel inventory, 
      @RequestHeader("event") String event, 
      @RequestHeader("Authorization") String authorization) { 
// authorization = "Basic <Base64 encoded value of username:pwd>" 

     RestTemplate restTemplate = new RestTemplate(); 

     org.springframework.http.HttpHeaders httpHeaders = new org.springframework.http.HttpHeaders(); 

     String url = "https://prod-01.centralindia.logic.azure.com/workflows/835348<hiding rest of part>";   
     String headerName = "Authorization"; 
     httpHeaders.add(headerName, authorization); 
     httpHeaders.add("Content-Type", "application/json"); 
     HttpEntity<String> requestEntity = new HttpEntity<>("Headers", httpHeaders); 
     System.out.println(">>>>>>>" + restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class).getBody()); 
    } 

エラー:私のターゲットURLがhttpsであると私はlocalhostはhttpので

SEVERE: Servlet.service() for servlet [webhooks] in context with path [/inventoryhooks] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 401 Unauthorized] with root cause 
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized 
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) 
. 
. 
. 

はそれですか?

誰かが間違っていると私に指摘できますか?あなたはこの問題をチェックするために

+0

その時点で例外がスローされますが、SSL restTemplateを取得するには、次のコードスニペットを参照することができますか? RestTemplateで、またはメソッドに入る前に? –

+0

'restTemplate.exchange' –

答えて

0

2点:

  1. ターゲットサーバーがHTTPサーバーである場合は、URLでHTTPSを使用しないでください。
  2. authorizationの値がbase64であることを確認してください。

更新:ターゲット・サーバーは、HTTPSサーバであるので

、問題はあなたがRestTempaltessl情報を設定していないということです。

@Configuration 
public class RestClientConfig { 
    @Bean 
    public RestOperations restOperations(ClientHttpRequestFactory clientHttpRequestFactory) throws Exception { 
     return new RestTemplate(clientHttpRequestFactory); 
    } 

    @Bean 
    public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) { 
     return new HttpComponentsClientHttpRequestFactory(httpClient); 
    } 

    @Bean 
    public HttpClient httpClient(@Value("${keystore.file}") String file, @Value("${keystore.pass}") String password) throws Exception { 
     KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     FileInputStream inStream = new FileInputStream(file); 
     try { 
      trustStore.load(inStream, password.toCharArray()); 
     } finally { 
      inStream.close(); 
     } 

     SSLContext sslcontext = 
       SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build(); 
     SSLConnectionSocketFactory sslsf = 
       new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1.2"}, null, 
               null); 
     return HttpClients.custom().setSSLSocketFactory(sslsf).build(); 
    } 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 
} 

テストケースを:

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes = RestClientConfig.class) 
public class RestClientTest { 

    @Autowired 
    private RestOperations rest; 

    private HttpHeaders getHeaders(){ 
     String plainCredentials="admin:admin"; 
     String base64Credentials = Base64.getEncoder().encodeToString(plainCredentials.getBytes()); 

     HttpHeaders headers = new HttpHeaders(); 
     headers.add("Authorization", "Basic " + base64Credentials); 
     return headers; 
    } 

    @Test 
    public void test() { 
     HttpEntity<String> request = new HttpEntity<String>(getHeaders()); 
     ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET, request, String.class); 
     System.out.println(response.getBody()); 
    } 
} 
+0

これはMicrosoft FlowのREST URLであり、httpsです。また、認可コードにbase64でエンコードされた値があります。同じ問題です... –

+0

今、私はこのエラーが発生しています。私はSSL操作またはトリック、助けてください! '重大度:[/ inventoryhooks]パスのコンテキスト内のサーブレット[webhooks]のServlet.service()が例外をスローしました[リクエスト処理に失敗しました。入れ子にされた例外はorg.springframework.web.client.ResourceAccessExceptionです: "https://prod-03.centralindia.logic.azure.com/workflows/a "のPOST要求のI/Oエラー:sun.security.validator。 ValidatorException:PKIXパス構築に失敗しました:sun.security.provider.certpath.SunCertPathBuilderException:要求されたターゲットへの有効な証明書パスを見つけることができませんでした。 –

関連する問題