nti/keycloak-security-bundle
Composer 安装命令:
composer require nti/keycloak-security-bundle
包简介
Allows you to easily handle you application security thanks to keycloak.
README 文档
README
This Symfony bundle is an alternative solution to FOSUserBundle, working with keycloak.
Installation
With composer:
$ composer require nti/keycloak-security-bundle
Configuration
If you want to set up keycloak locally you can download it here and follow instructions from the official documentation. In case that you want to use keycloak in docker go directly to configuration for Docker.
Bundle configuration
Basic
In case of you already have keycloak running locally on your machine or is running remotely but without proxy, here is the default configuration you should use:
# config/packages/nti_keycloak_security.yaml nti_keycloak_security: server_url: 'http://localhost: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
If you want to use keycloak in docker you can base your stack on this sample.
Here is a stack example configuration for docker swarm:
# config/packages/nti_keycloak_security.yaml nti_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
Make sure that your php container in the container is attached to a network with keycloak, otherwise it will not be able to resolve "http://keycloak:8080/auth" and the public_server_url must be accessible through the port 80 because keycloak verify the issuer.
Route configuration
Create a new file in config/routes/ to load pre configured bundle routes.
# config/routes/nti_keycloak_security.yaml KeycloakSecurityBundle: resource: "@KeycloakSecurityBundle/Resources/config/routing.yaml" prefix: /
Symfony security configuration
To link keycloak with symfony you must change the default security configuration in symfony.
Here is a simple configuration that restrict access to /admin/* routes only to user with role "ROLE_ADMIN" :
# config/packages/security.yaml imports: - { resource: '@KeycloakSecurityBundle/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: nti_keycloak_security_provider authenticators: - NTI\KeycloakSecurityBundle\Security\Authenticator\KeycloakAuthenticator # Bearer token authentication api: pattern: ^/api guard: provider: nti_keycloak_bearer_security_provider authenticators: - NTI\KeycloakSecurityBundle\Security\Authenticator\KeycloakBearerAuthenticator role_hierarchy: ROLE_ADMIN: ROLE_USER access_control: - { path: ^/admin, roles: ROLE_ADMIN } - { path: ^/api, roles: ROLE_API }
Keycloak configuration
If you need help to use keycloak because it is the first time you work on it, we've made a little tutorial step by step describing a basic configuration of a keycloak realm that you can found here
TODO
- Install bundle configuration with flex recipe.
nti/keycloak-security-bundle 适用场景与选型建议
nti/keycloak-security-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.22k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nti/keycloak-security-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nti/keycloak-security-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-26