gpt4 book ai didi

java - Jersey客户端BASIC认证的Base64编码异常

转载 作者:行者123 更新时间:2023-11-30 10:25:01 25 4
gpt4 key购买 nike

我有一个在 Wildfly 9 上运行并启用了 BASIC 身份验证的 Jersey 客户端。这在一般情况下效果很好,但是当使用带有特殊字符(例如德语变音符号)的密码时,我会遇到与 Base64 编码相关的异常:

Caused by: java.lang.ArrayIndexOutOfBoundsException: 255
at org.glassfish.jersey.internal.util.Base64.encode(Base64.java:112)
at org.glassfish.jersey.internal.util.Base64.encodeAsString(Base64.java:160)
at org.glassfish.jersey.client.filter.HttpBasicAuthFilter.<init>(HttpBasicAuthFilter.java:98)
at org.glassfish.jersey.client.filter.HttpBasicAuthFilter.<init>(HttpBasicAuthFilter.java:72)

知道我可能做错了什么吗?

最佳答案

这是一个 known bug .

作为解决方法,您可以直接设置 Authorization header ,而不是使用 HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAMEHttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD 属性。

旧代码:

Invocation.Builder builder = …;
builder
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME, username)
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD, password);

新代码:

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.ws.rs.core.HttpHeaders;

// …
builder.header(HttpHeaders.AUTHORIZATION, calculateAuthentication(username, password));
// …

private String calculateAuthentication(final String username, final byte[] password) {
final byte[] prefix = (username + ":").getBytes(StandardCharsets.UTF_8);
final byte[] usernamePassword = new byte[prefix.length + password.length];
System.arraycopy(prefix, 0, usernamePassword, 0, prefix.length);
System.arraycopy(password, 0, usernamePassword, prefix.length, password.length);
return "Basic " + Base64.getEncoder().encodeToString(usernamePassword);
}

关于java - Jersey客户端BASIC认证的Base64编码异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433701/

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