【发布时间】:2017-12-14 05:06:40
【问题描述】:
我目前在尝试使用 Angular 从返回 JSON 的 Symfony API 获取数据时遇到错误:
“跨域请求被阻止:同源策略不允许读取位于 http://localhost:8000/customers 的远程资源。(原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。”
这是完整结果的截图:
我知道这个问题已被多次询问,但我找不到有效的答案。
当我不尝试在 api 控制器中检索 $session 时,它可以工作并且我得到了我需要的所有数据,但它没有,这是我的 api 控制器:
/**
* @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);
}
所以当我调用“localhost:8000/index”时,用于 CAS 服务器身份验证的包也会调用 https URL,这样用户就可以向 Intranet 的公司进行身份验证,完成后,他们最终可以从 localhost:8000/ 检索结果索引
这是我的 Angular 控制器:
angular.module('frontProfilDeveloppementApp')
.controller('ClientsCtrl', function ($scope, $http){
$http.get('http://localhost:8000/customers')
.then(function (data) {
$scope.result = data;
});
});
nelmio_cors 包配置:
nelmio_cors:
defaults:
allow_credentials: true
allow_origin: ['*']
allow_headers: ['*']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
max_age: 3600
hosts: []
origin_regex: false
CAS 捆绑配置:
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}
我认为我的 API 没有设置与 angular 相同的标头,因此浏览器不允许获取。
是否可以直接从 Angular 控制器设置 headers 选项,以便与 api 匹配?
【问题讨论】:
-
您无法从角度控制标题!。您需要禁用谷歌浏览器的网络安全进行测试
-
如果我禁用安全性它会起作用(我的错误,已编辑)但无论如何,我不想禁用安全性,我想添加正确的标题(如果可能的话)!
标签: angularjs rest api symfony cors