xsolve-pl / xsolve-google-auth-bundle
XSolve Google Auth Bundle 可用于使用 Google Auth 登录 FOSUserBundle
Requires
- friendsofsymfony/user-bundle: v1.3.3
- google/apiclient: ~1.0.0@beta
This package is not auto-updated.
Last update: 2024-09-24 05:17:05 UTC
README
此包允许用户通过 Google Auth 2.0 API 登录到 FOSUserBundle。
它包括
- Google API 集成
- 在需要时创建新用户
- 登录
安装
-
安装并配置 FOSUserBundle
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md
-
添加到 composer.json
"require": { "xsolve-pl/xsolve-google-auth-bundle": "1.0.*" },
-
安装依赖
composer install
-
在 app/AppKernel.php 中运行包
public function registerBundles() { return array( // ... new Xsolve\GoogleAuthBundle\XsolveGoogleAuthBundle(), ); }
-
添加到 routing.yml
xsolve_google_auth: resource: "@XsolveGoogleAuthBundle/Controller/" type: annotation prefix: /
-
在 https://code.google.com/apis/console/b/0/ 注册您的服务并在 parameters.yml 中配置参数,然后添加
xsolve_google_auth: name: Xsolve 测试应用程序 client_id: "4429041xxxx.apps.googleusercontent.com" client_secret: "xrAbfJ7wmOBZbYU3TyDxxxxx" redirect_uri: "http://testapp.xsolve.pl/oauth2callback" dev_key: xsolvetestapplication
success_authorization_redirect_url: _welcome failure_authorization_redirect_url: _welcome scopes: - profile - email autoregistration: false autoregistration_domains: [] -
在 security.yml 中添加访问控制
access_control: - { path: ^/oauth2callback, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/googleauthlogin, role: IS_AUTHENTICATED_ANONYMOUSLY }
用法
配置完包后,您可以访问 /oauth2callback 页面,浏览器应重定向到 Google 访问页面。
配置选项
配置可以在 app/config/config.yml 中调整
xsolve_google_auth:
name: Xsolve test application #name of application from google console
client_id: "4429041xxxx.apps.googleusercontent.com" #client id of application from google console
client_secret: "xrAbfJ7wmOBZbYU3TyDxxxxx" #client secret key of application from google console
redirect_uri: "http://testapp.xsolve.pl/oauth2callback" #redirect uri of application from google console
dev_key: xsolvetestapplication #your developmnent's application name from google console
scopes: #data which application needs from google API
- profile
- email
success_authorization_redirect_url: _welcome #url key inside Symfony where user should be redirected after successful sign in
failure_authorization_redirect_url: _welcome #url key inside Symfony where user should be redirected after failure sign in
autoregistration: false #true|false - if false than user will be registered into system automaticaly
autoregistration_domains: #array may be empty. If autoregistration is true you can allow to register users only from selected domains
- xsolve.pl
添加链接到 Twig 模板
将此链接添加到您的 Twig 模板中,以强制使用 Google OAuth 2.0 登录操作
<a href="{{ path('xsolve_googleauth_googleauthorization_authorization') }}">Login with Google</a>
或
<a href="{{ path('xsolve_googleauth_googleauthorization_authorization_1') }}">Login with Google</a>