我正在使用 Spring 构建 Web。我想对字符串进行编码以作为基本身份验证发送。
这是代码,
private String authUsername = "admin";
private String authPassword = "admin";
private String unEncode = authUsername+authPassword;
private byte[] encodedBytes = Base64.encodeBase64(unEncode.getBytes());
private String encode = new String(encodedBytes);
private String authenCode = "Basic "+encode;
结果是“Basic YWRtaW5hZG1pbg==”,这是错误的。
正确的输出应该是“Basic YWRtaW46YWRtaW4=”
我做错了什么或错过了什么?
谢谢。
最佳答案
问题不在于 Base64 编码,而在于构建字符串。基本身份验证需要 username:password
但您缺少 :
。
关于java - JAVA 中的 EncodeBase64 返回错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605050/