I am currently facing an error while trying to fetch datas with Angular from my Symfony API which return JSON:
"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/customers. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)."
here is a screenshot of the full result:
I know the question have been multiple times asked but I couldn't find a working answer.
When I don't try to retrieve the $session in the api controller it works and I get all the datas I need, however it don't, here is my api controller:
/**
* @Rest\View(statusCode=Response::HTTP_CREATED)
* @Rest\Get("/customers")
*/
/**
* @Rest\View(statusCode=Response::HTTP_CREATED)
* @Rest\Get("/index")
*/
public function getIndexAction(Request $request)
{
$loginad = $this->getUser()->getUsername();
$nom_ad = "******";
$port_ad = ******;
$compte_ad = "*******";
$password_ad = "******";
//parcours de l'AD
// Connexion LDAP
$ldapconn = ldap_connect($nom_ad, $port_ad)
or die("Impossible de se connecter au serveur LDAP $nom_ad");
if ($ldapconn){
$ldapbind = ldap_bind($ldapconn, $compte_ad, $password_ad)
or die("Impossible de se binder au serveur LDAP $nom_ad");
if($ldapbind){
$employeeID = false;
$dn = "OU=CER35,DC=CeRNum,DC=dom";
$filter="(samAccountName=$loginad)";
$justtheseattributes = array( "ou", "sn", "givenname", "mail", "employeeid", "samaccountname");
$sr=ldap_search($ldapconn, $dn, $filter, $justtheseattributes);
$info = ldap_get_entries($ldapconn, $sr);
for ($i=0;$i<$info["count"];$i++) {
$employeeID = $info[$i]["employeeid"][0];
}
if (!$employeeID) {
$dn = "OU=CER56,DC=CeRNum,DC=dom";
$filter="(samAccountName=$loginad)";
$justtheseattributes = array( "ou", "sn", "givenname", "mail", "employeeid", "samaccountname");
$sr=ldap_search($ldapconn, $dn, $filter, $justtheseattributes);
$info = ldap_get_entries($ldapconn, $sr);
for ($i=0;$i<$info["count"];$i++) {
$employeeID = $info[$i]["employeeid"][0];
}
}
}
}
$agent = $this->get('doctrine')
->getRepository('CERAgentBundle:Agent', 'agent')
->findByCode($employeeID);
$session = new Session();
$session->set('agent', $agent);
$formatted = [
'civilite' => $agent[0]->getCivilite(),
'prenom' => $agent[0]->getPrenom(),
'nom' => $agent[0]->getNom()
];
return new JsonResponse($formatted);
}
So when I call "localhost:8000/index", a bundle for CAS server authentication also call an https URL so the user can authenticate themselves to the intranet's company, that done, they can finally retrieve results from localhost:8000/index
Here is my Angular controller:
angular.module('frontProfilDeveloppementApp')
.controller('ClientsCtrl', function ($scope, $http){
$http.get('http://localhost:8000/customers')
.then(function (data) {
$scope.result = data;
});
});
the nelmio_cors bundle config:
nelmio_cors:
defaults:
allow_credentials: true
allow_origin: ['*']
allow_headers: ['*']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
max_age: 3600
hosts: []
origin_regex: false
CAS bundle configuration:
p_rayno_cas_auth:
server_login_url: https://extranet-authentification-******/cas-a3net/
server_validation_url: https://extranet-authentification-*****/cas-a3net/serviceValidate
server_logout_url: https://extranet-authentification-****/cas-a3net/logout
(security.yml) :
security:
providers:
cas:
id: prayno.cas_user_provider
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
logout: ~
guard:
authenticators:
- prayno.cas_authenticator
access_control:
- { path: /index, roles: ROLE_USER}
I think my API doesn't set the same header as angular do, so the browser don't allow the fetching.
Is it possible to set headers option directly from the Angular controller, so it could match the api ones?
