In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
本篇内容介绍了"Quarkus中filter过滤器跨域cors问题怎么解决"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
前言
Quarkus中的web模块是基于java标准web规范jax-rs构建的,实现则选用了jboss的resteasy。这部分只是请求路由转发部分实现。真正的请求接收则使用了eclipse开源的vert.x框架,底层也是基于netty的一个响应式开发框架。Quarkus将vert.x和resteasy集成在了一起,所以支持响应式和非响应式应用混合开发,这也是Quarkus的一大卖点。
web依赖 io.quarkus quarkus-resteasy-jsonb过滤器filter开发resteasy的filter/** * @author kl : http://kailing.pub * @version 1.0 * @date 2020/7/9 15:34 */@Priority(Priorities.USER + 1)@Providerpublic class MyFilter implements ContainerRequestFilter, ContainerResponseFilter { private volatile CurrentVertxRequest currentVertxRequest; CurrentVertxRequest currentVertxRequest() { if (currentVertxRequest == null) { currentVertxRequest = CDI.current().select(CurrentVertxRequest.class).get(); } return currentVertxRequest; } @Override public void filter(ContainerRequestContext requestContext) throws IOException { RoutingContext httpServerRequest = this.currentVertxRequest().getCurrent(); String str = httpServerRequest.getBodyAsString(); JsonObject jsonObject = httpServerRequest.getBodyAsJson(); RequestImpl request = (RequestImpl) requestContext.getRequest(); System.out.println("拦截到请求了"); } @Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { System.out.println("拦截到响应了"); }}
实现ContainerRequestFilter、ContainerResponseFilter接口,可以分别拦截请求和响应。最后使用@Provider注解标记,@Priority注解用于表明优先级,值越大,优先级越高。前面已经说过,Quarkus虽然使用了resteasy,但是请求是使用vert'x来接收的,所以在拦截器实现里,可以通过上下文信息拿到vert'x的路由信息RoutingContext
vertx的filter/** * @author kl : http://kailing.pub * @version 1.0 * @date 2020/7/9 18:15 */@ApplicationScopedpublic class MyFilter { public void initfilter(@Observes Filters filters) { filters.register(routingContext -> { HttpServerRequest httpServerRequest = routingContext.request(); ForkJoinPool.commonPool().submit(()->{ System.out.println("进入vertx拦截器,下面是header参数:"); }); httpServerRequest.headers().forEach(stringStringEntry -> { System.out.println("key:"+stringStringEntry.getKey() +",value:"+stringStringEntry.getValue()); }); routingContext.next();//这一句不能漏掉,让拦截器继续往下走的逻辑 }, 100); }}Quarkus中的跨域
1、Quarkus中解决跨域问题,可以从两个层面来分析,一个是resteasy的角度。resteasy中内置了CorsFilter过滤器,我们只需要激活它即可解决跨域问题。如:
/** * @author kl : http://kailing.pub * @version 1.0 * @date 2020/7/9 16:46 */@Providerpublic class CorsFilter extends org.jboss.resteasy.plugins.interceptors.CorsFilter { public CorsFilter() { super.setAllowedMethods("OPTIONS, GET, POST, DELETE, PUT, PATCH"); super.setAllowedHeaders("*"); super.getAllowedOrigins().add("*"); }}
2、Quarkus本身也做了跨域的解决方案,是基于vert't的角度来实现的,代码见io.quarkus.vertx.http.runtime.cors.CORSFilter。从vertx的handler就拦截到了请求并做了跨域处理,但是跨域功能默认不是开启的,我们可以基于以下的配置来激活它,并进行相关的设置:
quarkus.http.cors=truequarkus.http.cors.origins=*quarkus.http.cors.headers=accept, authorization, content-type, x-requested-withquarkus.http.cors.methods=GET, OPTIONS"Quarkus中filter过滤器跨域cors问题怎么解决"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.