英文:
How to set Basic Auth (Usernama and password) using java code
问题
目前我正在进行以下设置:
connection.setRequestProperty("Authorization", "Basic NDUwNjMyOTc6U2hyaXNo2weQDEy");
Basic NDUwNjMyOTc6U2hyaXNo2weQDEy
——我用 Postman 生成了这部分内容,但其中包含了用户名和密码。
如何使用 Java 生成这个 Auth Basic NDUwNjMyOTc6U2hyaXNo2weQDEy
,并在 setRequestProperty
中传递它。
提前谢谢
英文:
currently i am setting as below:
connection.setRequestProperty("Authorization", "Basic NDUwNjMyOTc6U2hyaXNo2weQDEy");
Basic NDUwNjMyOTc6U2hyaXNo2weQDEy"----------
This part i have generated using postman. but it contains user
name and password
.
How to generate this Auth Basic NDUwNjMyOTc6U2hyaXNo2weQDEy
using java and pass it in setRequestProperty
.
Thanks in advance
答案1
得分: 3
基本身份验证头部是通过使用 base64 编码的 username:password
生成的,
头部授权值可以按以下方式生成:
Encoder encoder = Base64.getEncoder();
String originalString = username + ":" + password;
String encodedString = encoder.encodeToString(originalString.getBytes());
String headerAuthorization = "Basic " + encodedString;
英文:
The basic auth header is generated from username:password
using base 64,
the header authorization value could be generated like so:
Encoder encoder = Base64.getEncoder();
String originalString = username+":"+password;
String encodedString = encoder.encodeToString(originalString.getBytes());
String headerAthorization="Basic "+encodedString;
答案2
得分: 0
你添加头部 Basic ENCODED
,其中编码是 "user:password"
的base64编码。
参考示例:https://github.com/jcabi/jcabi-http/blob/master/src/main/java/com/jcabi/http/wire/BasicAuthWire.java#L123。
英文:
You add header Basic ENCODED
, where the encoded is base64 of "user:password"
.
See https://github.com/jcabi/jcabi-http/blob/master/src/main/java/com/jcabi/http/wire/BasicAuthWire.java#L123 for example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论