Mã thông báo truy cập hoặc mã thông báo API thường được sử dụng làm cơ chế xác thực trong ngữ cảnh API. Mã thông báo truy cập là một chuỗi, có được trong quá trình xác thực [sử dụng ứng dụng hoặc máy chủ ủy quyền]. Vai trò của mã thông báo truy cập là xác minh danh tính người dùng và nhận được sự đồng ý trước khi phát hành mã thông báo
Mã thông báo truy cập có thể thuộc bất kỳ loại nào, chẳng hạn như chuỗi mờ, Mã thông báo web JSON [JWT] hoặc SAML2 [cấu trúc XML]. Vui lòng tham khảo RFC6750. OAuth 2. 0 Khung ủy quyền. Cách sử dụng Bearer Token cho thông số kỹ thuật chi tiết
Sử dụng Trình xác thực mã thông báo truy cập
Hướng dẫn này giả định rằng bạn đã thiết lập bảo mật và đã tạo một đối tượng người dùng trong ứng dụng của mình. Làm theo hướng dẫn bảo mật chính nếu đây chưa phải là trường hợp
1] Định cấu hình Trình xác thực mã thông báo truy cập
Để sử dụng trình xác thực mã thông báo truy cập, bạn phải định cấu hình một
// config/packages/security.php
use App\Security\AccessTokenHandler;
use Symfony\Config\SecurityConfig;
return static function [SecurityConfig $security] {
$security->firewall['main']
->accessToken[]
->tokenHandler[AccessTokenHandler::class]
;
};
6. Trình xử lý mã thông báo nhận mã thông báo từ yêu cầu và trả về mã định danh người dùng chính xác. Để có được mã định danh người dùng, việc triển khai có thể cần tải và xác thực mã thông báo [e. g. thu hồi, thời gian hết hạn, chữ ký số, v.v. ]- YAML
- XML
- PHP
1 2 3 4 5 6
# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10
// config/packages/security.php
use App\Security\AccessTokenHandler;
use Symfony\Config\SecurityConfig;
return static function [SecurityConfig $security] {
$security->firewall['main']
->accessToken[]
->tokenHandler[AccessTokenHandler::class]
;
};
Trình xử lý này phải triển khai AccessTokenHandlerInterface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// src/Security/AccessTokenHandler.php
namespace App\Security;
use App\Repository\AccessTokenRepository;
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
class AccessTokenHandler implements AccessTokenHandlerInterface
{
public function __construct[
private AccessTokenRepository $repository
] {
}
public function getUserBadgeFrom[string $accessToken]: UserBadge
{
// e.g. query the "access token" database to search for this token
$accessToken = $this->repository->findOneByValue[$accessToken];
if [null === $accessToken || !$accessToken->isValid[]] {
throw new BadCredentialsException['Invalid credentials.'];
}
// and return a UserBadge object containing the user identifier from the found token
return new UserBadge[$accessToken->getUserId[]];
}
}
Trình xác thực mã thông báo truy cập sẽ sử dụng mã định danh người dùng được trả lại để tải người dùng bằng cách sử dụng nhà cung cấp người dùng
thận trọng
Điều quan trọng là phải kiểm tra mã thông báo nếu hợp lệ. Chẳng hạn, ví dụ trên xác minh xem mã thông báo chưa hết hạn. Với các mã thông báo truy cập độc lập như JWT, người xử lý được yêu cầu xác minh chữ ký số và hiểu tất cả các khiếu nại, đặc biệt là
// config/packages/security.php
use App\Security\AccessTokenHandler;
use Symfony\Config\SecurityConfig;
return static function [SecurityConfig $security] {
$security->firewall['main']
->accessToken[]
->tokenHandler[AccessTokenHandler::class]
;
};
7, // config/packages/security.php
use App\Security\AccessTokenHandler;
use Symfony\Config\SecurityConfig;
return static function [SecurityConfig $security] {
$security->firewall['main']
->accessToken[]
->tokenHandler[AccessTokenHandler::class]
;
};
8, // config/packages/security.php
use App\Security\AccessTokenHandler;
use Symfony\Config\SecurityConfig;
return static function [SecurityConfig $security] {
$security->firewall['main']
->accessToken[]
->tokenHandler[AccessTokenHandler::class]
;
};
9 và 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 260
2] Định cấu hình Trình trích xuất mã thông báo [Tùy chọn]
Ứng dụng hiện đã sẵn sàng để xử lý các mã thông báo đến. Trình trích xuất mã thông báo truy xuất mã thông báo từ yêu cầu [e. g. một tiêu đề hoặc nội dung yêu cầu]
Theo mặc định, mã thông báo truy cập được đọc từ tham số tiêu đề yêu cầu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 261 với lược đồ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 262 [e. g.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 263]
Symfony cung cấp các trình trích xuất khác theo RFC6750
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 264 [mặc định] Mã thông báo được gửi qua tiêu đề yêu cầu. Thông thường
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 261 với lược đồ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 262.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 267Mã thông báo là một phần của chuỗi truy vấn yêu cầu. Thông thường
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 268.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 269Mã thông báo là một phần của nội dung yêu cầu trong yêu cầu POST. Thông thường
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 268
thận trọng
Do các điểm yếu bảo mật liên quan đến phương thức URI, bao gồm khả năng cao là URL hoặc nội dung yêu cầu chứa mã thông báo truy cập sẽ được ghi lại, KHÔNG NÊN sử dụng các phương pháp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 267 và
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 269 trừ khi không thể vận chuyển mã thông báo truy cập trong yêu cầu
Bạn cũng có thể tạo trình trích xuất tùy chỉnh. Lớp phải triển khai AccessTokenExtractorInterface
- YAML
- XML
- PHP
# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
5# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
6# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
0# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
1# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
2____23Có thể thiết lập nhiều trình trích xuất. Trong trường hợp này, thứ tự là quan trọng. đầu tiên trong danh sách được gọi đầu tiên
- YAML
- XML
- PHP
# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
4# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
5# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
6# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
7# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
8# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
93] Gửi yêu cầu
Đó là nó. Ứng dụng của bạn hiện có thể xác thực các yêu cầu đến bằng cách sử dụng mã thông báo API
Sử dụng trình trích xuất tiêu đề mặc định, bạn có thể kiểm tra tính năng này bằng cách gửi yêu cầu như thế này
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 160
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 161
Tùy chỉnh Trình xử lý thành công
Theo mặc định, yêu cầu vẫn tiếp tục [e. g. bộ điều khiển cho tuyến đường đang chạy]. Nếu bạn muốn tùy chỉnh xử lý thành công, hãy tạo trình xử lý thành công của riêng bạn bằng cách tạo một lớp triển khai AuthenticationSuccessHandlerInterface và định cấu hình ID dịch vụ là
// src/Security/AccessTokenHandler.php
namespace App\Security;
use App\Repository\AccessTokenRepository;
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
class AccessTokenHandler implements AccessTokenHandlerInterface
{
public function __construct[
private AccessTokenRepository $repository
] {
}
public function getUserBadgeFrom[string $accessToken]: UserBadge
{
// e.g. query the "access token" database to search for this token
$accessToken = $this->repository->findOneByValue[$accessToken];
if [null === $accessToken || !$accessToken->isValid[]] {
throw new BadCredentialsException['Invalid credentials.'];
}
// and return a UserBadge object containing the user identifier from the found token
return new UserBadge[$accessToken->getUserId[]];
}
}
3- YAML
- XML
- PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 162____33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 164
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 165
# config/packages/security.yaml
security:
firewalls:
main:
access_token:
token_handler: App\Security\AccessTokenHandler
51 2 3 4 5 6 7 8 9 10 11 12 13 14 15 167
Mẹo
Nếu bạn muốn tùy chỉnh cách xử lý lỗi mặc định, hãy sử dụng tùy chọn
// src/Security/AccessTokenHandler.php
namespace App\Security;
use App\Repository\AccessTokenRepository;
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
class AccessTokenHandler implements AccessTokenHandlerInterface
{
public function __construct[
private AccessTokenRepository $repository
] {
}
public function getUserBadgeFrom[string $accessToken]: UserBadge
{
// e.g. query the "access token" database to search for this token
$accessToken = $this->repository->findOneByValue[$accessToken];
if [null === $accessToken || !$accessToken->isValid[]] {
throw new BadCredentialsException['Invalid credentials.'];
}
// and return a UserBadge object containing the user identifier from the found token
return new UserBadge[$accessToken->getUserId[]];
}
}
4 và tạo một lớp triển khai AuthenticationFailureHandlerInterface