ksami / keycloak-security-bundle
借助keycloak,让您轻松处理应用程序的安全。
v1.1.1
2021-08-27 18:07 UTC
Requires
- php: ^7.1
- guzzlehttp/guzzle: >=6.3
- knpuniversity/oauth2-client-bundle: ~1.0
- symfony/dependency-injection: >=4.1.12
- symfony/framework-bundle: >=4.1.12
- symfony/http-foundation: >=3.4.35
- symfony/routing: >=3.0
- symfony/security-bundle: >=3.0
Requires (Dev)
- phpunit/phpunit: ^6.0
- symfony/expression-language: >=3.0
- symfony/finder: >=3.0
- symfony/form: >=3.0
- symfony/stopwatch: >=3.0
- symfony/twig-bundle: *
- symfony/validator: >=3.0
- symfony/yaml: >=3.0
This package is auto-updated.
Last update: 2024-09-29 05:56:52 UTC
README
这个Symfony包是FOSUserBundle的替代方案,与keycloak协同工作。
安装
使用composer
$ composer require idci/keycloak-security-bundle
配置
如果您想在本地上设置keycloak,可以在此 下载 并按照 官方文档 中的说明操作。如果您想在Docker中使用keycloak,请直接跳转到 Docker配置。
包配置
基本
如果您已经在本地机器上运行keycloak或远程运行但没有代理,以下是您应该使用的默认配置
# config/packages/idci_keycloak_security.yaml idci_keycloak_security: server_url: 'https://:8080/auth' # your accessible keycloak url # server_url: 'http://keycloak.example.com/auth' # example with public url realm: 'MyRealm' client_id: 'my-client' client_secret: '21d4cc5c-9ed6-4bf8-8528-6d659b66f216' default_target_path: 'home' # The route name you will be redirected to after sign in
Docker
如果您想在Docker中使用keycloak,可以基于此 示例。
以下是一个用于Docker Swarm的堆栈示例配置
# config/packages/idci_keycloak_security.yaml idci_keycloak_security: server_public_url: 'http://keycloak.docker/auth' # your keycloak url accessible via your navigator server_private_url: 'http://keycloak:8080/auth' # your keycloak container reference in the network realm: 'MyRealm' client_id: 'my-client' client_secret: '21d4cc5c-9ed6-4bf8-8528-6d659b66f216' default_target_path: 'home' # The route you will be redirected to after sign in
请确保您的PHP容器连接到包含keycloak的网络,否则它将无法解析 "http://keycloak:8080/auth",并且public_server_url必须可以通过端口80访问,因为keycloak会验证发行者。
路由配置
在 config/routes/
中创建一个新文件来加载预配置的包路由。
# config/routes/idci_keycloak_security.yaml IDCIKeycloakSecurityBundle: resource: "@IDCIKeycloakSecurityBundle/Resources/config/routing.yaml" prefix: /
Symfony安全配置
要将keycloak与symfony链接,您必须更改symfony的默认安全配置。
以下是一个简单的配置,仅允许具有“ROLE_ADMIN”角色的用户访问/admin/*
路由
# config/packages/security.yaml imports: - { resource: '@IDCIKeycloakSecurityBundle/Resources/config/security.yaml' } # import our security provider security: firewalls: # Authorize everyone to try connecting (this route is imported from our bundle routing configuration) auth_connect: pattern: ^/auth/connect/.* security: false # This bundle is using security guard provided by symfony # Login form authentication secured_area: pattern: ^/admin guard: provider: idci_keycloak_security_provider authenticators: - IDCI\Bundle\KeycloakSecurityBundle\Security\Authenticator\KeycloakAuthenticator # Bearer token authentication api: pattern: ^/api guard: provider: idci_keycloak_bearer_security_provider authenticators: - IDCI\Bundle\KeycloakSecurityBundle\Security\Authenticator\KeycloakBearerAuthenticator role_hierarchy: ROLE_ADMIN: ROLE_USER access_control: - { path: ^/admin, roles: ROLE_ADMIN } - { path: ^/api, roles: ROLE_API }
Keycloak配置
如果您需要帮助使用keycloak(因为您是第一次使用它),我们已制作了一个逐步教程,描述了keycloak领域的基本配置,您可以在此 找到
待办事项
- 使用flex食谱安装包配置。