我正在尝试使用FOSOAuthServerBundle的文档配置FOSOAuthServerBundleFOSUserBundle . 这是配置:

config.yml

fos_user:
    db_driver: orm
    firewall_name: oauth_token #main
    user_class: Minn\UserBundle\Entity\User

fos_oauth_server:
    db_driver: orm
    client_class:        Minn\UserBundle\Entity\Client
    access_token_class:  Minn\UserBundle\Entity\AccessToken
    refresh_token_class: Minn\UserBundle\Entity\RefreshToken
    auth_code_class:     Minn\UserBundle\Entity\AuthCode
    service:
        user_provider: fos_user.user_manager # this is added to fos_oauth to use fos_user for authentication
        options:
            supported_scopes: api

security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_USER : ROLE_API
        ROLE_ADMIN: ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern:      ^/(_(profiler|wdt)|css|images|js)/
            security:     false

        oauth_token:
            pattern:      ^/oauth/v2/token
            security:     false

        api_doc:
            pattern:      ^/api/doc
            security:     false

        api:
            pattern:      ^/api
            fos_oauth:    true
            stateless:    true

    access_control:
        - { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }

在这一点上,它似乎正在测试配置与symfony命令的创建工作良好 .

测试命令:

<?php

namespace Minn\UserBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Acme\OAuthServerBundle\Document\Client;

class CreateOAuthClientCommand extends ContainerAwareCommand {

    protected function configure() {
        $this
                ->setName('oauth:client:create')
                ->setDescription('Creates a new client')
                ->addOption(
                        'redirect-uri', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.', null
                )
                ->addOption(
                        'grant-type', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Sets allowed grant type for client. Use this option multiple times to set multiple grant types..', null
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
        $client = $clientManager->createClient();
        $client->setRedirectUris($input->getOption('redirect-uri'));
        $client->setAllowedGrantTypes($input->getOption('grant-type'));
        $clientManager->updateClient($client);
        $output->writeln(
                sprintf(
                        'Added a new client with public id <info>%s</info>, secret <info>%s</info>', $client->getPublicId(), $client->getSecret()
                )
        );
    }

}
  • 运行以下symfony命令:
php app/console oauth:client:create --redirect-uri="http://localhost/minnapi/web/app_dev.php/" --grant-type="authorization_code" --grant-type="password" --grant-type="refresh-token" --grant-type="token" --grant-type="client_credentials"
  • 命令的输出是:
Added a new client with public id 5_552osbf54k4c0kow00ko8ww8kkgcwgg4g4okkgc0wcww0ggsw8, secret 10kv0z11wr688o8kws4wg08scs48o4o8o8cg004c44wcgcgc4s

请注意,该命令在表客户端创建了一条记录,如下所示:

INSERT INTO `minn_client` (`id`, `name`, `random_id`, `redirect_uris`, `secret`, `allowed_grant_types`) VALUES
(5, NULL, '552osbf54k4c0kow00ko8ww8kkgcwgg4g4okkgc0wcww0ggsw8', 'a:1:{i:0;s:41:"http://localhost/minnapi/web/app_dev.php/";}', '10kv0z11wr688o8kws4wg08scs48o4o8o8cg004c44wcgcgc4s', 'a:5:{i:0;s:18:"authorization_code";i:1;s:8:"password";i:2;s:13:"refresh-token";i:3;s:5:"token";i:4;s:18:"client_credentials";}');
  • 在浏览器中执行以下请求
http://localhost/minnapi/web/app_dev.php/oauth/v2/token?client_id=5_552osbf54k4c0kow00ko8ww8kkgcwgg4g4okkgc0wcww0ggsw8&client_secret=10kv0z11wr688o8kws4wg08scs48o4o8o8cg004c44wcgcgc4s&grant_type=client_credentials

返回的浏览器答案是:

{"access_token":"Njg5OWUzZmI5Yjg5MWFlYTZkOWNmMWIwNGMwNDNmZDhkZmEwZDhjMDM4OTcyNzZiNzRiMTNiZjBlOGMyMDk0OA","expires_in":3600,"token_type":"bearer","scope":"api"}

现在,当我尝试使用文档中提出的控制器中的操作检查它时出现问题:

测试动作

/**
 * @Route("/connect")
 */
public function connectAction(){
    // 1. creation of a client (manually)
    $clientManager = $this->get('fos_oauth_server.client_manager.default');
    $client = $clientManager->createClient();
    $client->setRedirectUris(array('http://localhost/minnapi/web/app_dev.php/'));
    $client->setAllowedGrantTypes(array('token', 'authorization_code'));
    $clientManager->updateClient($client);

    return $this->redirect($this->generateUrl('fos_oauth_server_authorize', array(
        'client_id'     => $client->getPublicId(),
        'redirect_uri'  => 'http://localhost/minnapi/web/app_dev.php/',
        'response_type' => 'api'
    )));
}

我得到的错误是:

Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Call to a member function getUser() on null" at /home/amine/NetBeansProjects/minnapi/vendor/friendsofsymfony/oauth-server-bundle/Controller/AuthorizeController.php line 58 Context: { "exception": "Object(Symfony\Component\Debug\Exception\FatalErrorException)" }

有没有关于错误的想法?

谢谢