使用 Jersey Client 验证 Spring 安全 Web 应用程序时,我出了什么问题?

What was I wrong when using Jersey Client to authenticate an Spring Security web application?

我有一个受 Spring 安全登录表单身份验证保护的 Web 应用程序。现在我想使用 Jersey Client 对我的网页进行身份验证,我想我应该像在普通浏览器上一样通过登录表单。

我的客户端验证码如下

    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080/authentication-inmem/j_spring_security_check");
    Form form = new Form();
    form.param("j_username", "car");
    form.param("j_password", "scarvarez");

    Response response =  target.request()
    .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
    System.out.println(response.getStatus());

此代码始终生成 404 状态代码。 当我在浏览器中键入 link http://localhost:8080/authentication-inmem/j_spring_security_check 或将上面的代码修改为 GET 请求时。我通常可以收到 HTML 验证登录表单代码。因此,我真的不知道为什么 url 找不到 POST?

希望你能告诉我我这里的错误,而且,我正在做的是在不使用浏览器的情况下向我的服务器进行身份验证的正确方法?

我找到了答案,默认情况下,Jersey 客户端会在收到状态为 3xx 的响应后立即自动重定向。 (在本例中为 302)。 现在,只需提前一点配置关闭该功能,我就可以在我的控制台上显示状态 302。

ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.FOLLOW_REDIRECTS, false);
Client client = ClientBuilder.newClient(clientConfig);