Livy REST API:GET 请求有效,但 POST 请求失败并显示“需要 401 身份验证”
Livy REST API: GET requests work but POST requests fail with '401 Authentication required'
我在 https://github.com/apache/incubator-livy/blob/master/docs/rest-api.md 为 Livy 的 REST API 的部分编写了一个 Java 客户端。客户端使用Spring的RestTemplate.getForObject()
和postForObject()
分别发出GET和POST请求。 Livy 服务器使用 Kerberos 进行保护。
GET /sessions
and GET /batches
requests work fine: I get the expected responses from Livy. But both POST /sessions
and POST /batches
请求失败:
org.springframework.web.client.HttpClientErrorException: 401 Authentication required
有谁知道为什么 GET 请求成功时 POST 请求失败?我的代码没有对身份验证做任何明确的事情。
我已经尝试通过 Kerberos 以多个不同的用户身份进行身份验证,但我总是遇到这个问题。 Livy 是否需要额外配置以允许来自特定用户的 POST 请求(因为 POST 请求有效地创建交互式会话或向 Spark 提交作业)?
事实证明,虽然常规 org.springframework.web.client.RestTemplate
class is sufficient for GET requests, you need to use org.springframework.security.kerberos.client.KerberosRestTemplate
for POST requests. You may also need to add an extra header to POST requests if the Livy server has CSRF (cross-site request forgery) protection enabled as described here.
GET /batches 示例
RestTemplate restTemplate = new RestTemplate();
GetBatchesResponse response2 = restTemplate.getForObject("http://your_livy_server:8998" + "/batches", GetBatchesResponse.class);
其中 GetBatchesResponse
是我编写的一个简单的 POJO,代表 response body 到 GET /batches
。
POST /批次示例
PostBatchesRequest postRequestBody = new PostBatchesRequest();
postRequestBody.setFile("/path/to/your/application"); // In HDFS
KerberosRestTemplate kerberosRestTemplate = new KerberosRestTemplate("path_to_your_key_tab_file", "your_user@your_realm");
// Add CSRF header if required:
HttpHeaders headers = new HttpHeaders();
headers.set("X-Requested-By", "your_user@your_realm");
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<PostBatchesRequest> postRequest = new HttpEntity<PostBatchesRequest>(postRequestBody, headers);
Batch batch = kerberosRestTemplate.postForObject("http://your_livy_server:8998" + "/batches", postRequest, Batch.class);
其中 PostBatchesRequest
和 Batch
是我编写的 POJO,分别表示 request body and response。
我在 https://github.com/apache/incubator-livy/blob/master/docs/rest-api.md 为 Livy 的 REST API 的部分编写了一个 Java 客户端。客户端使用Spring的RestTemplate.getForObject()
和postForObject()
分别发出GET和POST请求。 Livy 服务器使用 Kerberos 进行保护。
GET /sessions
and GET /batches
requests work fine: I get the expected responses from Livy. But both POST /sessions
and POST /batches
请求失败:
org.springframework.web.client.HttpClientErrorException: 401 Authentication required
有谁知道为什么 GET 请求成功时 POST 请求失败?我的代码没有对身份验证做任何明确的事情。
我已经尝试通过 Kerberos 以多个不同的用户身份进行身份验证,但我总是遇到这个问题。 Livy 是否需要额外配置以允许来自特定用户的 POST 请求(因为 POST 请求有效地创建交互式会话或向 Spark 提交作业)?
事实证明,虽然常规 org.springframework.web.client.RestTemplate
class is sufficient for GET requests, you need to use org.springframework.security.kerberos.client.KerberosRestTemplate
for POST requests. You may also need to add an extra header to POST requests if the Livy server has CSRF (cross-site request forgery) protection enabled as described here.
GET /batches 示例
RestTemplate restTemplate = new RestTemplate();
GetBatchesResponse response2 = restTemplate.getForObject("http://your_livy_server:8998" + "/batches", GetBatchesResponse.class);
其中 GetBatchesResponse
是我编写的一个简单的 POJO,代表 response body 到 GET /batches
。
POST /批次示例
PostBatchesRequest postRequestBody = new PostBatchesRequest();
postRequestBody.setFile("/path/to/your/application"); // In HDFS
KerberosRestTemplate kerberosRestTemplate = new KerberosRestTemplate("path_to_your_key_tab_file", "your_user@your_realm");
// Add CSRF header if required:
HttpHeaders headers = new HttpHeaders();
headers.set("X-Requested-By", "your_user@your_realm");
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<PostBatchesRequest> postRequest = new HttpEntity<PostBatchesRequest>(postRequestBody, headers);
Batch batch = kerberosRestTemplate.postForObject("http://your_livy_server:8998" + "/batches", postRequest, Batch.class);
其中 PostBatchesRequest
和 Batch
是我编写的 POJO,分别表示 request body and response。