Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

coze 的jwt和若依冲突解决方案

Open
#103 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java

Research direction

Start with the DefaultJWTBuilder.generateJWT and JWTOAuthClient.doGetAccessToken stack-trace entries, then inspect the relevant pom.xml dependency tree for jjwt versions in ruoyi-tax, ruoyi-common, and coze-api. Confirm the resolved JWT version and verify that the OAuth example no longer raises NoSuchMethodError after a clean Maven build.

Written by the indexing model from the issue text.

Description

问题现象

在使用 coze-api 进行JWT OAuth认证时遇到运行时错误:

Exception in thread "main" java.lang.NoSuchMethodError: io.jsonwebtoken.JwtBuilder.signWith(Ljava/security/Key;Lio/jsonwebtoken/SignatureAlgorithm;)Lio/jsonwebtoken/JwtBuilder;
	at com.coze.openapi.service.auth.DefaultJWTBuilder.generateJWT(DefaultJWTBuilder.java:25)
	at com.coze.openapi.service.auth.JWTOAuthClient.doGetAccessToken(JWTOAuthClient.java:114)
	at com.coze.openapi.service.auth.JWTOAuthClient.getAccessToken(JWTOAuthClient.java:52)
	at com.ruoyi.coze.controller.JWTOAuthExample.main(JWTOAuthExample.java:95)

根本原因分析

核心问题:JWT库版本冲突

项目中同时存在多套JWT相关依赖:

项目原有依赖 (ruoyi-common):

io.jsonwebtoken:jjwt-api:0.9

io.jsonwebtoken:jjwt-impl:0.9

io.jsonwebtoken:jjwt-jackson:0.9

coze-api自带依赖:

com.auth0:java-jwt:3.18.2

io.jsonwebtoken:jjwt-api:0.11.5

io.jsonwebtoken:jjwt-impl:0.11.5

io.jsonwebtoken:jjwt-jackson:0.11.5

冲突机制:

不同版本的JWT库API签名不兼容

类加载器无法确定使用哪个版本的方法实现

运行时调用到错误版本的方法签名

问题诊断过程

  1. 依赖树分析
mvn dependency:tree -pl ruoyi-tax | grep -E "(jwt|coze)"

发现多个JWT库同时存在:

[INFO] \- com.coze:coze-api:jar:0.3.2:compile
[INFO]    +- com.auth0:java-jwt:jar:3.18.2:compile
[INFO]    +- io.jsonwebtoken:jjwt-api:jar:0.11.5:compile
[INFO]    +- io.jsonwebtoken:jjwt-impl:jar:0.11.5:runtime
[INFO]    +- io.jsonwebtoken:jjwt-jackson:jar:0.11.5:runtime

解决方案

方案一

升级父项目依赖,使依赖保持一致:

pom.xml中改为:

<jwt.version>0.11.5</jwt.version>

然后清理 maven:

方案二

业务项目中移除依赖:

<!-- 移除coze-api的所有exclusions -->
<dependency>
    <groupId>com.coze</groupId>
    <artifactId>coze-api</artifactId>
    <version>0.3.2</version>
    <!-- 不添加任何exclusions,让其使用完整的JWT依赖 -->
</dependency>

<!-- 从ruoyi-common中排除JWT依赖,避免冲突 -->
<dependency>
    <groupId>com.ruoyi</groupId>
    <artifactId>ruoyi-common</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
        </exclusion>
    </exclusions>
</dependency>

补充:两种方案可以同时使用,在我的测试中,方案一执行后清理 maven 后依然不行,方案二也不行,第二天起来丢给 ai 分析了一波(可能是用命令清理了缓存还是什么,就可以了),删除了方案二的代码依旧可行,我的最终代码是保留了方案一

Dominant language
Java
Stars
214
Forks
97
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from coze-dev/coze-java

All issues in coze-dev/coze-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.