Symfony2 rest api 安全配置(试图理解)

Symfony2 rest api security configuration (Trying to understand)

我',正在使用 Symfony2(FOSRestBundle、FOSOauthBundle、JMSBundle)休息 api,但我不明白(也没有找到)我应该如何设置我的 angularjs 应用程序访问我的 api 资源。我对安全部分有点困惑并且有很多问题。

1- 我准备了 oauth 客户端。由于 angular 代码已公开,我很确定我无法在代码中添加我的密码和客户端 ID 以进行身份​​验证,所以我很忙。 H

2- 我在尝试访问资源时遇到(无 'Access-Control-Allow-Origin')错误。我怎样才能简单地允许我的应用程序访问资源(CORS!nelmio/cors-bundle?)但是我对我的应用程序的 oauth 和 CORS 授权的作用感到困惑。

如有任何帮助,我们将不胜感激。 谢谢

  1. 你可以曝光。不是完全授权。这只是客户端授权,不是用户。

  2. NelmioCorsBundle 是一个不错的选择。您需要这样的配置:

    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
    

allow_origin,你应该设置为my.frontend.domain.com。例如,这将为您的 AngularJS 前端打开您的 API。如果您将 API 构建为一项服务(对所有人开放),则比对所有来源“*”开放。

CORS 不是授权。将其视为防火墙。比你还需要授权 (OAuth)。