Symfony 在登录和重定向之间丢失身份验证会话
Symfony losing auth session between login and redirect
我有一个应用程序有一个表单登录,一直运行良好。
然后我使用 this guide 添加了 api 面。现在我的网页端登录不了了
这是我的 security.yaml 文件:
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'App\Entity\User'
property: 'username'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
guard:
authenticators:
- App\Security\LoginFormAuthenticator
form_login:
login_path: app_login
check_path: app_login
logout:
path: app_logout
target: app_user_index
secured_area:
form_login:
csrf_token_generator: security.csrf.token_manager
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
role_hierarchy:
ROLE_ADMIN: ROLE_USER
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/delete, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
我不确定我还需要什么 post,但我想不出任何其他可能导致它损坏的更改。
如果我恢复到实施 API 之前的某个点,登录将再次正常工作。
可能是什么问题?
尝试比较前后的文件以查看差异。
你查过日志了吗?您遇到错误了吗?
由于文章谈到更改多个不同的文件,所以很难说。 IE。您发布的文件不包含文章中的任何内容,例如 ^/api firewall
也许在单独的文件夹中为文章中的行创建骨架线,然后与您的项目进行比较,一个文件一个文件地添加骨架内容...
您似乎没有告诉主防火墙使用哪个提供程序进行身份验证,因此您的 api 可能会覆盖它...
即你的 main: form_login: provider: 需要是用户,你的 api 可以使用 fos_user bundle
只要字段名称相同,两者就应该能够使用相同的提供程序
编辑:
1) 结帐交响乐 firewalls and access control
2) 决定是要与主站点相同的用户,还是要 api
的不同用户提供商
3) 将防火墙相关部分中的 form_login 指向您要使用的用户提供商
您在上方注册了一个提供者,"users" 在提供者部分。
假设您希望单独的用户访问您的主站点:
如果你按照这篇文章,你会把 "fos_userbundle" 作为那个部分的另一个提供者,并添加防火墙部分以允许 api 的 oauth。在 "aouth_authorize" 上的 "form_login" 下,有一个指向 fos_user 包的提供程序。您还应该添加 api 路由来控制您的 api 响应的路由(模式:^/api <== 任何以 api 开头的路由)
我怀疑主要部分下的用户提供者现在不知道使用哪个用户包进行身份验证。即,您是否尝试以 api 用户身份登录您的主站点?它认证吗?如果是这样,您需要告诉防火墙中的 "main" 部分,通过将 provider: users 添加到防火墙的 "form_login" 部分,用户提供者必须是 "users" 提供者。
如果您想为您的主站点和您的 api 使用单独的用户提供商:
(未经测试的代码)
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'App\Entity\User'
property: 'username'
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
form_login:
provider: fos_userbundle
check_path: /oauth/v2/auth_login_check
login_path: /oauth/v2/auth_login
use_referer: true
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: false
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
guard:
authenticators:
- App\Security\LoginFormAuthenticator
form_login:
provider: users
login_path: app_login
check_path: app_login
logout:
path: app_logout
target: app_user_index
secured_area:
form_login:
csrf_token_generator: security.csrf.token_manager
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/delete, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
如果您想对主站点和 api 使用相同的提供商:
(未经测试的代码)
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'App\Entity\User'
property: 'username'
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
form_login:
provider: users
check_path: /oauth/v2/auth_login_check
login_path: /oauth/v2/auth_login
use_referer: true
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: false
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
guard:
authenticators:
- App\Security\LoginFormAuthenticator
form_login:
provider: users
login_path: app_login
check_path: app_login
logout:
path: app_logout
target: app_user_index
secured_area:
form_login:
csrf_token_generator: security.csrf.token_manager
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
role_hierarchy:
ROLE_ADMIN: ROLE_USER
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/delete, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
我有一个应用程序有一个表单登录,一直运行良好。
然后我使用 this guide 添加了 api 面。现在我的网页端登录不了了
这是我的 security.yaml 文件:
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'App\Entity\User'
property: 'username'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
guard:
authenticators:
- App\Security\LoginFormAuthenticator
form_login:
login_path: app_login
check_path: app_login
logout:
path: app_logout
target: app_user_index
secured_area:
form_login:
csrf_token_generator: security.csrf.token_manager
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
role_hierarchy:
ROLE_ADMIN: ROLE_USER
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/delete, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
我不确定我还需要什么 post,但我想不出任何其他可能导致它损坏的更改。
如果我恢复到实施 API 之前的某个点,登录将再次正常工作。
可能是什么问题?
尝试比较前后的文件以查看差异。
你查过日志了吗?您遇到错误了吗?
由于文章谈到更改多个不同的文件,所以很难说。 IE。您发布的文件不包含文章中的任何内容,例如 ^/api firewall
也许在单独的文件夹中为文章中的行创建骨架线,然后与您的项目进行比较,一个文件一个文件地添加骨架内容...
您似乎没有告诉主防火墙使用哪个提供程序进行身份验证,因此您的 api 可能会覆盖它...
即你的 main: form_login: provider: 需要是用户,你的 api 可以使用 fos_user bundle
只要字段名称相同,两者就应该能够使用相同的提供程序
编辑: 1) 结帐交响乐 firewalls and access control
2) 决定是要与主站点相同的用户,还是要 api
的不同用户提供商3) 将防火墙相关部分中的 form_login 指向您要使用的用户提供商
您在上方注册了一个提供者,"users" 在提供者部分。 假设您希望单独的用户访问您的主站点: 如果你按照这篇文章,你会把 "fos_userbundle" 作为那个部分的另一个提供者,并添加防火墙部分以允许 api 的 oauth。在 "aouth_authorize" 上的 "form_login" 下,有一个指向 fos_user 包的提供程序。您还应该添加 api 路由来控制您的 api 响应的路由(模式:^/api <== 任何以 api 开头的路由)
我怀疑主要部分下的用户提供者现在不知道使用哪个用户包进行身份验证。即,您是否尝试以 api 用户身份登录您的主站点?它认证吗?如果是这样,您需要告诉防火墙中的 "main" 部分,通过将 provider: users 添加到防火墙的 "form_login" 部分,用户提供者必须是 "users" 提供者。
如果您想为您的主站点和您的 api 使用单独的用户提供商:
(未经测试的代码)
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'App\Entity\User'
property: 'username'
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
form_login:
provider: fos_userbundle
check_path: /oauth/v2/auth_login_check
login_path: /oauth/v2/auth_login
use_referer: true
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: false
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
guard:
authenticators:
- App\Security\LoginFormAuthenticator
form_login:
provider: users
login_path: app_login
check_path: app_login
logout:
path: app_logout
target: app_user_index
secured_area:
form_login:
csrf_token_generator: security.csrf.token_manager
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/delete, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
如果您想对主站点和 api 使用相同的提供商:
(未经测试的代码)
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'App\Entity\User'
property: 'username'
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
form_login:
provider: users
check_path: /oauth/v2/auth_login_check
login_path: /oauth/v2/auth_login
use_referer: true
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: false
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
guard:
authenticators:
- App\Security\LoginFormAuthenticator
form_login:
provider: users
login_path: app_login
check_path: app_login
logout:
path: app_logout
target: app_user_index
secured_area:
form_login:
csrf_token_generator: security.csrf.token_manager
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
role_hierarchy:
ROLE_ADMIN: ROLE_USER
# activate different ways to authenticate
# http_basic: true
# https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
# form_login: true
# https://symfony.com/doc/current/security/form_login_setup.html
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/delete, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }