如何在Flutter中添加Java依赖?

huangapple go评论120阅读模式
英文:

How do I add a Java dependency in flutter?

问题

以下是您提供的内容的中文翻译:

我正在Java中开发一个函数,负责为我的API生成RSA令牌。
Java开发人员提供了以下要添加的依赖项:

api 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly('io.jsonwebtoken:jjwt-orgjson:0.11.2') {
    exclude group: 'org.json', module: 'json' //由Android本地提供
}

但在Flutter中,你不能像在pubspec.yaml文件中那样添加依赖。

事实证明,在我进行了研究后,还有另一种方法可以添加GitHub文件的依赖项,我找到了依赖项的GitHub链接,并将其添加到我的pubspec文件中,如下所示:

dev_dependencies:
  flutter_test:
    sdk: flutter
  plugin1:
    git:
      url: git://github.com/jwtk/jjwt.git

但是我遇到了以下错误pubspec中的git文件错误git://github.com/jwtk/jjwt.git

我在pub.dev上搜索了任何等效的依赖项,发现了dart_jsonwebtoken,但是它引发了以下错误:dart_jsonwebtoken错误

基本上,这是我用于生成令牌的Java类。在这里,Jwts.builder()需要一个适当的插件,这就是我被卡住的地方:

import android.util.Log;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class RSAKeyGenerator {
    private static PrivateKey getPrivateKey() throws GeneralSecurityException {
        String pKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        KeyFactory kf = KeyFactory.getInstance("RSA");
        byte[] decode;
        decode = android.util.Base64.decode(pKey, android.util.Base64.DEFAULT);
        PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(decode);
        return kf.generatePrivate(keySpecPKCS8);
    }
    
    public static String getJwtToken() {
        final long VALIDITY_MS = TimeUnit.MINUTES.toMillis(60);
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        Date exp = new Date(nowMillis + VALIDITY_MS);
        PrivateKey privateKey = null;
        try {
            privateKey = getPrivateKey();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        
        String jws = Jwts.builder()
                .claim("version", "13")
                .claim("user_id", "xxxxxxxxxxxxxxxxxxx")
                .setIssuedAt(now)
                .setExpiration(exp)
                .signWith(privateKey, SignatureAlgorithm.RS256)
                .setAudience("live-tv")
                .compact();
        Log.d("111__", jws);
        SpUtil.Companion.getInstance().putString(J_TOKEN, jws);
        return jws;
    }
}

我尝试了Flutter中的另一个插件,但无法在Java类中导入它,我对该插件也不太确定。我的意思是,在Java类中导入package:xxx.dart会引发错误。我该如何解决这种情况?

英文:

I am working on a function in Java which is responsible to generate a RSA token for my API's.
The java person gave the following dependency to be added

api 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly('io.jsonwebtoken:jjwt-orgjson:0.11.2') {
exclude group: 'org.json', module: 'json' //provided by Android natively
}

But In flutter ,you cannot add this like that in pubspec.yaml file.

It turns out that there is another way to add dependency of the github file(after I did my research) and I found the github link of the dependency and add it to my pubspec file like this,under dependency,

dev_dependencies:
flutter_test:
sdk: flutter
plugin1:
git:
url: git://github.com/jwtk/jjwt.git

But I am getting the following errorpubspec error for git file git://github.com/jwtk/jjwt.git

I searched on pub.dev for any equivalent dependency,which was dart_json webtoken ,but that is throwing the following error:dart_jsonwebtoken error

Basically this is my java class to generate the token.Here the Jwts.builder() excepts a appropriate plugin and that where I am stuck :

import android.util.Log;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class RSAKeyGenerator {
private static PrivateKey getPrivateKey() throws GeneralSecurityException {
String pKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
KeyFactory kf = KeyFactory.getInstance("RSA");
byte[] decode;
decode = android.util.Base64.decode(pKey, android.util.Base64.DEFAULT);
PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(decode);
return kf.generatePrivate(keySpecPKCS8);
}
public static String getJwtToken() {
final long VALIDITY_MS = TimeUnit.MINUTES.toMillis(60);
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
Date exp = new Date(nowMillis + VALIDITY_MS);
PrivateKey privateKey = null;
try {
privateKey = getPrivateKey();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
String jws = Jwts.builder()
.claim("version", "13")
.claim("user_id", "xxxxxxxxxxxxxxxxxxx")
.setIssuedAt(now)
.setExpiration(exp)
.signWith(privateKey, SignatureAlgorithm.RS256)
.setAudience("live-tv")
.compact();
Log.d("111__", jws);
SpUtil.Companion.getInstance().putString(J_TOKEN, jws);
return jws;
}
}

I worked with another plugin in flutter but you can't import that in Java class and I am not sure of that plugin as well.I mean in java class import 'package:xxx.dart' throws an error.How do I solve this scenario?

答案1

得分: 3

你不能直接将Java库/类添加到Flutter中,反之亦然。Flutter使用Dart语言,而不是其他语言。

如果你想在Android上使用提供的Java代码,你需要构建一个Flutter插件。你可以在这里阅读如何操作:https://flutter.dev/docs/development/packages-and-plugins/developing-packages

但我怀疑你是否需要一个插件来生成JWT,因为有许多处理JWT的包可供使用:https://pub.dev/packages?q=jwt

关于你的依赖错误:

下次请贴出堆栈跟踪信息,而不是截图。

dart_jsonwebtoken使用的是比Flutter中crypto包更旧的版本。你可以尝试覆盖这个依赖。

你可以在这里阅读有关依赖和覆盖的一般信息:https://dart.dev/tools/pub/dependencies

英文:

You can not add Java libraries/classes to Flutter directly, or the other way around. Flutter uses Dart and no other language.

If you want to use the provided Java code on Android, you have to build yourself a Flutter plugin. You can read on how to do that here: https://flutter.dev/docs/development/packages-and-plugins/developing-packages

But I doubt you need a plugin to generate a JWT, there are lots of packages out there that deal with JWTs: https://pub.dev/packages?q=jwt

Regarding your dependency error:

Please post the stacktrace next time and not a screenshot.

dart_jsonwebtoken uses an older version of the crypto package than Flutter. You can try overriding the dependency.

You can read about dependencies in general and overrides here: https://dart.dev/tools/pub/dependencies

huangapple
  • 本文由 发表于 2020年8月21日 17:10:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63519882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定