connectholland/user-bundle

Symfony 4项目的用户扩展包

安装次数: 4,187

依赖关系: 0

建议者: 0

安全性: 0

星标: 8

关注者: 15

分支: 8

开放问题: 3

类型:symfony-bundle

3.0.0 2023-08-14 09:56 UTC

README

Scrutinizer Code Quality Code Coverage Build Status

Symfony 4.4和5项目的用户扩展包

功能

此扩展包将可扩展并提供

  • 简单的注册表单
  • 简单的登录表单
  • 创建用户及其角色的命令
  • 包含安全链接的电子邮件消息以完成账户注册
  • 密码恢复功能
  • 启用OAuth(Google/GitHub/Facebook等)的能力
  • API访问能力
  • 启用多因素认证的能力

环境

设置环境变量以发送电子邮件。

USERBUNDLE_FROM_EMAILADDRESS=example@example.com

创建用户

要创建新用户,请运行

./bin/console connectholland:user:create example@example.com p@$$w0rd --role=ROLE_USER

OAuth

如果应用需要OAuth登录,则应要求HWI OAuth扩展包,并添加适当的配置。

安装扩展包

composer require hwi/oauth-bundle 

在路由中取消注释oauth包含。

添加环境变量以启用特定的OAuth提供者(资源)。例如,对于Google

USERBUNDLE_OAUTH_GOOGLE_ID=xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com
USERBUNDLE_OAUTH_GOOGLE_SECRET=XXXXXXXXXXX-xx_xx_xxxxx
USERBUNDLE_OAUTH_GOOGLE_SCOPE='email profile'
# Options specific for the provider can be added in a json encoded string like below.
USERBUNDLE_OAUTH_GOOGLE_OPTIONS={"hd": "connectholland.nl"}

添加(自动)路由配置

# config/routes/connectholland_user.yaml
connectholland_user_oauth:
    resource: '@ConnecthollandUserBundle/Resources/config/routing_oauth.yaml'
    prefix: '/'

JTW令牌支持

如果应用需要JTW令牌支持,则应要求Lexik JWT认证扩展包,并添加适当的配置。

安装扩展包

composer require lexik/jwt-authentication-bundle

根据Lexik JWT认证扩展包文档所述生成公钥和私钥。将密钥位置相对于项目根目录作为环境变量JWT_SECRET_KEYJWT_PUBLIC_KEY设置,并将使用的密码短语作为JWT_PASSPHRASE设置。

API支持

要添加API支持,请安装API平台JWT认证配置防火墙并添加认证路由。

composer req api-pack jwt-auth
# Example of the security settings for your project.
# config/packages/security.yaml
    firewalls:
        api_login:
            pattern: ^/api/users/authenticate
            stateless: true
            anonymous: true
            provider: app_user_provider
            json_login:
                check_path:                 /api/users/authenticate
                success_handler:            lexik_jwt_authentication.handler.authentication_success
                failure_handler:            lexik_jwt_authentication.handler.authentication_failure
                require_previous_session:   false

        api:
            pattern: ^/api
            stateless: true
            anonymous: true
            provider: app_user_provider
            json_login:
                check_path:                 /api/users/authenticate
                success_handler:            lexik_jwt_authentication.handler.authentication_success
                failure_handler:            lexik_jwt_authentication.handler.authentication_failure
                require_previous_session:   false
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

    access_control:
        - { path: ^/api/authenticate, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api, roles: ROLE_USER }
# config/routes.yaml
api_authenticate:
    path: /api/users/authenticate

例如

###> lexik/jwt-authentication-bundle ###
JWT_SECRET_KEY=config/jwt/private.pem
JWT_PUBLIC_KEY=config/jwt/public.pem
JWT_PASSPHRASE=DEVELOPMENT-KSZEW-YHMIE-XWWCL-DBGPQ-MSYCU-RJRWL-UIYBH-TPNXM-GJTVU-BRDQI-XWXHX
###< lexik/jwt-authentication-bundle ###

安全配置示例

security:
    encoders:
        Symfony\Component\Security\Core\User\UserInterface:
            algorithm: auto

    providers:
        app_user_provider:
            entity:
                class: ConnectHolland\UserBundle\Entity\User
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true
            guard:
                authenticators:
                    - ConnectHolland\UserBundle\Security\UserBundleAuthenticator
            logout:
                path: connectholland_user_logout
            oauth:
                use_forward: false
                resource_owners:
                    # The resource_owners routing postfixes are a composition of the firewall name and the resource name
                    google: connectholland_user_oauth_check_main_google
                    facebook: connectholland_user_oauth_check_main_facebook
                    linkedin: connectholland_user_oauth_check_main_linkedin
                    # etcetera
                login_path: connectholland_user_login
                failure_path: connectholland_user_login
                oauth_user_provider:
                    service: ConnectHolland\UserBundle\Security\OAuthUserProvider

    access_control:
        - { path: ^/(login|inloggen|register|registreren|password-reset|wachtwoord-vergeten), roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: [ROLE_OAUTH, ROLE_ADMIN ] }

扩展用户实体

如果您想扩展用户实体,应克隆User并将其添加为您的项目中的实体。