Dmi*_* S. 5 symfony jwt lexikjwtauthbundle api-platform.com
我提供了该security.yaml文件的两个版本。根据API 平台文档的第二个版本。API 平台向创建发送自定义用户提供程序。对于security.yamlAPI 平台文档中推荐的第二个选项,我需要创建两个额外的文件。我没有将它们附加到主题中,但如有必要,我会这样做。
但我认为问题出在 JWT 中。
环境:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct() // add $username
{
$this->isActive = true;
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_ADMIN');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
}
Run Code Online (Sandbox Code Playgroud)
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/api/login
stateless: true
anonymous: true
form_login:
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
api:
pattern: ^/api
stateless: true
provider: our_db_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
Run Code Online (Sandbox Code Playgroud)
security:
encoders:
App\Entity\User:
algorithm: bcrypt
App\Security\User\WebserviceUser: bcrypt
providers:
our_db_provider:
entity:
class: App\Entity\User
property: username
webservice:
id: App\Security\User\WebserviceUserProvider
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/api/login
stateless: true
anonymous: true
provider: webservice
form_login:
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
api:
pattern: ^/api
stateless: true
provider: our_db_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
Run Code Online (Sandbox Code Playgroud)




###> lexik/jwt-authentication-bundle ###
# Key paths should be relative to the project directory
JWT_PRIVATE_KEY_PATH=var/jwt/private.pem
JWT_PUBLIC_KEY_PATH=var/jwt/public.pem
JWT_PASSPHRASE=d70414362252a41ce772dff4823d084d
###< lexik/jwt-authentication-bundle ###
Run Code Online (Sandbox Code Playgroud)
lexik_jwt_authentication:
private_key_path: '%kernel.project_dir%/%env(JWT_PRIVATE_KEY_PATH)%'
public_key_path: '%kernel.project_dir%/%env(JWT_PUBLIC_KEY_PATH)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
Run Code Online (Sandbox Code Playgroud)
cos*_*osy 14
我的解决方案是在 .htaccess 中添加它
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
Run Code Online (Sandbox Code Playgroud)
问题是加密的私钥。
在传输或发送私钥之前,私钥通常是用密码或密码加密和保护的。当您收到加密的私钥时,您必须解密该私钥才能使用该私钥。
要确定私钥是否已加密,请在任何文本编辑器中打开私钥。加密密钥的前几行类似于以下内容,带有 ENCRYPTED 字:
---BEGIN RSA PRIVATE KEY---
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,AB8E2B5B2D989271273F6730B6F9C687
------
------
------
---END RSA PRIVATE KEY---Run Code Online (Sandbox Code Playgroud)
另一方面,未加密的密钥将具有以下格式:
---BEGIN RSA PRIVATE KEY---
------
------
------
---END RSA PRIVATE KEY---Run Code Online (Sandbox Code Playgroud)
在大多数情况下,加密密钥不能直接用于应用程序。必须先解密。
Linux 中的 OpenSSL 是解密加密私钥的最简单方法。使用以下命令解密加密的 RSA 密钥:
openssl rsa -in ssl.key.secure -out ssl.key
确保将“server.key.secure”替换为加密密钥的文件名,并将“server.key”替换为加密输出密钥文件所需的文件名。
如果加密密钥受密码短语或密码保护,请在提示时输入密码短语。
完成后,您会注意到文件中的 ENCRYPTED 措辞已经消失。
如果我没有使用Postman,那么我就不会看到Symfony的错误,它帮助我找到了问题的根源。如果 Lesik LexikJWTAuthenticationBundle 处理了这个错误,那就太好了。
您需要在项目 .htaccess 文件或虚拟站点配置中允许 Authorization 标头(例如 /etc/apache2/sites-available/000-default.conf)
<Directory your_project_directory>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
Require all granted
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</Directory>
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我在 Apache 配置文件中添加了以下行。
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
Run Code Online (Sandbox Code Playgroud)
您可以在页面底部的github LexikJWTAuthenticationBundle中找到详细信息。
| 归档时间: |
|
| 查看次数: |
16696 次 |
| 最近记录: |