簡體   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