错误:在null"上调用成员函数getUser();在AuthorizeController.php第58行

时间:2016-08-01 03:04:03

标签: symfony fosuserbundle fosoauthserverbundle

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

config.yml

public class PagerAdapter extends FragmentPagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                TestFragment testFragment = new TestFragment();
                return testFragment;
            case 1:
                TestFragment testFragment2 = new TestFragment2();
                return testFragment2;
            case 2:
                TestFragment testFragment3 = new TestFragment3();
                return testFragment3;
            case 3:
                TestFragment testFragment4 = new TestFragment4();
                return testFragment4;
            case 4:
                TestFragment testFragment5 = new TestFragment5();
                return testFragment5;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

security.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

在这一点上,它似乎正在用symfony命令的创建来测试配置。

测试命令:

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()
                    )
            );
        }
    
    }
    
  • 命令的输出为:

    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"}

我得到的错误是:

未捕获的PHP异常Symfony \ Component \ Debug \ Exception \ FatalErrorException:&#34;错误:在null&#34;上调用成员函数getUser() at /home/amine/NetBeansProjects/minnapi/vendor/friendsofsymfony/oauth-server-bundle/Controller/AuthorizeController.php第58行 上下文:{&#34; exception&#34;:&#34; Object(Symfony \ Component \ Debug \ Exception \ FatalErrorException)&#34; }

是否有任何关于错误的想法?

由于

0 个答案:

没有答案