使用 RestClient 时出现 ContextNotActiveException
ContextNotActiveException when using RestClient
我目前正在使用 quarkus 开发基于微服务的应用程序。我需要修改 SecurityIdentity(用户使用 oidc 进行身份验证)以注入从另一个微服务检索到的权限列表。我目前使用它的目的是:
@ApplicationScoped
public class RolesAugmentor implements SecurityIdentityAugmentor {
@Inject
@RestClient
CoreServiceClient coreServiceClient;
@Override
public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context) {
return context.runBlocking(build(identity));
}
private Supplier<SecurityIdentity> build(SecurityIdentity identity) {
if (identity.isAnonymous()) {
return () -> identity;
} else {
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity);
var permissions = coreServiceClient.getPermissions();
builder.addRoles(new HashSet<>(permissions));
return builder::build;
}
}
}
其中 CoreServiceClient 是用于检索用户权限的 RestEasy RestClient,但是当我 运行 代码时,我得到 javax.ws.rs.ProcessingException:javax.enterprise.context.ContextNotActiveException
知道如何完成这项任务吗?
谢谢
euks
好的。我现在正在使用上下文传播,它似乎有效。这是更新后的代码。
@ApplicationScoped
public class RolesAugmentor implements SecurityIdentityAugmentor {
@Inject
@RestClient
CoreServiceClient coreServiceClient;
@Inject
ThreadContext threadContext;
@Override
public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context) {
return context.runBlocking(build(identity));
}
private Supplier<SecurityIdentity> build(SecurityIdentity identity) {
return threadContext.contextualSupplier(() -> {
if (identity.isAnonymous()) {
return identity;
} else {
JsonWebToken jwt = (JsonWebToken) identity.getPrincipal();
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity);
var permissions = coreServiceClient.getPermissions("Bearer " + jwt.getRawToken());
builder.addRoles(new HashSet<>(permissions));
return builder.build();
}
});
}
}
我目前正在使用 quarkus 开发基于微服务的应用程序。我需要修改 SecurityIdentity(用户使用 oidc 进行身份验证)以注入从另一个微服务检索到的权限列表。我目前使用它的目的是:
@ApplicationScoped
public class RolesAugmentor implements SecurityIdentityAugmentor {
@Inject
@RestClient
CoreServiceClient coreServiceClient;
@Override
public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context) {
return context.runBlocking(build(identity));
}
private Supplier<SecurityIdentity> build(SecurityIdentity identity) {
if (identity.isAnonymous()) {
return () -> identity;
} else {
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity);
var permissions = coreServiceClient.getPermissions();
builder.addRoles(new HashSet<>(permissions));
return builder::build;
}
}
}
其中 CoreServiceClient 是用于检索用户权限的 RestEasy RestClient,但是当我 运行 代码时,我得到 javax.ws.rs.ProcessingException:javax.enterprise.context.ContextNotActiveException
知道如何完成这项任务吗?
谢谢 euks
好的。我现在正在使用上下文传播,它似乎有效。这是更新后的代码。
@ApplicationScoped
public class RolesAugmentor implements SecurityIdentityAugmentor {
@Inject
@RestClient
CoreServiceClient coreServiceClient;
@Inject
ThreadContext threadContext;
@Override
public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context) {
return context.runBlocking(build(identity));
}
private Supplier<SecurityIdentity> build(SecurityIdentity identity) {
return threadContext.contextualSupplier(() -> {
if (identity.isAnonymous()) {
return identity;
} else {
JsonWebToken jwt = (JsonWebToken) identity.getPrincipal();
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity);
var permissions = coreServiceClient.getPermissions("Bearer " + jwt.getRawToken());
builder.addRoles(new HashSet<>(permissions));
return builder.build();
}
});
}
}