请问maven中有相关依赖,但执行的时候找不到相关依赖中的类是为什么?

请问maven中有相关依赖,但执行的时候找不到相关依赖中的类是为什么?

以下是langchain4j智谱模型的源码:

class AuthorizationInterceptor implements Interceptor {

    private static final long expireMillis = 1000 * 60 * 30;
    private static final String id = "HS256";
    private static final String jcaName = "HmacSHA256";
    private static final MacAlgorithm macAlgorithm;

    static {
        try {
            //create a custom MacAlgorithm with a custom minKeyBitLength
            int minKeyBitLength = 128;
            Class<?> c = Class.forName("io.jsonwebtoken.impl.security.DefaultMacAlgorithm");
            Constructor<?> ctor = c.getDeclaredConstructor(String.class, String.class, int.class);
            ctor.setAccessible(true);
            macAlgorithm = (MacAlgorithm) ctor.newInstance(id, jcaName, minKeyBitLength);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    private final String apiKey;
    private final Cache<String, String> cache = CacheBuilder.newBuilder()
            .expireAfterWrite(Duration.ofMillis(expireMillis))
            .build();


    public AuthorizationInterceptor(String apiKey) {
        this.apiKey = apiKey;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        String token = getOrDefault(cache.getIfPresent(this.apiKey), generateToken());
        Request request = chain.request()
                .newBuilder()
                .addHeader("Authorization", "Bearer " + token)
                .removeHeader("Accept")
                .build();
        return chain.proceed(request);
    }

    private String generateToken() {
        String[] apiKeyParts = this.apiKey.split("\\.");
        String keyId = apiKeyParts[0];
        String secret = apiKeyParts[1];
        Map<String, Object> payload = new HashMap<>(3);
        payload.put("api_key", keyId);
        payload.put("exp", currentTimeMillis() + expireMillis);
        payload.put("timestamp", currentTimeMillis());

        String token = Jwts.builder()
                .header()
                .add("alg", id)
                .add("sign_type", "SIGN")
                .and()
                .content(Json.toJson(payload))
                .signWith(new SecretKeySpec(secret.getBytes(UTF_8), jcaName), macAlgorithm)
                .compact();
        cache.put(this.apiKey, token);
        return token;
    }

}

在debug到静态代码块中的Class.forName("io.jsonwebtoken.impl.security.DefaultMacAlgorithm")方法时报错:Caused by: java.lang.ClassNotFoundException: io.jsonwebtoken.impl.security.DefaultMacAlgorithm
依赖:
image.png

io.jsonwebtoken.impl.security.DefaultMacAlgorithm是jjwt的依赖,但找不到这个类和MacAlgorithm,但是Jwts是有的,所以我去找了jar包发现jjwt里没有security这个包。。。,但明明github上是有的啊
image.png

经过一小时的修改后,把jjwt依赖换成jjwt-api,jjwt-impl,jjwt-jackson后解决库中没有security包这个问题,全局搜索DefaultMacAlgorithm类也找得到,但依旧报同样的错误,吐血......

阅读 902
1 个回答
✓ 已被采纳

最后在之前操作的基础上把jjwt版本改成0.12.6成功了,DefaultMacAlgorithm在12.0的版本后才有(所以为什么在之前的版本中就能看到)
总结就是把jjwt依赖换成jjwt-api,jjwt-impl,jjwt-jackson,再把版本号改成0.12.6即可

推荐问题
宣传栏