2014-06-23 5 views
5

Ho problemi a trovare CORS abilitato sul mio server in combinazione con AngularJS. Sto usando angolare 1.2.16 e questa è la mia configurazione del server:AngularJS richiesta CORS intestazione personalizzata

Header set Access-Control-Allow-Origin "*" 
Header set Access-Control-Allow-Headers "Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name, Authorization" 
Header set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS" 
Header set Access-Control-Allow-Credentials "true" 

posso utilizzare la seguente richiesta:

$http.post(configuration.authUrl, {username: 'username', password: 'password'}) 
    .success(function (data) { 
     $cookieStore.put(configuration.sessionName, { 
      token: data.authenticationToken, 
       user: data.user 
     }); 
    }) 
    .error(function() {})); 

poiché la richiesta non utilizza un header personalizzato.

Quando poi provo a richiedere: Balance.get() con saldo è:

angular.module('portalApp') 
    .factory('Balance', ['$resource', 'Auth', 'configuration', function ($resource, Auth, configuration) { 
     return $resource(configuration.balanceUrl, {}, { 
      get: { 
       method: 'GET', 
       isArray: false, 
       headers: { 
        Authorization: Auth.getAuthToken() 
       } 
      } 
     }); 
    }]); 

ottengo un 401 Unauthorized sul balanceUrl.

Nel config ho messo:

$httpProvider.defaults.useXDomain = true; 
delete $httpProvider.defaults.headers.common['X-Requested-With']; 

Ho anche provato a mettere $http.defaults.headers.common.Authorization = Auth.getAuthToken(); prima della $resource in fabbrica risorsa Balance ma che non ha aiutato.

Le intestazioni inviate alla richiesta preflight OPTIONS non hanno l'intestazione Authorization, indipendentemente dal metodo che utilizzo. Queste sono le intestazioni di richiesta della richiesta preflight OPTIONS.

OPTIONS /api/v1.0/user/orders HTTP/1.1 
Host: host 
Connection: keep-alive 
Cache-Control: no-cache 
Access-Control-Request-Method: GET 
Pragma: no-cache 
Origin: origin 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 
Access-Control-Request-Headers: accept, authorization 
Accept: */* 
Referer: referer 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 

Qualche suggerimento?

risposta

3

Dopo molte ricerche ho scoperto che il problema non si verificava a causa della configurazione di AngularJS o Apache. Era un problema nell'app del server back-end (Java). Gli URL erano limitati per tutti i metodi HTTP, quindi quando AngularJS desidera eseguire la richiesta preflight OPTIONS, l'accesso è stato negato e viene restituito un valore 401.

Questo è stato fissato dal facendo:

if(!authenticationService.isTokenValid(glueToken) && !((HttpServletRequest)servletRequest).getMethod().equals(HttpMethod.OPTIONS.toString())){ 
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
} else { 
    filterChain.doFilter(servletRequest, servletResponse); 
} 

Invece di:

if(!authenticationService.isTokenValid(glueToken)){ 
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
} else { 
    filterChain.doFilter(servletRequest, servletResponse); 
} 
-1

Sto usando un approccio abbastanza diverso ma la premessa è sempre la stessa.

Prima prova a mettere Portatore di fronte al tuo token di accesso:

headers: { 
    Authorization: 'Bearer ' + Auth.getAuthToken() 
} 

Se non provare questo: app.config potrebbe non funzionare se non si dispone di app impostato come una variabile, quindi si può bisogno di adeguarsi.

app.config(
    function($httpProvider) { 
     $httpProvider.defaults.headers.common = {}; 
     $httpProvider.defaults.headers.post = {}; 
     $httpProvider.defaults.headers.put = {}; 
     $httpProvider.defaults.headers.patch = {}; 

     $httpProvider.defaults.headers.common.Authorization = 'Bearer ' + Auth.getAuthToken(); 
    } 
); 
3

Come so non è possibile utilizzare Access-Control-Allow-Origin "*" con Access-Control-Allow-Credentials "true".

+0

sì, non è possibile, http: // StackOverflow.com/a/19744754/101662 – Oliver