WebSphereLiberty-JWT-JsonWebToken主体可以';不能注射,因为没有

WebSphereLiberty-JWT-JsonWebToken主体可以';不能注射,因为没有,jwt,websphere-liberty,microprofile,Jwt,Websphere Liberty,Microprofile,我试图在WebSphereLiberty上实现JWT,但在授权头中传递JWT时遇到错误 无法注入JsonWebToken主体,因为其中一个主体不可用。保护请求的资源,以便在访问资源之前进行身份验证。 同一个JWT是用私钥签名的,可以使用公钥或公共证书在JWT.io上成功验证,所以我认为有效性不是问题 这是我将JWT传递到的JAXRS web资源: import org.eclipse.microprofile.jwt.Claim; import org.eclipse.microprofile.

我试图在WebSphereLiberty上实现JWT,但在授权头中传递JWT时遇到错误

无法注入JsonWebToken主体,因为其中一个主体不可用。保护请求的资源,以便在访问资源之前进行身份验证。

同一个JWT是用私钥签名的,可以使用公钥或公共证书在JWT.io上成功验证,所以我认为有效性不是问题

这是我将JWT传递到的JAXRS web资源:

import org.eclipse.microprofile.jwt.Claim;
import org.eclipse.microprofile.jwt.Claims;
import org.eclipse.microprofile.jwt.JsonWebToken;
import javax.annotation.security.PermitAll;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/jwtpoc")
@PermitAll
@RequestScoped
public class JwtPocResource {
    @Inject
    @Claim(standard = Claims.groups)
    private Set<String> groups;

    @Inject
    private JsonWebToken token;

    @GET
    public Response getOK() {
        return Response.ok().build();
    }
}
import org.eclipse.microfile.jwt.Claim;
导入org.eclipse.microfile.jwt.Claims;
导入org.eclipse.microfile.jwt.JsonWebToken;
导入javax.annotation.security.PermitAll;
导入javax.enterprise.context.RequestScope;
导入javax.inject.inject;
导入javax.ws.rs.GET;
导入javax.ws.rs.Path;
导入javax.ws.rs.core.Response;
@路径(“/jwtpoc”)
@佩尔米塔尔
@请求范围
公共类JWTPOC资源{
@注入
@索赔(标准=索赔.组)
私有集合组;
@注入
私有JsonWebToken令牌;
@得到
公众响应getOK(){
返回Response.ok().build();
}
}
当请求GET/jwtpoc时,我可以在messages.log中看到“无法注入JsonWebToken主体,因为其中一个主体不可用。请保护请求的资源,以便在访问资源之前进行身份验证。”因此我认为这是注入或底层配置的问题

我将密钥对设置为pkcs#12密钥库,公共证书存储在pkcs#12信任库中,两者都由ssl配置引用,对ssl配置的引用在mpJwt server.xml元素中配置。我正在使用微文件3.2(mpjwt-1.1)

引用JwtPocResource的应用程序类具有:

import org.eclipse.microprofile.auth.LoginConfig;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@LoginConfig(authMethod = "MP-JWT")
@ApplicationPath("/")
public class JwtPocApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> services = new HashSet<Class<?>>();
        services.add(JwtPocResource.class);
    }
}
import org.eclipse.microfile.auth.LoginConfig;
导入javax.ws.rs.ApplicationPath;
导入javax.ws.rs.core.Application;
导入java.util.HashSet;
导入java.util.Set;
@登录配置(authMethod=“MP-JWT”)
@应用程序路径(“/”)
公共类JWTPOC应用程序扩展了应用程序{
@凌驾

public Set>services=new HashSet为了进行注入,必须保护资源。根据,注入是为经过身份验证的调用方完成的

使用@PermitAll for JAXRS将把此资源视为未受保护的资源,并且不会有经过身份验证的调用方,并且不会发生JsonWebToken的注入

要解决此问题,可以使用@RolesAllowed保护资源

如果端点必须使用@PermitAll来处理其自身的身份验证和授权,则以下情况适用:

如果将JWT发送到不需要身份验证的端点 和/或授权,则必须先验证,然后才能进行验证 通过JsonWebToken接口访问

需要自己控制身份验证过程的端点 可以通过调用 JsonWebToken.getRawToken()方法

对于1.1,认证要求也适用于

MP-JWT实现必须支持当前 作为JsonWebToken的经过身份验证的调用方


尝试删除
@PermitAll
,因为这定义了没有任何安全要求的方法。此外,您可能需要在
web.xml
中添加
SecurityConstraint
,以保护您的应用程序。恐怕没有什么乐趣。我将“PermitAll”替换为“DenyAll”,并将两个注入变量都替换为注入声明(“组”)private ClaimValue groups;当调用GET/jwtpoc now时,我得到了一个401 Unauthorized,即使JWT中的组声明包含我用RolesAllowed注释的角色。JwtPocResource类中没有RolesAllowed注释的另一个方法返回403 Forbidden,这是预期的。