【问题标题】:@TestSecurity tests no longer succeeding with custom HttpAuthenticationMechanism@TestSecurity 测试不再通过自定义 HttpAuthenticationMechanism 成功
【发布时间】:2023-01-24 15:48:37
【问题描述】:

添加自定义 HttpAuthenticationMechanism 时,@TestSecurity 注释不再起作用。

  1. 按照 https://quarkus.io/guides/security-jwt 中的描述设置一个带有 SmallRye JWT 身份验证的项目
  2. 使用@TestSecurity(user = "user") 注释的测试方法创建@QuarkusTest 测试,检查状态代码 200
  3. 运行测试,成功,状态码为200
  4. 添加一个没有任何自定义逻辑的自定义 HttpAuthenticationMechanism,只是转发调用(见下文,记录在https://quarkus.io/guides/security-customization#dealing-with-more-than-one-http-auth-mechanisms
  5. 测试不再成功,因为返回的结果是 401
    @Alternative
    @Priority(1)
    @ApplicationScoped
    public class MyHttpAuthenticationMechanism implements HttpAuthenticationMechanism {
    
        @Inject
         JWTAuthMechanism jwt;
        
        @Override
        public Uni<SecurityIdentity> authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) {
            return jwt.authenticate(context, identityProviderManager);
        }
    
        @Override
        public Uni<ChallengeData> getChallenge(RoutingContext context) {
            return jwt.getChallenge(context);
        }
    
        @Override
        public Set<Class<? extends AuthenticationRequest>> getCredentialTypes() {
            return jwt.getCredentialTypes();
        }
    
        @Override
        public Uni<HttpCredentialTransport> getCredentialTransport(RoutingContext context) {
            return jwt.getCredentialTransport(context);
        }
    }
    

    我怎样才能使测试再次成功?

【问题讨论】:

    标签: quarkus


    【解决方案1】:

    src/test/java 下添加以下类似乎有效。

    @Alternative
    @Priority(1)
    @ApplicationScoped
    public class TestHttpAuthenticationMechanism extends io.quarkus.test.security.TestHttpAuthenticationMechanism {
    }
    

    由于 MyHttpAuthenticationMechanism 上的 @Alternative@Priority 属性,我假设 io.quarkus.test.security.TestHttpAuthenticationMechanism 未在测试中使用。所以在io.quarkus.test.security.TestHttpAuthenticationMechanism的子类上设置这些属性使得这个测试机制再次被使用。

    【讨论】:

      猜你喜欢
      • 2012-07-17
      • 2021-12-29
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      相关资源
      最近更新 更多