Quarkus REST服务新增端点如何跳过所有ServerRequestFilter?
解决方案
一、让新端点绕过所有ServerRequestFilter(无需修改现有过滤器)
可以通过JAX-RS的DynamicFeature实现动态禁用目标端点的所有请求过滤器,无需改动现有过滤器代码:
- 创建自定义注解,标记需要绕过过滤器的端点:
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface BypassAllFilters { }
- 实现
DynamicFeature,为带有@BypassAllFilters注解的资源移除所有请求过滤器:
@Provider public class FilterBypassFeature implements DynamicFeature { @Override public void configure(ResourceInfo resourceInfo, FeatureContext context) { // 检查当前资源方法或类是否带有绕过注解 boolean hasBypassAnnotation = resourceInfo.getResourceMethod().isAnnotationPresent(BypassAllFilters.class) || resourceInfo.getResourceClass().isAnnotationPresent(BypassAllFilters.class); if (hasBypassAnnotation) { // 禁用所有已注册的ContainerRequestFilter context.getRegisteredInstances().stream() .filter(instance -> instance instanceof ContainerRequestFilter) .forEach(context::disable); } } }
- 在新端点方法上添加
@BypassAllFilters注解即可生效:
@Path("/app-properties") public class AppPropertiesResource { @GET @BypassAllFilters @Produces(MediaType.APPLICATION_JSON) public Map<String, String> getAppProperties() { // 属性获取逻辑 return new HashMap<>(); } }
二、暴露应用最终生效的配置属性(含环境覆盖值)
Quarkus的Config接口可直接获取经过合并、覆盖后的最终配置值(包括环境变量、系统属性、配置文件等来源的修改):
- 注入
Config实例并实现属性获取逻辑:
import io.smallrye.config.ConfigProvider; import org.eclipse.microprofile.config.Config; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import java.util.HashMap; import java.util.Map; @Path("/app-properties") public class AppPropertiesResource { @Inject Config config; @GET @BypassAllFilters @Produces(MediaType.APPLICATION_JSON) public Map<String, String> getAppProperties() { Map<String, String> properties = new HashMap<>(); // 遍历所有配置属性,获取最终生效值 config.getPropertyNames().forEach(name -> { try { String value = config.getValue(name, String.class); properties.put(name, value); } catch (Exception e) { // 处理无法转为字符串的复杂类型属性 properties.put(name, "[complex type]"); } }); return properties; } }
- (可选)添加敏感属性过滤逻辑,避免泄露密码、密钥等信息:
config.getPropertyNames().forEach(name -> { if (name.contains("password") || name.contains("secret")) { properties.put(name, "[REDACTED]"); } else { try { String value = config.getValue(name, String.class); properties.put(name, value); } catch (Exception e) { properties.put(name, "[complex type]"); } } });
内容的提问来源于stack exchange,提问作者Kiran K
相关产品推荐
相关产品推荐

