Java 网络调用时候出现auth校验的问题

上次Java调用节点遇到auth   401 没有权限  Java代码当中提示流无法打开
可以使用如下方法进行调用:获取到用户名密码   加上auth权限校验

CloseableHttpAsyncClient httpClient = null;
URL _url = new URL("http://ABBCCoinrpc:admin@192.168.12.22:27472");
RequestConfig requestConfig = RequestConfig.custom()
      .setSocketTimeout(3000)
      .setConnectTimeout(3000).build();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
String[] userInfoArray =_url.getUserInfo().split(":");
String userName = userInfoArray[0];
String password = userInfoArray[1];

System.out.println(userName+"===="+password);
credsProvider.setCredentials(
      new AuthScope(_url.getHost(), _url.getPort()),
      new UsernamePasswordCredentials(userName, password));
httpClient = HttpAsyncClients.custom()
      .setDefaultRequestConfig(requestConfig)
      .setDefaultCredentialsProvider(credsProvider)
      .build();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以通过HttpURLConnection来发送带有Digest Auth的SSE请求。首先需要创建一个HttpURLConnection对象,并设置请求的URL、请求方法、以及请求中的Authorization字段。Authorization字段需要包含Digest Auth的凭证信息,可以使用JavaMessageDigest类来计算请求消息的摘要值,并将摘要值与用户名、密码等信息一起编码成Base64字符串,然后放入Authorization字段中。以下是示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class SSEWithDigestAuth { public static void main(String[] args) throws IOException, NoSuchAlgorithmException { String username = "your_username"; String password = "your_password"; String url = "your_sse_url"; HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("GET"); // Calculate digest MessageDigest md = MessageDigest.getInstance("MD5"); String ha1 = username + ":" + "realm" + ":" + password; byte[] ha1Bytes = md.digest(ha1.getBytes()); String ha2 = "GET" + ":" + url; byte[] ha2Bytes = md.digest(ha2.getBytes()); String response = ha1Bytes.toString() + ":" + "nonce" + ":" + "00000001" + ":" + "cnonce" + ":" + "qop" + ":" + ha2Bytes.toString(); byte[] responseBytes = md.digest(response.getBytes()); String digest = Base64.getEncoder().encodeToString(responseBytes); // Set Authorization header String authHeader = "Digest username=\"" + username + "\", realm=\"realm\", nonce=\"nonce\", uri=\"" + url + "\", qop=auth, nc=00000001, cnonce=\"cnonce\", response=\"" + digest + "\""; connection.setRequestProperty("Authorization", authHeader); // Send request BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 在上面的示例代码中,我们使用了JavaMessageDigest类来计算请求消息的md5摘要值,然后将摘要值与用户名、密码等信息编码成Base64字符串,并放入Authorization字段中。注意,这里只是一个简单的示例,实际使用中需要根据具体的情况进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值