Avere un server Jetty impostato a livello di codice che cerco di accedere tramite ajax e xmlHttpRequest. Senza autorizzazione, la chiamata funziona bene ma con, ottengo 401 non autorizzato. Qualsiasi suggerimento.Jetty, preflight e ajax
JavaScript chiamata simile a questo (ridotto):
var auth = base64encode('name','pwd');
try{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://127.0.0.1:5563/ajax/index.html", true);
xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xmlhttp.setRequestHeader('Authorization', auth);
xmlhttp.withCredentials = 'true';
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
$('#textResult').val(xmlDoc);
}
catch(e){
$('#textResult').val('CATCH: ' + e);
}
codice Server simile a questo (ancora più breve)
class CallObject extends HttpServlet {
//...
@Override
public void doOptions(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods",
"GET, POST, HEAD, OPTIONS");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Headers",
"X-Requested-With, authorization");
}
//...
}
class WebServer{
//...
SecurityHandler sh = null;
if (logins != null && logins.length > 0){
String role = "user";
sh = new SecurityHandler();
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[]{role});
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
HashUserRealm hur = new HashUserRealm();
hur.setName("eMark Web Server");
for (int i = 0; i < logins.length; i++) {
String user_name = logins[i][0];
String password = logins[i][1];
hur.put(user_name, password);
hur.addUserToRole(user_name, role);
}
sh.setUserRealm(hur);
sh.setConstraintMappings(new ConstraintMapping[]{cm});
_server.setHandlers(
new Handler[]{sh, _contexts, new DefaultHandler()});
}
//...
}
Immagino che questo ti sarà utile [1]. (Http://docs.codehaus.org/display/JETTY/How+to+Configure+Security+with+Embedded+Jetty). Posso anche vedere che le credenziali che stai inserendo da javascript sono "var auth = base64encode ('name', 'pwd');". Ciò significa che le credenziali per "nome" sono "pwd" e dovrebbero essere presenti al molo con ruolo rilevante – nibin012
Perché si imposta direttamente l'intestazione della richiesta "Autorizzazione" sul codice cliente? Perché non utilizzare il supporto XmlHttpRequest per l'autenticazione nel metodo open()? Inoltre, non sono sicuro se la tua funzione base64encode formatta la variabile auth come "nome: pwd". –
Sembra che tu stia facendo una richiesta asincrona, ma stai provando a leggere la risposta in modo sincrono? È solo una semplificazione che hai fatto quando hai postato questa domanda? In caso contrario, sarà necessario assegnare una funzione di callback alla proprietà 'onreadystatechange' dell'oggetto' xmlhttp'. –