Today I implemented a javax.ws.rs.client.ClientRequestFilter for a form based authentication. The FormAuthenticator class can be used in combination with a javax.ws.rs.client.Client to interact, for example, with a rest api secured by a login form. Such a login form in Java EE typically uses the request URI ‘/j_security_check‘ with the form input fields ‘j_username’ and ‘j_password‘. As a result of a successful login the browser stores a cookie named “JSESSIONID” which need to be send with every request.
The request filter can be added to a javax.ws.rs.Client like this:
.... // create a javax.ws.rs.client client = ClientBuilder.newClient(); // create new formAuthenticator FormAuthenticator formAuthFilter = new FormAuthenticator(rest_api_url, userid, password); // register the filter... client.register(formAuthFilter); // now you can GET, POST, .... ....
You cam find the source code of this filter class on GitHub.
If you have any ideas for improvements your comments are welcome!