[英]Can't get Lexik JWT token from other domain
Symfony 4
我通过指令做了一个 lexik jwt 自动化:
从 postman 或控制台 curl 可以正常工作。 当我尝试通过 ajax 请求(来自其他域)获取令牌时,出现标准错误
请求的资源上不存在“Access-Control-Allow-Origin”header。
我尝试通过 nelmio_cors bundle 修复它
但
如果我通过 postman 或控制台 curl 获取令牌 - 调试器甚至没有进入 controller,我得到了令牌(我什至不知道如何)。
如果我在安装 nelmio 后尝试通过 ajax 请求(来自其他域)获取令牌 - 我进入 controller:App\Controller\AuthController::getTokenUser
我有一个 500 错误
无法自动装配“App\Controller\AuthController::getTokenUser()”的参数 $user:它引用了接口“Symfony\Component\Security\Core\User\UserInterface”,但不存在这样的服务。 您是否创建了实现此接口的 class?
我不明白为什么来自其他域的 nelmio_cors 请求会到达 controller,并且没有 nelmio_cors 就无法获得...如何解决?
我需要通过来自其他域的 http 请求获得令牌
安全性.yaml
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
json_login:
provider: app_user_provider
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
# 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: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/login_check, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/test, roles: IS_AUTHENTICATED_FULLY }
路由器:
api_login_check:
path: /api/login_check
controller: App\Controller\AuthController::getTokenUser
授权控制器:
<?php
namespace App\Controller;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
class AuthController extends ApiController
{
/**
* @param UserInterface $user
* @param JWTTokenManagerInterface $JWTManager
* @return JsonResponse
*/
public function getTokenUser(UserInterface $user, JWTTokenManagerInterface $JWTManager)
{
return new JsonResponse(['token' => $JWTManager->create($user)]);
}
}
nelmio_cors.yaml:
nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
CORS_ALLOW_ORIGIN='^.*$'
.htaccess:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]
请帮忙
您的问题(可能)与 Controller 的UserInterface $user
部分有关。 尝试注入Security $security
,然后从$security
获取用户 object 。
<?php
namespace App\Controller;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Security;
class AuthController extends ApiController
{
/**
* @param Security $security
* @param JWTTokenManagerInterface $JWTManager
* @return JsonResponse
*/
public function getTokenUser(Security $security, JWTTokenManagerInterface $JWTManager): JsonResponse
{
return new JsonResponse(['token' => $JWTManager->create($security->getUser())]);
}
}
由于您无法自动连接 Symfony 中的当前用户
此解决方案适用于我而无需获取用户:
class ApiController extends AbstractFOSRestController
{
public $jwtManager;
public $tokenStorageInterface;
public $serializer;
public $imgProvider;
public function __construct(ImageProvider $imgProvider, TokenStorageInterface $tokenStorageInterface, JWTTokenManagerInterface $jwtManager)
{
$this->jwtManager = $jwtManager;
$this->tokenStorageInterface = $tokenStorageInterface;
$this->serializer = new Serializer([new ObjectNormalizer()]);
$this->imgProvider = $imgProvider;
}
/**
* @Route(name="api_login", path="/api/login_check")
* @return JsonResponse
*/
public function api_login(): JsonResponse
{
$decodedJwtToken = $this->jwtManager->decode($this->tokenStorageInterface->getToken());
return new JsonResponse([
'token' => $decodedJwtToken
]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.