gpt4 book ai didi

java - 具有 Web API 的 JWT Introspection 中间件 - Java 和 Identity Server

转载 作者:行者123 更新时间:2023-12-02 09:24:46 24 4
gpt4 key购买 nike

我目前正在使用基于 .Net Core 的 Identity Server 4 来颁发 JWT token 。我有一个 .Net Core Web api,它具有中间件,以便在 Startup.cs 中验证 JWT:

services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = "http://localhost:5005";
options.Audience = "api1";
});

正如我们所看到的,除了 token 服务器的位置以及 api 是谁(即“api1”)之外,它并没有要求太多。显然,它在幕后做一些更复杂的事情。

我找到了一个基于 Java 的与上面的中间件等效的中间件,它验证了 JWT:

String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
RSAPublicKey publicKey = //Get the key instance
RSAPrivateKey privateKey = //Get the key instance
try {
Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
JWTVerifier verifier = JWT.require(algorithm)
.withIssuer("auth0")
.build(); //Reusable verifier instance
DecodedJWT jwt = verifier.verify(token);
} catch (JWTVerificationException exception){
//Invalid signature/claims
}

这是来自HERE正如 jwt.io 网站所推荐的那样。

基本上,我希望能够在 Java 中实现与上面的 .Net Core 代码相同的功能,但它明确要求诸如 PublicPrivate 键,我目前不知道如何提供,因为 JWT 是通过请求的 header 来的。

最佳答案

在底层,Microsoft JWT 中间件将访问 IdentityServer 的发现端点并加载配置,例如颁发者和 JWKS(公钥)。发现文档始终托管在 /.well-known/openid-configuration 上。

要验证 token ,您至少需要来自 JWKS 的公钥。过去我使用 jwks-rsa 库加载它: https://www.scottbrady91.com/Kotlin/JSON-Web-Token-Verification-in-Ktor-using-Kotlin-and-Java-JWT

验证访问 token 时,您至少还需要检查该 token 的受众(该 token 是为您准备的)以及它是否已过期。

关于java - 具有 Web API 的 JWT Introspection 中间件 - Java 和 Identity Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58411330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com