Search code examples
jenkinsjerseyjax-rsjersey-client

Jersey Client: Authentication fails at redirect by Jenkins


I am attempting to use the REST api of Jenkins. Jenkins requires a POST request to a URL to delete a job. This results in the following:

  1. I tell my chosen Client to send a POST to the appropriate URL.
    The client sends a POST and authorizes itself with username and password.
  2. Jenkins deletes the job.
  3. Jenkins returns a "302 - Found" with the location of folder containing the deleted job.
  4. Client automatically sends a POST to the location.
  5. Jenkins answers with "200 - OK" and the full HTML of the folder page.

This works just fine with Postman (unless I disable "Automatically follow redirects" of course). Jersey however keeps running into a "404" at step 5 because I blocked anonymous users from viewing the folder in question. (Or a "403" if I blocked anonymous users altogether.) Note that the authentication works in step 1 because the job has been deleted successfully!

I was under the impression that Jersey should use the given authentication for all requests concerning the client. Is there a way to actually make this true? I really don't want to forbid redirects just to do every single redirect myself.

To clarify: The problem is that while Jersey follows the redirect, but fails to authenticate itself again, leading to the server rejecting the second request.

Code in question:

HttpAuthenticationFeature auth = HttpAuthenticationFeature.basicBuilder()
    .credentials(username, token)
    .build();
Client client = ClientBuilder.newBuilder()
    .register(auth)
    .build();
WebTarget deleteTarget = client.target("http://[Jenkins-IP]/job/RestTestingArea/job/testJob/doDelete")  
Response response = deleteTarget.request()
    .post(null);

EDIT: The "302-Found" only has 5 headers according to Postman: Date, X-Content-Type-Options ("nosniff"), Location, Content-Length (0) and Server. So neither any cookies nor any tokens that Postman might use and Jersey disregard.

Question loosely related to this one - if I were able to log the second request I might be able to understand what's happening behind the scenes.

EDIT2: I have also determined that the problem is clearly with the authentication. If I allow anonymous users to view the folder in question, the error disappears and the server answers with a 200.


Solution

  • I found the answer with the help of Paul Samsotha and Gautham.

    TL;DR: This is intended behavior and you have to set the System property http.strictPostRedirect=true to make it work or perform the second request yourself.


    As also described here, HttpURLConnection decided to not implement a redirect as it is defined in the HTTP standard but instead as many browsers implemented it (so in laymans terms, "Do it like everyone else instead of how it is supposed to work"). This leads to the following behavior:

    1. Send POST to URL_1.
    2. Server answers with a "302 - Found" and includes URL_2.
    3. Send GET to URL_2, dropping all the headers.
    4. Server answers with a "404 - Not Found" as the second request does not included correct authentication headers.
    5. The "404" response is the one received by the code, as steps 2 and 3 are "hidden" by the underlying code.

    By dropping all headers, the authentication fails. As Jersey uses this class by default, this lead to the behavior I was experiencing.