gpt4 book ai didi

java - Jersey 安全文摘与基本身份验证

转载 作者:行者123 更新时间:2023-12-02 06:08:02 25 4
gpt4 key购买 nike

任何人都可以向我解释为什么基本身份验证有效,而摘要不起作用或无论如何都没有显示在服务器上的 http header 中。

public String login(UserDTO user)
{
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
// client.addFilter(new HTTPBasicAuthFilter(user.getUsername(), user.getPassword()));
client.addFilter(new HTTPDigestAuthFilter(user.getUsername(), user.getPassword()));
ClientResponse response = client.resource(url + "user/login").accept("application*json").type("application/json").get(ClientResponse.class);

System.out.println(response.toString());

return null;
}

如果我使用:

client.addFilter(new HTTPBasicAuthFilter(user.getUsername(), user.getPassword()));

我在服务器上获得授权 header :

USER LOGIN REQUEST
request:uri: /StambomenWebAPI/rest/user/login
method: GET
QueryString: null
Parameters:
Headers:
Name: accept Value: application*json
Name: content-type Value: application/json
Name: authorization Value: Basic QXhsOkxvbA==
Name: user-agent Value: Java/1.7.0_51
Name: host Value: localhost:8084
Name: connection Value: keep-alive
USER AND PASS[XXXXX, XXXXX]

但是当我使用

 client.addFilter(new HTTPDigestAuthFilter(user.getUsername(), user.getPassword()));

我没有获得授权 header 字段...:s ?

在 tomcat v7 中使用 jersey

谨此致意,并提前感谢您的帮助

最佳答案

由于摘要身份验证工作流程,您没有获得授权 header 字段。请参阅here了解更多详细信息,但基本上:

  1. 客户端发出不带 Authorization header 的请求
  2. 服务器响应 401 状态和 WWW-Authenticate header ,如下所示:

    Digest realm="testrealm@host.com",
    qop="auth,auth-int",
    nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
    opaque="5ccc069c403ebaf9f0171e9517f40e41"
  3. 既然客户端已从服务器获取了摘要信息,则使用正确的 Authorization header 重复请求

从客户端来看,这一切都由 Jersey HTTPDigestAuthFilter 处理。因此,过滤器首先发出不带 Authorization header 的请求,并且您的服务器应返回 401 状态,并带有包含必要摘要信息的 WWW-Authenticate header 。然后,过滤器使用正确的 Authorization header 重复请求,您的服务器应该进行身份验证并返回内容。

在此初始握手之后,HTTPDigestAuthFilter 会记住必要的摘要信息,因此对于第一个请求之后的所有请求,将包含 Authorization header 。

关于java - Jersey 安全文摘与基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22117512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com