我正在尝试使用JWT身份验证在thorntail中设置JAX-RS服务。一切正常(我可以注入Principal
并且用户设置正确),除非在身份验证失败的情况下,仍然会在没有任何401-HTTP-Header的情况下发送应答。我所做的是:
@LoginConfig(authMethod = "MP-JWT", realmName = "my-domain")
添加到我的Application
-Class中security:
security-domains:
my-domain:
jaspi-authentication:
login-module-stacks:
roles-token-stack:
login-modules:
jwt-jaspi-login-module:
code: org.wildfly.swarm.microprofile.jwtauth.deployment.auth.jaas.JWTLoginModule
flag: required
auth-modules:
http:
code: org.wildfly.extension.undertow.security.jaspi.modules.HTTPSchemeServerAuthModule
module: org.wildfly.extension.undertow
flag: required
login-module-stack-ref: roles-token-stack
为了使其正常工作,我还需要做些什么?我是否需要在端点上添加任何注释?正如我所说,我想在验证失败的情况下返回401。
到目前为止我发现了:JASPICAuthenticationMechanism.isMandatory
需要返回true才能使其正常工作。如果是这种情况,则在JWTAuthMechanism.sendChallenge
失败后触发JWTAuthMechanism.authenticate
,因此将401发送给客户端。但我不知道,在哪种情况下isMandatory
返回true。
在这种情况下感谢您的帮助!
一,安全域的配置不是100%正确。这是对YAML的一部分的修复:
roles-token-stack:
login-modules:
- login-module: jwt-jaspi-login-module
code: org.wildfly.swarm.microprofile.jwtauth.deployment.auth.jaas.JWTLoginModule
flag: required
二,确实你需要在JAX-RS资源上使用常见的Java EE注释(@RolesAllowed
,@DenyAll
,@PermitAll
)。
解决方案(感谢Ladicek,见下面的评论):
如果你想使用MP JWT,不要用Swarm
启动它,如果用useUberJar
启动它,不要忘记设置标志thorntail:run
。