title: How can you authenticate using the Jersey Client against a JAAS enabled web-server?
body: <p>I have the following scenario:</p>
  
  <p>Server: Jetty (with configured JAAS)</p>
  
  <p>Client: Jersey invoked via JUnit (via Maven)</p>
  
  <p>I have JAAS set up in the web server. I am using the client part as a test.</p>
  
  <p>On the server side users are authenticated through a form with Basic authentication handled via JAAS. Obviously, users need to be authenticated before being able to view certain pages.</p>
  
  <p>I would like to be able to login via the Jersey before trying to access a secured page. How can this be done? I have checked that you can define a filter, but I'm not quite sure how to use that. And -- once the user is logged in via the form, how can I proceed (from the client-side) to the page I'm actually interested in?</p>
  
  <p>I would really appreciate it, if somebody could show me an example how this is done on the client side with Jersey.</p>
  
  <p>I have the following JUnit test case method:</p>
  
  <pre><code>@Test
  public void testLogin()
          throws IOException
  {
      String URL_LOGIN = "http://localhost:9080/foo/auth.html";
      Client client = Client.create();
  
      String username = "me";
      String password = "me";
  
      final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(username, password);
      client.addFilter(authFilter);
      client.addFilter(new LoggingFilter());
  
      WebResource webResource = client.resource(URL_LOGIN);
      // I even tried:
      // webResource.header("Authorization", "Basic " + "base64encoded_userid:password").type("application/xml");
  
      String page = webResource.post(String.class);
  
      System.out.println(page);
  }
  </code></pre>
  
  <p>Please, note:</p>
  
  <p>1) <a href="http://localhost:9080/foo/auth.html">http://localhost:9080/foo/auth.html</a> is the page I am supposed to be seeing upon successful auth.</p>
  
  <p>2) I am actually seeing the output of <a href="http://localhost:9080/foo/login.html">http://localhost:9080/foo/login.html</a>.</p>
  
  <p>3) Obviously, through a browser, I can successfully login via the login.html page.</p>
  
  <p>What do I seem to be missing out here?</p>