简体   繁体   English

Symfony2 rest api安全配置(尝试了解)

[英]Symfony2 rest api security configuration (Trying to understand)

I', working on a rest api with Symfony2 (FOSRestBundle, FOSOauthBundle, JMSBundle) and I do not understand (and don't find) how I'm supposed to setup my angularjs app to access my api resources. 我正在使用Symfony2(FOSRestBundle,FOSOauthBundle,JMSBundle)开发一个REST API,但我不了解(也找不到)我应该如何设置我的angularjs应用程序来访问我的api资源。 I'm a bit confused about the security part and have a lot of question. 我对安全性部分感到困惑,并且有很多疑问。

1- I prepared the oauth client. 1-我准备了oauth客户端。 since angular code is exposed I'm pretty sure that I can't add my secret and client id inside the code for authentication so I'm stack. 因为公开了角度代码,所以我很确定不能在代码中添加密码和客户端ID进行身份验证,所以我将其堆叠。 H H

2- I'm having (No 'Access-Control-Allow-Origin') error when I try to access the resources. 2-尝试访问资源时出现错误(没有“ Access-Control-Allow-Origin”错误)。 How can I simply allow my app to access the resources (CORS! nelmio/cors-bundle?) but then I'm getting confused about the role of oauth and CORS authorization of my app. 我如何才能简单地允许我的应用访问资源(CORS!nelmio / cors-bundle?),但是我对我的应用的oauth和CORS授权的作用感到困惑。

Any help will be appreciate. 任何帮助将不胜感激。 Thanks 谢谢

  1. You can expose. 您可以公开。 It's not fully authorization. 这不是完全授权。 It's only a client auth, not a user. 它仅是客户端身份验证,而不是用户。

  2. NelmioCorsBundle is a good choice. NelmioCorsBundle是一个不错的选择。 You need a config like this: 您需要这样的配置:

     nelmio_cors: defaults: allow_credentials: false allow_origin: [] allow_headers: [] allow_methods: [] expose_headers: [] max_age: 0 hosts: [] paths: '^/': allow_origin: ['*'] allow_headers: ['origin', 'x-requested-with', 'content-type', 'authorization'] allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'] max_age: 0 

There is allow_origin , you should set it to my.frontend.domain.com . allow_origin ,您应该将其设置为my.frontend.domain.com This will open your API for example for your AngularJS frontend. 这将为您的AngularJS前端打开您的API。 If you are building API as a service (open for everyone), than open to all origins "*". 如果您要构建API即服务(向所有人开放),则对所有来源“ *”开放。

CORS is not authorization. CORS不是授权。 Treat it as firewall. 将其视为防火墙。 Than you still need authorization (OAuth). 比您仍然需要授权(OAuth)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM