简体   繁体   中英

@TestSecurity tests no longer succeeding with custom HttpAuthenticationMechanism

When adding a custom HttpAuthenticationMechanism, the @TestSecurity annotation does no longer work.

  1. setup a project with SmallRye JWT authentication as described in https://quarkus.io/guides/security-jwt
  2. create a @QuarkusTest test with test methods annotated with @TestSecurity(user = "user"), check for status code 200
  3. run the test, they succeed, status code is 200
  4. add a custom HttpAuthenticationMechanism without any custom logic, just forwarding the call (see below, documented in https://quarkus.io/guides/security-customization#dealing-with-more-than-one-http-auth-mechanisms )
  5. tests no longer succeed, because returned result is 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);
    }
}

How can I make the tests suceed again?

Adding the following class under src/test/java seems to be working.

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

I assume the io.quarkus.test.security.TestHttpAuthenticationMechanism is not used in tests due the @Alternative and @Priority attributes on MyHttpAuthenticationMechanism . So setting these attributes on a subclass of io.quarkus.test.security.TestHttpAuthenticationMechanism makes this test mechanism being used again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM