Spring Rest文档文档oauth / oidc安全性不起作用

如何解决Spring Rest文档文档oauth / oidc安全性不起作用

设置:

Spring-Boot 2.3.3

Java 11

身份验证服务器:Keycloak 11

上下文:

我有一个使用Keycloak 11作为oidc / oauth身份验证/授权服务器的Spring-Boot应用程序。这很好用,我的测试也能用。现在,我想用spring rest文档来记录api,因为我非常喜欢测试驱动的api设计。

@Test
@Order(5)
@WithMockKeycloakAuth(authorities = "ROLE_owner")
void createProduct_RealmRoleOwner_HttpStatusCreated() throws Exception {

MvcResult response = mockmvc.perform(post(URL).contentType(MediaType.APPLICATION_JSON)
        .content(objectMapper.writeValueAsstring(testProductDto))
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.created").isNumber())
        .andExpect(jsonPath("$.name").value(testProductDto.getName()))
        .andExpect(jsonPath("$.price").value(testProductDto.getPrice()))
        .andExpect(jsonPath("$.id").value(1L))
          .andDo(document("{method-name}",Preprocessors.preprocessRequest(),Preprocessors.preprocessResponse(
                            ResponseModifyingPreprocessors.replaceBinaryContent(),ResponseModifyingPreprocessors.limitJsonArrayLength(objectMapper),Preprocessors.prettyPrint()),responseFields(fieldWithPath("id").description("Id of the new product"),fieldWithPath("name").description("Name of the product"),fieldWithPath("price").description("Price of the new product"),fieldWithPath("created").description("Unix timestamp when the product was created")
                            )))
        .andReturn();

response = mockmvc.perform(get(URL + "/1"))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.created").isNumber())
        .andExpect(jsonPath("$.name").value(testProductDto.getName()))
        .andExpect(jsonPath("$.price").value(testProductDto.getPrice()))
        .andExpect(jsonPath("$.id").value(1L))
        .andReturn();

}

当我运行此测试时,该测试通过了,但未记录任何身份验证详细信息。因此,我添加标题,因为我的标题包含一个Authorization字段。

 @Test
    @Order(5)
    @WithMockKeycloakAuth(authorities = "ROLE_owner")
    void createProduct_RealmRoleOwner_HttpStatusCreated() throws Exception {


    MvcResult response = mockmvc.perform(post(URL).contentType(MediaType.APPLICATION_JSON).header("Authorization","Bearer dXNlcjpzZWNyZXQ=")
            .content(objectMapper.writeValueAsstring(testProductDto))
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.created").isNumber())
            .andExpect(jsonPath("$.name").value(testProductDto.getName()))
            .andExpect(jsonPath("$.price").value(testProductDto.getPrice()))
            .andExpect(jsonPath("$.id").value(1L))
              .andDo(document("{method-name}",Preprocessors.preprocessResponse(
                                ResponseModifyingPreprocessors.replaceBinaryContent(),fieldWithPath("created").description("Unix timestamp when the product was created")
                                )))
            .andDo(document("headers",requestHeaders(
                            headerWithName("Authorization").description("Bearer auth credentials"))))
            .andReturn();

    response = mockmvc.perform(get(URL + "/1"))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.created").isNumber())
            .andExpect(jsonPath("$.name").value(testProductDto.getName()))
            .andExpect(jsonPath("$.price").value(testProductDto.getPrice()))
            .andExpect(jsonPath("$.id").value(1L))
            .andReturn();
}

在这里,我尝试使用虚拟标题。它以401失败。当我创建一个真实的Bearer令牌时,只要它没有过期,它就可以工作。当然,我无法始终在运行测试时创建Bearer令牌。如果我离开标题字段,则会得到一个异常,因为它在请求中丢失,因此无法记录。

问题:

如果我不添加标头字段,我会对认证过程的工作感到有些困惑。有人可以解释原因吗?

当我将测试与@SpringBoottest一起使用时,将加载整个应用程序上下文,并使用我的Spring安全过滤器链,因为我没有对其进行模拟。这可以解释为什么Bearer令牌过期时会被拒绝。当我使用vild时,spring rest docs将我需要进行身份验证的所有详细信息添加到单独的标头文件夹中。有没有一种方法可以创建没有有效Bearer令牌的标头文件夹?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?