繁体   English   中英

自定义 JWT 生成在 WSO2 APIM 3.1.0 中不起作用

[英]Customizing JWT generation not working in WSO2 APIM 3.1.0

I tried to implement the steps given in WSO2 3.1.0 documentation ( https://apim.docs.wso2.com/en/3.0.0/learn/api-gateway/passing-end-user-attributes-to-the- backend/passing-enduser-attributes-to-the-backend-using-jwt/ )用于自定义 JWT。

As given in the documentation, I created the custom JWT generator java class, generated the jar and placed it under WSO2 Home/repository/components/lib folder. 在 deployment.toml 中进行必要的配置以启用 JWT 并重新启动服务器。

When i hit an API with the bearer token, i am getting the X-JWT-Assertion header in the carbon logs but when i decode it, it doesn't contain the custom claims that i added in the custom JWT generator java class.

它包含如下图所示的标准声明,而不是添加的自定义声明(current_timestamp、message)。

需要这方面的建议,因为我已按照文档中给出的步骤进行操作。

在此处输入图像描述

我保留了产品预定义的默认 JWT 属性和值,并添加了您告诉的那个。 将 CustomGatewayJWTGenerator jar 放在 dropins 文件夹中。 JWT 属性现在在 deployment.toml 中如下所示。 请让我知道下面显示的配置是否正确。

[apim.jwt]

启用 = 真

encoding = "base64" # base64,base64url

generator_impl = "org.wso2.carbon.apimgt.keymgt.token.JWTGenerator"

claim_dialect = "http://wso2.org/claims"

header = "X-JWT-断言"

签名算法 = "SHA256withRSA"

enable_user_claims = true

claim_extractor_impl = "org.wso2.carbon.apimgt.impl.token.DefaultClaimsRetriever"

[apim.jwt.gateway_generator]

impl = "org.wso2.carbon.test.CustomGatewayJWTGenerator"

重新启动服务器,现在当我使用 JWT 访问令牌测试 API 时,调用失败并出现 null 指针异常。 错误详情如下图所示:

错误{org.apache.synapse.transport.passthru.ServerWorker} - 处理 GET 请求时出错:/pizzashack/1.0.0/menu。 java.lang.NullPointerException at org.wso2.carbon.apimgt.gateway.handlers.security.jwt.JWTValidator.generateAndRetrieveJWTToken_aroundBody2(JWTValidator.java:353) at org.wso2.carbon.apimgt.gateway.handlers.security.jwt.JWTValidator .generateAndRetrieveJWTToken(JWTValidator.java:336) at org.wso2.carbon.apimgt.gateway.handlers.security.jwt.JWTValidator.authenticate_aroundBody0(JWTValidator.java:319) at org.wso2.carbon.apimgt.gateway.handlers.security .jwt.JWTValidator.authenticate(JWTValidator.java:110) at org.wso2.carbon.apimgt.gateway.handlers.security.Z7C82E855B0415F27BD 92D2FB4F415350Z.OAuthAuthenticator.authenticate_aroundBody4(OAuthAuthenticator.java:334) at org.wso2.carbon.apimgt.gateway.handlers.security.oauth.OAuthAuthenticator.authenticate(OAuthAuthenticator.java:109) at org.wso2.carbon.apimgt.gateway. handlers.security.APIAuthenticationHandler.isAuthenticate_aroundBody42(APIAuthenticationHandler.java:419) at org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler.isAuthenticate(APIAuthenticationHandler.java:413) at org.wso2.carbon.apimgt.gateway. handlers.security.APIAuthenticationHandler.handleRequest_aroundBody36(APIAuthenticationHandler.java:349) at org.wso2.carbon.apimgt.gateway.handlers.security.APIAu thenticationHandler.handleRequest(APIAuthenticationHandler.java:320) at org.apache.synapse.rest.API.process(API.java:367) at org.apache.synapse.rest.RESTRequestHandler.apiProcessNonDefaultStrategy(RESTRequestHandler.java:149) at org .apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:95) at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:71) at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage (Axis2SynapseEnvironment.java:327)在 org.ZB6EFD606D118D0F62066 E31419FF04CCZ.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:98) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180) at org.apache.synapse.transport.passthru.ServerWorker. processNonEntityEnclosingRESTHandler(ServerWorker.java:368) at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:189) at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java :172) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) a t java.lang.Thread.run(Thread.java:813)

经过一番研究,我发现它是一个运行在 Apache Felix 之上的 OSGi 包。

请检查示例代码 ( CustomGatewayJWTGenerator ) 中可用的以下导入:

import org.osgi.service.component.annotations.Component;

另请参见示例 pom.xml。 它添加了一些关于 OSGi 包的信息:

这是重要的部分:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>3.2.0</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Bundle-Name>${project.artifactId}</Bundle-Name>
            <Export-Package>
                org.wso2.carbon.test.*
            </Export-Package>
            <Import-Package>
                org.wo2.carbon.apimgt.gateway.*,
                org.wso2.carbon.apimgt.impl.*
                com.nimbusds.jwt.*,
                *;resolution:=optional
            </Import-Package>
        </instructions>
    </configuration>
</plugin>

如您所见,它将组件导出为 OSGi。 请参阅下面我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.wso2</groupId>
        <artifactId>wso2</artifactId>
        <version>1.2</version>
    </parent>
    <groupId>org.example</groupId>
    <artifactId>CustomGatewayJWTGenerator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>bundle</packaging>
    <dependencies>
        <dependency>
            <groupId>org.wso2.carbon.apimgt</groupId>
            <artifactId>org.wso2.carbon.apimgt.gateway</artifactId>
            <version>${carbon.apimgt.version}</version>
        </dependency>
        <dependency>
            <groupId>org.wso2.orbit.com.nimbusds</groupId>
            <artifactId>nimbus-jose-jwt</artifactId>
            <version>7.3.0.wso2v1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.2.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Name>${project.artifactId}</Bundle-Name>
                        <Export-Package>
                            la.foton.wso2.apim.custom.*
                        </Export-Package>
                        <Import-Package>
                            org.wo2.carbon.apimgt.gateway.*,
                            org.wso2.carbon.apimgt.impl.*
                            com.nimbusds.jwt.*,
                            *;resolution:=optional
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <carbon.apimgt.version>6.6.163</carbon.apimgt.version>
    </properties>
</project>

我相信您只需要更改<Export-Package>中的 package 信息,将 JAR 文件复制到$APIM_HOME/repository/components/dropins并重新启动服务器。

如果出现问题,请告诉我。 如果有效,请将答案标记为正确以帮助他人。 :)

我试图在本地重现这种情况。 当我们使用 JWT 访问令牌来调用 API 时,它是可重现的。

但是当使用 OAuth 令牌时,我能够成功获得 X-JWT-Assertion header 中的自定义声明。 请看下图。

在此处输入图像描述

使用 JWT 访问令牌调用 API 时,您需要按照此文档进行自定义。

我按照您共享的文档使用 JWT 访问令牌调用 API。 I cloned the GIT repository given in the doc.: https://github.com/wso2/samples-apim/tree/master/CustomGatewayJWTGenerator , imported the CustomGatewayJWTGenerator code into eclipse. 导入成功后,我可以在 eclipse 中看到构建路径错误:无法读取项目“CustomGatewayJWTGenerator”中的 joda-time-2.9.4.wso2v1.jar 或不是有效的 ZIP 文件 即使在 eclipse 中看到错误,我还是能够使用 Maven 构建 jar。 我将生成的 jar 放在 lib 文件夹中,并在服务器重新启动后验证 jar 也存在于 dropins 文件夹中。 但不知何故,我在 CustomGatewayJWTGenerator java class 中添加的自定义声明仍然没有出现在 X-JWT-Assertion Z099FB995346F31C749F6E40DB0F395E3 中。 这与我在导入 CustomGatewayJWTGenerator 项目后在 eclipse 中遇到的错误有关,还是我在其他地方出错了?

CustomGatewayJWTGenerator java class: 在此处输入图像描述

X-JWT-断言 header: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM