0

我有一个 Micronaut Rest API,一些端点受安全注释保护

@Secured(SecurityRule.IS_AUTHENTICATED)
Single<MutableHttpResponse<?>> post(@Body @Valid CategoryCommand model);

对于使用 JWT 令牌进行授权和身份验证,我使用的是 ASP.NET 身份服务器 4 https://github.com/IdentityServer/IdentityServer4

现在我正在尝试为每个端点编写单元测试和集成。我不确定如何从身份服务器获取 JWT 令牌并从 Microanut 声明式客户端传递它

@Client(id="feteBirdProduct", path = "/category")
public interface ICategoryClient  extends ICategoryOperation{
}

JUnit 测试

@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CategoryControllerTest {

    @Inject
    ICategoryClient iCategoryClient;

    @Inject
    ICategoryProducer iCategoryProducer;
    private CategoryCommand categoryCommand;
    private CategoryCommand savedCategoryCommand;

    @BeforeAll
    @DisplayName("Initial setup")
    void initialSetUp() {
        this.categoryCommand = new CategoryCommand(null, "Men", "Men description");
    }

    @Test
    @Order(1)
    @DisplayName("Should return http status code 200 ok")
    void shouldReturnHttpStatusCode200Ok() {
        var response = this.iCategoryClient.post(this.categoryCommand).blockingGet().status();
        assertEquals(HttpStatus.OK, response);
    }
}

我知道模拟 JWT 令牌,但不确定如何模拟?

应用程序.yml

security:
    enabled: true
    token:
      jwt:
        enabled: true
        signatures:
          jwks:
            IdentityServer:
              url: 'https://localhost:5001/.well-known/openid-configuration/jwks'
4

0 回答 0