SF7+API Platform 4集成JWT遇401无效凭证错误求助
Symfony 7 + API Platform 4 JWT认证API:/auth路径始终返回401无效凭证
我用Symfony 7、API Platform 4搭建带JWT认证的API项目,目前/auth路径能正常接收请求,但始终返回ERROR: 401 Invalid credentials错误。试过网络和StackOverflow上的多种解决方案都没用,推测是这个特定技术栈的配置问题,求建议。
已完成以下配置:
- 生成JWT密钥
- 在bundles.php中激活Lexik JWT Bundle
- 在User实体中配置getUserIdentifier()
相关配置代码
security.yaml
security: password_hashers: Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' providers: app_user_provider: entity: class: App\Entity\Users property: email firewalls: dev: pattern: ^/_(profiler|wdt) security: false main: stateless: true provider: app_user_provider json_login: check_path: auth username_path: email password_path: password success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure jwt: ~ access_control: - { path: ^/$, roles: PUBLIC_ACCESS } - { path: ^/docs, roles: PUBLIC_ACCESS } - { path: ^/auth, roles: PUBLIC_ACCESS } - { path: ^/, roles: IS_AUTHENTICATED_FULLY }
lexik_jwt_authentication.yaml
lexik_jwt_authentication: secret_key: '%env(resolve:JWT_SECRET_KEY)%' public_key: '%env(resolve:JWT_PUBLIC_KEY)%' pass_phrase: '%env(resolve:JWT_PASSPHRASE)%'
routes/api_platform.yaml
api_platform: resource: . type: api_platform prefix: /api
routes.yaml
auth: path: /auth methods: ['POST'] controllers: resource: path: ../src/Controller/ namespace: App\Controller type: attribute
UserFixtures.php
$user1 = new Users(); $user1->setCivility('mr'); $user1->setEmail('toto@toto.com'); $user1->setRoles(['ROLE_SUPER_ADMIN']); $hashedPassword = $this->hasher->hashPassword($user1, 'rere-toto'); $user1->setPassword($hashedPassword); $user1->setUsername('jordy'); $user1->setFirstName('michael'); $user1->setLastName('jordan'); $manager->persist($user1); $manager->flush();
Users.php(实体)
<?php namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use App\Repository\UsersRepository; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\JoinColumn; use Doctrine\ORM\Mapping\ManyToOne; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; #[ORM\Entity(repositoryClass: UsersRepository::class)] #[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])] #[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')] #[ApiResource] class Users implements UserInterface, PasswordAuthenticatedUserInterface { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column(length: 180)] private ?string $email = null; #[ORM\Column] private array $roles = []; #[ORM\Column(type: 'string')] private ?string $password = null; #[ORM\Column(type: 'boolean')] private bool $isVerified = false; #[ORM\Column(type: 'string')] private string $civility; #[ORM\Column(type: 'string')] private string $firstName; #[ORM\Column(type: 'string')] private string $lastName; #[ORM\Column(type: 'string')] private string $username; #[ORM\Column(type: 'blob', nullable: true)] private string $picture; #[ORM\Column(type: 'string', length: 2)] private string $locale = 'fr'; public function getId(): ?int { return $this->id; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): static { $this->email = $email; return $this; } public function getUserIdentifier(): string { return (string) $this->email; } public function getRoles(): array { $roles = $this->roles; // guarantee every user at least has ROLE_USER $roles[] = 'ROLE_USER'; return array_unique($roles); } public function setRoles(array $roles): static { $this->roles = $roles; return $this; } public function getPassword(): string { return $this->password; } public function setPassword(string $password): static { $this->password = $password; return $this; } public function getCivility(): string { return $this->civility; } public function setCivility(string $civility): void { $this->civility = $civility; } public function getFirstName(): string { return $this->firstName; } public function setFirstName(string $firstName): void { $this->firstName = $firstName; } public function getLastName(): string { return $this->lastName; } public function setLastName(string $lastName): void { $this->lastName = $lastName; } public function getUsername(): string { return $this->username; // return $this->email; } public function setUsername(string $username): void { $this->username = $username; } public function getLocale(): string { return $this->locale; } public function setLocale(string $locale): void { $this->locale = $locale; } public function eraseCredentials(): void { // If you store any temporary, sensitive data on the user, clear it here // $this->plainPassword = null; } public function getIsVerified(): bool { return $this->isVerified; } public function setIsVerified(bool $isVerified): static { $this->isVerified = $isVerified; return $this; } public function getPicture(): string { return $this->picture; } public function setPicture(string $picture): void { $this->picture = $picture; } }
.env
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem JWT_PASSPHRASE=a0b919dGJxxxxxxcaa29fkh56bxxxxx2fde41f13381b7dac864
内容的提问来源于stack exchange,提问作者miltone
相关产品推荐
相关产品推荐

