wassafr/simple-apikey-authenticator-bundle

Wassa 简单 API 密钥认证包

1.0.0 2017-12-12 15:26 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:10 UTC


README

WassaSimpleApiKeyAuthenticatorBundle 包允许您以非常简单和轻量级的方式使用 API 密钥进行 HTTP 请求认证。它不依赖于现有的 Symfony 认证机制,因为它们需要定义某种用户类才能工作。

安装

在您的 composer.json 中要求 wassafr/simple-apikey-authenticator-bundle 包,并更新您的依赖。

$ composer require wassafr/simple-apikey-authenticator-bundle

app/AppKernel.php 中注册该包

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Wassa\SimpleApiKeyAuthenticatorBundle\WassaSimpleApiKeyAuthenticatorBundle(),
    );
}

要启用配置,请按以下方式编辑 config.yml 文件

# app/config/config.yml
wassa_simple_api_key_authenticator:
    api_key: <YOUR_API_KEY>
    default_action: check | dont_check
    order: secured,unsecured | unsecured,secured
    secured_patterns:
        - ^\/api\/(?!doc)
    unsecured_patterns:
        - ^\/api\/(?!doc)
        - ^\/admin
        - ^\/bundles
        - ^\/toto

配置

  • api_key: 非常简单明了
  • default_action: 如果没有配置 API 密钥,或者请求的 URL 不匹配任何配置的模式,或者没有提供 API 密钥,则该包应允许还是拒绝访问
  • order: 请求 URL 将按照配置的模式进行评估的顺序。请注意这一点,因为它可能导致意外的访问或相反的情况
  • secured_patterns: 需要有效 API 密钥的 URL 模式
  • unsecured_patterns: 不需要有效 API 密钥的 URL 模式

大多数情况下,您不需要配置 secured_patternsunsecured_patterns,但谁知道呢。