Symfony在登录和重定向之间丢失身份验证会话



>我有一个应用程序,它有一个表单登录,一直工作正常。

然后,我使用本指南向其添加了 api 端。现在我在网络端的登录不再有效。

这是我的安全.yaml 文件:

security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'AppEntityUser'
property: 'username'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
guard:
authenticators:
- AppSecurityLoginFormAuthenticator
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:
AppEntityUser:
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 }

我不确定我还需要发布什么,但我想不出任何其他可能导致它崩溃的事情。

如果我在实现 API 之前恢复到某个点,登录再次工作正常。

可能是什么问题?

尝试比较前后的文件以查看差异。

你检查过日志吗?您收到错误吗?

当文章谈到更改许多不同的文件时,很难说。 即您发布的文件不包含文章中的任何内容,例如^/api防火墙

也许在单独的文件夹中创建文章中的骨架行,然后与您的项目进行比较,逐个文件,添加骨架内容......

似乎您没有告诉您的主防火墙使用哪个提供商进行身份验证,因此您的 api 可能会覆盖它......

即您的主:form_login:提供者:需要是用户,并且您的 API 可以使用fos_user捆绑包

只要字段名称相同,两者就应该能够使用相同的提供程序

编辑: 1)结帐交响乐防火墙和访问控制

2) 确定是希望与主站点相同的用户,还是需要不同的 API 用户提供程序

3) 将防火墙相关部分中的form_login指向要使用的用户提供程序

上面您注册了一个提供商,即提供商部分中的"用户"。 假设您希望将用户分开访问主站点: 如果您按照本文进行操作,则会在该部分中将"fos_userbundle"作为另一个提供程序,并添加防火墙部分以允许 API 的 oauth。在"aouth_authorize"上的"form_login"下,有一个指向fos_userbundle的提供程序。您还应该添加 api 路由来控制您的 api 响应哪个路由(模式:^/api <== 任何以 api 开头的路由)

我怀疑您在主要部分下的用户提供商现在不知道使用哪个用户捆绑包进行身份验证。 即您是否尝试过在主站点上以 API 用户身份登录?它是否进行身份验证?如果是这样,您需要通过在防火墙的"form_login"部分添加提供者:用户来告诉防火墙中的"主要"部分,用户提供者必须是"用户"提供者。

如果要为主站点和 API 使用单独的用户提供程序:

(未经测试的代码)

security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
entity:
class: 'AppEntityUser'
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:
- AppSecurityLoginFormAuthenticator
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:
AppEntityUser:
algorithm: bcrypt
cost: 12
FOSUserBundleModelUserInterface: 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: 'AppEntityUser'
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:
- AppSecurityLoginFormAuthenticator
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:
AppEntityUser:
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 ] }

最新更新