2

我想编写一个允许两种 RESTful 形式的数据交互的应用程序;常规 REST CRUD 端点和 web-ui。

过去,我走的是在 JS 中实现 UI 的大部分功能的路线,这将调用常规的 REST 端点。这很好用,但是对于这个应用程序,我想使用 Qute 来执行生成页面内容的大部分基本功能。但是,为了让我正确管理端点并确保正确的 RBAC 控制,我需要通过 cookie 而不是普通标头访问 jwt。这似乎是可能的,但与其他需要标头形式的端点不同。(来源:https ://quarkus.io/guides/security-jwt#microprofile-jwt-configuration )

这里的最佳做法可能是什么?我是否应该将这两个应用程序完全分开,并用 Javascript 做所有事情?

作为参考,关于我如何在代码中使用 jwt:(来自 UI qute 生成的示例,但我以与 CRUD 端点相同的方式使用 JWT)

@Traced
@Slf4j
@Path("/")
@Tags({@Tag(name = "UI")})
@RequestScoped
@Produces(MediaType.TEXT_HTML)
public class Index extends UiProvider {

    @Inject
    Template index;

    @Inject
    JsonWebToken jwt;

    @Inject
    UserService userService;

    @GET
    @PermitAll
    @Produces(MediaType.TEXT_PLAIN)
    public TemplateInstance index(
            @Context SecurityContext securityContext
    ) {
        logRequestContext(jwt, securityContext);

        return index.data("hasToken", hasJwt(jwt));
    }

相关扩展:

    implementation 'io.quarkus:quarkus-resteasy-jackson'
    implementation 'io.quarkus:quarkus-resteasy'
    implementation 'io.quarkus:quarkus-smallrye-jwt'
    implementation 'io.quarkus:quarkus-smallrye-jwt-build'
    implementation 'io.quarkus:quarkus-qute'
    implementation 'io.quarkus:quarkus-resteasy-qute'

尝试配置(使用 yaml 扩展),导致所有休息交互的 jwt 处理异常:

mp:
  jwt:
    token:
      header: Cookie
      cookie: jwt

smallrye:
  jwt:
    always-check-authorization: true

Caused by: org.jose4j.jwt.consumer.InvalidJwtException: JWT processing failed. Additional details: [[17] Unable to process JOSE object (cause: org.jose4j.lang.JoseException: Invalid JOSE Compact Serialization. Expecting either 3 or 5 parts for JWS or JWE respectively but was 23.): function(n){var%20r%2Ce%2Ci%2Ct=this[0]%3Breturn%20arguments.length?(i=m(n)%2Cthis.each(function(e){var%20t%3B1===this.nodeType&&(null==(t=i?n.call(this%2Ce%2CS(this).val()):n)?t=%22%22:%22number%22==typeof%20t?t+=%22%22:Array.isArray(t)&&(t=S.map(t%2Cfunction(e){return%20null==e?%22%22:e+%22%22}))%2C(r=S.valHooks[this.type]||S.valHooks[this.nodeName.toLowerCase()])&&%22set%22in%20r&&void%200!==r.set(this%2Ct%2C%22value%22)||(this.value=t))})):t?(r=S.valHooks[t.type]||S.valHooks[t.nodeName.toLowerCase()])&&%22get%22in%20r&&void%200!==(e=r.get(t%2C%22value%22))?e:%22string%22==typeof(e=t.value)?e.replace(yt%2C%22%22):null==e?%22%22:e:void%200}] 应该注意的是,JWT 在常规标头中对我来说可以正常工作,并且在未设置 cookie 或标头时发生此错误。

我在最新的 Quarkus,2.4.1.Final

更新:我尝试只换成“Cookie”方法来测试带有生成令牌的 ui 端,并发现这似乎被破坏了......代码无法正确解析 cookie 中的令牌。现在,我将继续进行完整的 JS 实现,但仍然很想听听这是否可能在某个时候

重现:https ://github.com/Epic-Breakfast-Productions/OpenQuarterMaster/tree/main/software/open-qm-base-station并取消注释https://github.com/Epic-Breakfast-Productions /OpenQuarterMaster/blob/main/software/open-qm-base-station/src/main/resources/application.yaml#L70 您需要在父目录中发布库才能构建,并使用大部分它想要一个 Mongodb 实例与之交谈的 REST 端点。

4

1 回答 1

1

smallrye.jwt.always-check-authorization=true将确保同时检查 Authorization 和 Cookie 标头

于 2021-11-08T21:39:33.630 回答