evolic/get-jwt-bundle

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

安装数: 8,921

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 37

类型:symfony-bundle

3.1.0 2022-03-08 13:51 UTC

This package is auto-updated.

Last update: 2024-09-08 19:25:41 UTC


README

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

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

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

LexikJWTAuthenticationBundle提供JSON Web Token的认证。

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

您应该仅使用SSL连接来保护您的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附加更多信息。

必须为获取令牌的URL定义一个路由

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