【问题标题】:HWIOAuthBundle - Can't redirect after successfull connectionHWIOAuthBundle - 连接成功后无法重定向
【发布时间】:2018-12-12 01:52:07
【问题描述】:

我可以根据 danvbe 的文档集成 fosuserbundle 和 hwioauthbundle。我可以通过用户名/密码、facebook 和 twitter 登录。我还可以将用户名连接到他的 facebook/twitter 帐户。例如,在用户个人资料页面,我尝试将我的用户名与我的 Facebook 帐户连接起来。连接后,我停留在页面上(网址:http://localhost/Projects/symfony/web/app_dev.php/connect/service/facebook?key=12345678),并在页面上显示一条消息“已成功连接帐户'我的帐户'!”。没有按钮可以重定向到任何地方。如何重定向到用户个人资料页面? 这是我的设置: 安全性.yml:

providers:
    fos_userbundle:
        id: fos_user.user_provider.username_email
firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            login_path: /login
            csrf_provider: form.csrf_provider
        logout: true
        anonymous: true
        oauth:
            resource_owners:
                facebook:           "/login/check-facebook"
                twitter:            "/login/check-twitter"
            login_path:        /login
            use_forward:       false
            failure_path:      /login
            default_target_path: /profile

            oauth_user_provider:
                service: tf_user_provider

routing.yml:

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix:   /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix:   /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix:   /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix:   /profile

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /connect

hwi_oauth_login:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix:   /login

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix:   /connect    

hwi_oauth_facebook_login:
    pattern: /connect/facebook

facebook_login:
    pattern: /login/check-facebook

twitter_login:
    pattern: /login/check-twitter

config.yml:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Acme\UserBundle\Entity\User
    registration:
        form:
            type: tf_user_registration
    profile:
        form:
            type: tf_user_registration
hwi_oauth:
    firewall_name: main
    resource_owners:
        facebook:
            type:          facebook
            client_id:     "id"
            client_secret: "secret"
            scope:         ""
        twitter:
            type:          twitter
            client_id:     "id"
            client_secret: "secret"
    fosub:
        # try 30 times to check if a username is available
        username_iterations: 30
        #mapping between resource owners and properties
        properties:
            facebook: facebookId
            twitter: twitterId
    # if you want to use 'connect' and do not use the FOSUB integration, configure these separately
    connect: 
        account_connector: tf_user_provider

        confirmation: true # should show confirmation page or not

    # optional HTTP Client configuration
    http_client:
        timeout:       100
        verify_peer:   true
        ignore_errors: true
        max_redirects: 5

【问题讨论】:

    标签: symfony fosuserbundle hwioauthbundle


    【解决方案1】:

    HWIOAuthBundle 没有这样的功能。

    最简单的解决方案是在 vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/views/Connect/connect_success.html.twig 模板中进行 javascript 重定向。

    或者这个解决方案(不是最好的,但对我来说很好)

    生成新的空 OAuthBundle

    php app/console generate:bundle

    覆盖 HWIOAuthBundle

    public function getParent()
    {
        return 'HWIOAuthBundle';
    }
    

    覆盖 ConnectController

    class ConnectController extends \HWI\Bundle\OAuthBundle\Controller\ConnectController

    覆盖 connectServiceAction。重写处理成功连接的部分并返回 RedirectResponse

            // Show confirmation page?
        if (!$this->container->getParameter('hwi_oauth.connect.confirmation')) {
            goto show_confirmation_page;
        }
    
        // Handle the form
        /** @var $form FormInterface */
        $form = $this->container->get('form.factory')
            ->createBuilder('form')
            ->getForm();
    
        if ($request->isMethod('POST')) {
            $form->bind($request);
    
            if ($form->isValid()) {
                show_confirmation_page:
    
                /** @var $currentToken OAuthToken */
                $currentToken = $this->container->get('security.context')->getToken();
                $currentUser  = $currentToken->getUser();
    
                $this->container->get('hwi_oauth.account.connector')->connect($currentUser, $userInformation);
                // new logic
                $this->container->get('session')->getFlashBag()->set('success', 'Successfully connected the '. $resourceOwner->getName() .' account');
                return new RedirectResponse($this->generate('connections'));
                // old logic
                //if ($currentToken instanceof OAuthToken) {
                //    // Update user token with new details
                //    $this->authenticateUser($request, $currentUser, $service, $currentToken->getRawToken(), false);
                //}
    
                //return $this->container->get('templating')->renderResponse('HWIOAuthBundle:Connect:connect_success.html.' . $this->getTemplatingEngine(), array(
                //    'userInformation' => $userInformation,
                //));
            }
        }
    

    最好的解决方案是分叉并将事件逻辑贡献给 HWIOAuthBundle 存储库;)

    【讨论】:

      【解决方案2】:

      简单复制

      vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/views/ 
      

      内容到

      app/Resources/HWIOAuthBundle/views/
      

      并编辑文件。例如。我的 layout.html.twig 看起来像这样

      {% set dialogTitle = 'register.connect' | trans([], 'dialogs')%}
      {% extends '::base.html.twig' %}
      {% block title %}{% trans%}app.title{% endtrans %}{% endblock %}
      {% block content %}
        <div class="row">
          <div class="col-sm-12">
            <div class="dialog login">
              <div class="dialog-header">{{dialogTitle}}</div>
              <div class="dialog-body">
                {% block hwi_oauth_content %}
                {% endblock hwi_oauth_content %}
               </div>
            </div>
          </div>
        </div>
      {% endblock %}
      

      不可能添加“继续”按钮或您需要的任何东西

      【讨论】:

        【解决方案3】:

        看看this

        # an optional setting to configure a query string parameter which can be used to redirect
        # the user after authentication, e.g. /connect/facebook?_destination=/my/destination will
        # redirect the user to /my/destination after facebook authenticates them.  If this is not
        # set then the user will be redirected to the original resource that they requested, or
        # the base address if no resource was requested.  This is similar to the behaviour of
        # [target_path_parameter for form login](http://symfony.com/doc/2.0/cookbook/security/form_login.html).
        # target_path_parameter: _destination
        

        所以我输入了target_path_parameter: _destination,使用了一些JS 将这个参数添加到URL,并且成功了!

        【讨论】:

          猜你喜欢
          • 2019-08-04
          • 2016-05-02
          • 2012-11-19
          • 2018-04-23
          • 2019-09-11
          • 2020-08-07
          • 2018-05-28
          • 2017-01-10
          • 1970-01-01
          相关资源
          最近更新 更多