在从Symfony 3.4迁移到Symfony 5.2的过程中,我只在测试中(在应用程序中起作用)面临以下问题:
fos_rest.yaml
fos_rest:
body_listener:
enabled: true
disable_csrf_role: ROLE_API
format_listener:
rules:
- { path: '^/v1', priorities: [ 'json' ], fallback_format: json, prefer_extension: false }
zone:
- { path: ^/v1/* }security.yaml
api_login:
pattern: ^/v1/login
stateless: true
anonymous: true
provider: mg_users
json_login:
provider: mg_users
check_path: /v1/login
require_previous_session: false
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure如果我试图输出响应,则针对需要身份验证的API端点进行功能测试,然后返回输出:
请求
/** @var KernelBrowser $client */
$client = static::createClient();
$client->request(
'POST',
'/v1/login',
[],
[],
[
'CONTENT_TYPE' => 'application/json',
],
'{"email":"admin@email.it","password":"admin"}'
);响应
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='https://local.api.it/v1/login'" />
<title>Redirecting to https://local.api.it/v1/login</title>
</head>
<body>
Redirecting to <a href="https://local.api.it/v1/login">https://local.api.it/v1/login</a>.
</body>
</html>此问题仅在需要自动生成的页面/端点中出现。如果我使用$client->followRedirect(),请求将不再包含电子邮件和密码,并且在HTML中接收到一个无效的JSON错误,而不是JSON。
所以问题是:
如何防止成为redirected?
发布于 2021-02-12 10:46:53
在json_login中,username_path和password_path是必要的吗?您可以这样指定并移除这两个项:
providers:
# used to reload user from session & other features (e.g. switch_user)
api_entity_provider:
entity:
class: App\Entity\YourUserEntity
property: email
firewalls:
dev:
api_login:
pattern: ^/v1/login
stateless: true
anonymous: true
provider: api_entity_provider
json_login:
check_path: /v1/login
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false您是否在access_control中添加了您的路由:
access_control:
- { path: ^/v1/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/v1, roles: IS_AUTHENTICATED_FULLY}当我查看https://symfony.com/doc/current/http_client.html和vendor/symfony/http-client-contracts/HttpClientInterface.php时,我看到只有3个参数,第三个是选项,还有4个参数。你还在用别的东西吗?为什么"CONTENT_TYPE“而不是”内容类型“呢?
这看起来像是我发布了一年半的问题:Symfony 4 / FosREST : API routes return 404。
发布于 2021-02-16 13:27:20
我的猜测是检查您的routes.yaml文件是否包含正确的路径,如下所示
Api_login_check:
path: /v1/login_check
methods: [POST]你的security.yaml在防火墙下
login:
pattern: ^/v1/login_check
stateless: true
anonymous: true
provider: app_user_provider
json_login:
check_path: /v1/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure并将FOS_REST格式侦听器默认为
format_listener:
rules:
- { path: '^/v1', priorities: [ 'json', 'html', '*/*' ], fallback_format: json, prefer_extension: true }
- { path: '^/', priorities: [ 'html', '*/*' ], fallback_format: html, prefer_extension: true }https://stackoverflow.com/questions/66122383
复制相似问题