Search code examples
javascriptajaxauthenticationcorsalfresco

Request alfresco authentication token with ajax POST/GET call (Angular)


Here is the problem,

I try to get an authentication token from my alfresco community server with a js (angular 4) script. The request seems to be blocked by "CORS header missing"...

The login/password are ok since I can try the login URL directly in my browser and I get the expected result (xml) like this :

https://www.my-url.com/alfresco/service/api/login?u=username&pw=password

I get this as a result :

enter image description here

Now, I come to my web app and I need to retrieve this ticket from an http call, like this (with angular 4 service) :

getTicket(user: User): Observable<boolean>{
    let headers = new Headers();
    headers.append('Access-Control-Allow-Origin','*');
    return this.http.post('https://www.my-url.com/alfresco/service/api/login',{'u':'username','pw':'password'},{headers:headers}).map(response => {
        console.log('response : '+JSON.stringify(response));
    });
}

I hardcoded the u/pw variables, I also tried to name them "user" & "password", and I tried GET method too, but nothing changes, the only thing I get back is a Cors problem. Result in console :

OPTIONS XHR https://www.my-url.com/alfresco/service/api/login [HTTP/1.1 200 OK 39 ms]

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource ... Reason: CORS header 'Access-Control-Allow-Origin' missing.

Request data :

Response Headers : 
Allow: "OPTIONS, GET, POST"
Content-Length: "0"
Date : "Wed, 11 Oct 2017 14:37:45 GMT"
Server: "Apache-Coyote/1.1"

Request Headers : 
Host : "www.my-url.com"
User-Agent : "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
Accept-Language : "en-US,en;q=0.5"
Accept-Encoding: "gzip, deflate"
Access-Control-Request-Method: "POST"
Access-Control-Request-Headers: "access-control-allow-origin"
Origin: "http://localhost"
Connection: "keep-alive"

I also tried removing the headers I manually create for the request, but no effect...


Solution

  • How To Enable CORS in alfresco V5.2 ? Put enablecors-1.0.jar in modules/platform folder.