我有在启用了BASIC身份验证的Wildfly 9上运行的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标头,而不是使用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);
}

07-28 13:36