【问题标题】:How do you disable Okta auto configuration for testing?如何禁用 Okta 自动配置以进行测试?
【发布时间】:2020-08-05 01:21:29
【问题描述】:

我正在使用this Okta Spring Boot starter,但我无法禁用集成测试的自动配置。通常,你会这样做:

@SpringBootTest
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class, OktaOAuth2AutoConfig.class })
class ApplicationTests {

    @Test
    void contextLoads() {
    }
}

但是,OktaOAuth2AutoConfig 类是包保护的,您不能禁用它。我已经尝试过组件扫描过滤器和许多其他技术,但问题是它仍在加载并需要okta.oauth2.issuer(以及客户端 ID 和机密)并调用它以确保它是有效的 OAuth2 提供程序。我不想要这个功能,以防测试必须在自动配置无法调用发行者的地方运行。有什么想法吗?

【问题讨论】:

    标签: spring-boot spring-security okta


    【解决方案1】:

    您应该能够模拟事物,以便进行 OIDC 发现。我在this blog post 中展示了如何为 JHipster 应用程序执行此操作。由于您使用的是 Okta Spring Boot 启动器,我猜您可以执行此类操作。

    1. 创建一个TestSecurityConfiguration.java 类。
    @TestConfiguration
    public class TestSecurityConfiguration {
        private final ClientRegistration clientRegistration;
    
        public TestSecurityConfiguration() {
            this.clientRegistration = clientRegistration().build();
        }
    
        @Bean
        ClientRegistrationRepository clientRegistrationRepository() {
            return new InMemoryClientRegistrationRepository(clientRegistration);
        }
    
        private ClientRegistration.Builder clientRegistration() {
            Map<String, Object> metadata = new HashMap<>();
            metadata.put("end_session_endpoint", "https://example.org/logout");
    
            return ClientRegistration.withRegistrationId("okta")
                .redirectUriTemplate("{baseUrl}/{action}/oauth2/code/{registrationId}")
                .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .scope("read:user")
                .authorizationUri("https://example.org/login/oauth/authorize")
                .tokenUri("https://example.org/login/oauth/access_token")
                .jwkSetUri("https://example.org/oauth/jwk")
                .userInfoUri("https://api.example.org/user")
                .providerConfigurationMetadata(metadata)
                .userNameAttributeName("id")
                .clientName("Client Name")
                .clientId("client-id")
                .clientSecret("client-secret");
        }
    
        @Bean
        JwtDecoder jwtDecoder() {
            return mock(JwtDecoder.class);
        }
    
        @Bean
        public OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {
            return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
        }
    
        @Bean
        public OAuth2AuthorizedClientRepository authorizedClientRepository(OAuth2AuthorizedClientService authorizedClientService) {
            return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
        }
    }
    

    然后在使用@SpringBootTest的类中,将这个类配置为配置源。

    @SpringBootTest(classes = {YourMainApp.class, TestSecurityConfiguration.class})
    

    另一篇博文The Hitchhiker's Guide to Testing Spring Boot APIs and Angular Components with WireMock, Jest, Protractor, and Travis CI 提供了一些关于模拟 API 以进行测试的附加信息。具体见Mock Okta’s API with WireMock

    【讨论】:

    • 谢谢哥们。你的博客文章非常有帮助。我想我有办法在完全理解发生了什么之前理解 OAuth2,但是使用该测试配置,我可以从 application.properties 中删除 Okta 配置以进行测试。我认为这就是我正在寻找的东西,但也许经过更多研究,我可以进一步确定。
    • 以“它必须更容易”的心态浏览了无尽的互联网资源后 - 我为你的解决方案安顿下来,这是我唯一设法离线工作的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 2016-02-15
    • 2015-04-27
    • 2014-01-25
    • 1970-01-01
    相关资源
    最近更新 更多