robię pewne proste uwierzytelnianie HTTP i otrzymujęJava: pobrać URL z HTTPBasic Authentication
java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...)
(...more password data...)
co moim zdaniem jest spowodowane mi o bardzo długiej nazwy użytkownika i hasła, a enkoder owija go z \n
na 76 znaków. Czy jest jakiś sposób, żeby to obejść? Adres URL obsługuje tylko podstawowe uwierzytelnianie HTTP.
Oto mój kod:
private class UserPassAuthenticator extends Authenticator {
String user;
String pass;
public UserPassAuthenticator(String user, String pass) {
this.user = user;
this.pass = pass;
}
// This method is called when a password-protected URL is accessed
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass.toCharArray());
}
}
private String fetch(StoreAccount account, String path) throws IOException {
Authenticator.setDefault(new UserPassAuthenticator(account.getCredentials().getLogin(), account.getCredentials().getPassword()));
URL url = new URL("https", account.getStoreUrl().replace("http://", ""), path);
System.out.println(url);
URLConnection urlConn = url.openConnection();
Object o = urlConn.getContent();
if (!(o instanceof String))
throw new IOException("Wrong Content-Type on " + url.toString());
// Remove the authenticator back to the default
Authenticator.setDefault(null);
return (String) o;
}
Zawiadomienie bezpieczeństwa: jeśli nazwa wykorzystanie użytkownika i hasło za pośrednictwem protokołu HTTP, zarówno rzeczy są wysyłane jako czysty ciąg. Lepiej używać różnych uwierzytelnień (formularz, POST) i protokołu transportowego (HTTPS, H2). – tfb785