Je fais une authentification simple HTTP et je obtenir unJava: chercher URL avec HTTPBasic authentification
java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...)
(...more password data...)
que je pense est dû me avoir un nom d'utilisateur très long et mot de passe et l'encodeur enveloppe avec un \n
à 76 caractères. Y a-t-il un moyen de contourner cela? L'URL ne prend en charge que l'authentification HTTP de base.
Voici mon code:
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;
}
Un avis de sécurité: si l'utilisation du nom d'utilisateur et mot de passe via HTTP, à la fois les choses sont envoyées en tant que chaîne claire. Il vaut mieux utiliser une authentification différente (formulaire, POST) et un protocole de transport (HTTPS, H2). – tfb785