Smfony2 : HWIOauthBundle : Using multiple locales

2019-08-07 11:33发布

问题:

I have implemented HWIOauhBundle in my Symfony2 project. Everything works fine except the use of different locales. My default locale is "fr". When logging and using "en" locale, the connexion works fine but I am redirected to the homepage corresponding to "fr" locale (the homepage is the default_target_path). I only use google login.

Any proposals ?

My configuration is the following :

app/config/routing.yml :

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

hwi_oauth_login:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix:   /{_locale}/login-oauth

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

google_login:
    pattern: /login/check-google

app/config/config.yml

# HWIOauth Configuration
hwi_oauth:
    # 
    firewall_name: main

    resource_owners:
        google:
            type:                google
            client_id:           xxxxxxxxx
            client_secret:       yyyyyyyyy
            scope:               "email profile"

    fosub:
        # try 30 times to check if a username is available (foo, foo1, foo2 etc)
        username_iterations: 30

        # mapping between resource owners and properties
        properties:
            google: googleId

    connect: 
        confirmation: true

app/config/security.yml

security:
    encoders:
        My\UserBundle\Entity\User: sha512

    role_hierarchy:
        ROLE_ADMIN:       [ROLE_AUTEUR, ROLE_MODERATEUR]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern:   ^/
            anonymous: true
            #provider:  hwi
            provider: fos_userbundle
            form_login:
                login_path: fos_user_security_login
                check_path: fos_user_security_check
                default_target_path: mybundle_homepage
                csrf_provider: form.csrf_provider
            logout:
                path:   fos_user_security_logout
                target: mybundle_homepage
                handlers: [mybundle.logout_handler]
            remember_me:
                key: %secret% 
                lifetime: 31536000
            oauth:
                resource_owners:
                    google: google_login
                login_path:  hwi_oauth_connect
                use_forward: false
                failure_path: hwi_oauth_connect
                default_target_path: mybundle_homepage

                oauth_user_provider:          
                    service: mybundle_user.provider.fosub_bridge

    access_control:
        #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

Google Developpers Console : redirect URIs

http://myhost/web/app_dev.php/connect/service/google
http://myhost/web/app_dev.php/login/check-google 

回答1:

The solution is quite simple. It just requires to introduce locale in each route and each redirect URI.

app/config/routing.yml :

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /{_locale}/connect
    requirements:
        _locale: en|fr

hwi_oauth_login:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix:   /{_locale}/login-oauth
    requirements:
        _locale: en|fr 

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix:   /{_locale}/connect
    requirements:
        _locale: en|fr 

Google Developpers Console : redirect URIs

http://myhost/web/app_dev.php/fr/connect/service/google
http://myhost/web/app_dev.php/en/connect/service/google
http://myhost/web/app_dev.php/fr/login/check-google
http://myhost/web/app_dev.php/en/login/check-google