gfreeau/get-jwt-bundle

此Symfony扩展包提供了一个安全监听器以返回JWT

安装次数: 584,935

依赖者: 5

建议者: 0

安全: 0

星标: 86

关注者: 2

分支: 37

开放问题: 5

类型:symfony-bundle

2.0.2 2017-12-22 22:04 UTC

This package is auto-updated.

Last update: 2024-09-18 04:36:39 UTC


README

此扩展包需要LexikJWTAuthenticationBundle。请阅读该扩展包的文档,链接为https://github.com/lexik/LexikJWTAuthenticationBundle

它提供了对安全工厂 "form_login" 的替代方案。 "form_login" 是为与cookies一起使用而设计的,即使stateless参数为true,也会设置cookies。

此安全工厂不支持 "switch_user" 和 "logout" 配置选项,因为它们依赖于cookies。

LexikJWTAuthenticationBundle 提供了认证json web tokens的功能。

Json Web Tokens非常适合在SPA(如AngularJS)或移动应用程序中使用。使用此扩展包,您可以轻松地将symfony2用于您的API。

您应该仅使用SSL连接来保护您的API中的json web tokens内容。

安装

使用composer进行安装

(适用于Symfony2、Symfony3和Symfony4)

composer require gfreeau/get-jwt-bundle "^2.0"

接下来,确保在您的 app/AppKernel.php 文件中启用此扩展包

public function registerBundles()
{
    return array(
        // ...
        new Gfreeau\Bundle\GetJWTBundle\GfreeauGetJWTBundle(),
        // ...
    );
}

使用方法

可能的 security.yml 示例

    firewalls:
        gettoken:
            pattern:  ^/api/getToken$
            stateless: true
            gfreeau_get_jwt:
                # this is the default config
                username_parameter: username
                password_parameter: password
                authentication_provider: security.authentication.provider.dao
                user_checker: security.user_checker 
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure

        # protected firewall, where a user will be authenticated by its jwt token
        api:
            pattern:   ^/api
            stateless: true
            # default configuration
            lexik_jwt: ~ # check token in Authorization Header, with a value prefix of e:    bearer

此扩展包支持LexikJWTAuthenticationBundle的AuthenticationSuccessEvent,请阅读他们的文档以获取更多信息。您可以使用此事件将更多信息附加到您的json web token中。

必须为要获取token的url定义一个路由

/**
 * @Route("/api/getToken")
 * @Method({"POST"})
 */
public function getTokenAction()
{
    // The security layer will intercept this request
    return new Response('', 401);
}