From ba997d7f69cc372b61c139271760884aeba4ab7f Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 8 Jan 2025 15:58:22 +0200 Subject: [PATCH] Move BasicAuthenticationMechanism to synthetic bean --- .../deployment/HttpSecurityProcessor.java | 30 ++++++++++--------- .../BasicAuthenticationMechanism.java | 5 ---- .../security/HttpSecurityRecorder.java | 10 +++++++ 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java index 51829fb5db14e..72071c0a0690b 100644 --- a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java +++ b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java @@ -27,7 +27,6 @@ import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; -import org.jboss.jandex.AnnotationTransformation; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.IndexView; @@ -37,9 +36,7 @@ import org.jboss.jandex.TypeVariable; import org.objectweb.asm.Opcodes; -import io.quarkus.arc.DefaultBean; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; -import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem; import io.quarkus.arc.deployment.GeneratedBeanBuildItem; @@ -187,23 +184,28 @@ void detectBasicAuthImplicitlyRequired(HttpBuildTimeConfig buildTimeConfig, } @BuildStep(onlyIf = IsApplicationBasicAuthRequired.class) - AdditionalBeanBuildItem initBasicAuth(HttpBuildTimeConfig buildTimeConfig, - BuildProducer annotationsTransformerProducer, + @Record(ExecutionTime.RUNTIME_INIT) + SyntheticBeanBuildItem initBasicAuth(HttpSecurityRecorder recorder, + HttpConfiguration runtimeConfig, + HttpBuildTimeConfig buildTimeConfig, BuildProducer securityInformationProducer) { - if (makeBasicAuthMechDefaultBean(buildTimeConfig)) { - //if not explicitly enabled we make this a default bean, so it is the fallback if nothing else is defined - annotationsTransformerProducer.produce(new AnnotationsTransformerBuildItem(AnnotationTransformation - .forClasses() - .whenClass(BASIC_AUTH_MECH_NAME) - .transform(ctx -> ctx.add(DefaultBean.class)))); - } - if (buildTimeConfig.auth.basic.isPresent() && buildTimeConfig.auth.basic.get()) { securityInformationProducer.produce(SecurityInformationBuildItem.BASIC()); } - return AdditionalBeanBuildItem.builder().setUnremovable().addBeanClass(BasicAuthenticationMechanism.class).build(); + SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem + .configure(BasicAuthenticationMechanism.class) + .types(io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism.class) + .scope(Singleton.class) + .supplier(recorder.basicAuthenticationMechanismBean(runtimeConfig, buildTimeConfig.auth.form.enabled)) + .setRuntimeInit() + .unremovable(); + if (makeBasicAuthMechDefaultBean(buildTimeConfig)) { + configurator.defaultBean(); + } + + return configurator.done(); } private static boolean makeBasicAuthMechDefaultBean(HttpBuildTimeConfig buildTimeConfig) { diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/BasicAuthenticationMechanism.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/BasicAuthenticationMechanism.java index 7c16ecd8d00c6..17ed00a04f2de 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/BasicAuthenticationMechanism.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/BasicAuthenticationMechanism.java @@ -29,9 +29,6 @@ import java.util.Set; import java.util.regex.Pattern; -import jakarta.inject.Inject; -import jakarta.inject.Singleton; - import org.jboss.logging.Logger; import io.netty.handler.codec.http.HttpHeaderNames; @@ -51,7 +48,6 @@ * The authentication handler responsible for BASIC authentication as described by RFC2617 * */ -@Singleton public class BasicAuthenticationMechanism implements HttpAuthenticationMechanism { private static final Logger log = Logger.getLogger(BasicAuthenticationMechanism.class); @@ -75,7 +71,6 @@ public class BasicAuthenticationMechanism implements HttpAuthenticationMechanism private final Charset charset; private final Map userAgentCharsets; - @Inject BasicAuthenticationMechanism(HttpConfiguration runtimeConfig, HttpBuildTimeConfig buildTimeConfig) { this(runtimeConfig.auth.realm.orElse(null), buildTimeConfig.auth.form.enabled); } diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java index 1f1faee90d42d..410980a1fac66 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/security/HttpSecurityRecorder.java @@ -514,4 +514,14 @@ private static Set parseRoles(String value) { return Set.copyOf(roles); } + public Supplier basicAuthenticationMechanismBean(HttpConfiguration runtimeConfig, + boolean formAuthEnabled) { + return new Supplier<>() { + @Override + public BasicAuthenticationMechanism get() { + return new BasicAuthenticationMechanism(runtimeConfig.auth.realm.orElse(null), formAuthEnabled); + } + }; + } + }