How can you authenticate using the Jersey Client against a JAAS enabled web-server?

前端 未结 5 954
攒了一身酷
攒了一身酷 2020-12-28 09:39

I have the following scenario:

Server: Jetty (with configured JAAS)

Client: Jersey invoked via JUnit (via Maven)

I have JAAS set up in the web server

5条回答
  •  感情败类
    2020-12-28 10:06

    With Basic auth you don't need to go to any login page at all. If the server is configured to use Basic auth, then you can make requests to any protected page if you include the basic auth header in your requests. The Jersey filter takes care of that. So, if Basic auth would really be what the server is using, then your code should work.

    Given it does not work and the way it does not work I am pretty sure the server is configured to use form-based authentication instead of the basic authentication.

    For the form-based authentication to work, you'll have to send a post request with form data including user name and password to log in and then set cookies you receive from the server to your subsequent requests (since the server - once you log in - will set the session cookie).

    Look at how the login.html looks like - it should contain a form. If it is using the standard servlet form auth., the action URL of that form should be "j_security_check" and there should be two form parameters: j_username and j_password. If that is the case, you can try something like the following:

    String URL_LOGIN = "http://localhost:9080/foo/j_security_check";
    String URL_DATA = "http://localhost:9080/foo/auth.html";
    Client client = Client.create();
    
    // add a filter to set cookies received from the server and to check if login has been triggered
    client.addFilter(new ClientFilter() {
        private ArrayList cookies;
    
        @Override
        public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
            if (cookies != null) {
                request.getHeaders().put("Cookie", cookies);
            }
            ClientResponse response = getNext().handle(request);
            // copy cookies
            if (response.getCookies() != null) {
                if (cookies == null) {
                    cookies = new ArrayList();
                }
                // A simple addAll just for illustration (should probably check for duplicates and expired cookies)
                cookies.addAll(response.getCookies());
            }
            return response;
        }
    });
    
    String username = "me";
    String password = "me";
    
    // Login:
    WebResource webResource = client.resource(URL_LOGIN);
    
    com.sun.jersey.api.representation.Form form = new Form();
    form.putSingle("j_username", username);
    form.putSingle("j_password", password);
    webResource.type("application/x-www-form-urlencoded").post(form);
    
    // Get the protected web page:
    webResource = client.resource(URL_DATA);
    String response = webResource.get(String.class);
    
    
    

    I haven't tested this, so maybe there will be some typos or bugs.

    提交回复
    热议问题