重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

SpringGateway在IPV6环境发生请求的异常-创新互联

问题描述

现象:IPV6环境,使用不同的密码登陆,会导致SpringGateway时好时坏。SpringGateway里面具体错误信息:

创新互联建站主营保靖网站建设的网络公司,主营网站建设方案,重庆APP软件开发,保靖h5小程序制作搭建,保靖网站营销推广欢迎保靖等地区企业咨询

java.lang.IllegalArgumentException: Invalid character ‘:’ for HOST_IPV4 in “2400:379:1200:0:0:0:0:35”

异常堆栈信息
2023-01-16 15:06:10,469 [boundedElastic-1] ERROR [o.s.b.a.w.r.error.AbstractErrorWebExceptionHandler] CompositeLog.java:122 - [76ce6037-1, L:/2400:379:1200:0:0:0:0:35%0:15380 - R:/2400:379:1200:0:0:0:0:36%0:38824]  500 Server Error for HTTP POST "/auth/oauth/token?username=xxxx&password=MIcHIZBbB%2BdPwVZTXKd3vA%3D%3D&grant_type=password&scope=server"
java.lang.IllegalArgumentException: Invalid character ':' for HOST_IPV4 in "2400:379:1200:0:0:0:0:35"
	at org.springframework.web.util.HierarchicalUriComponents.verifyUriComponent(HierarchicalUriComponents.java:409)
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	*__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
	*__checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
	*__checkpoint ⇢ HTTP POST "/auth/oauth/token?username=lihc&password=MIcHIZBbB%2BdPwVZTXKd3vA%3D%3D&grant_type=password&scope=server" [ExceptionHandlingWebHandler]
Stack trace:
		at org.springframework.web.util.HierarchicalUriComponents.verifyUriComponent(HierarchicalUriComponents.java:409)
		at org.springframework.web.util.HierarchicalUriComponents.verify(HierarchicalUriComponents.java:372)
		at org.springframework.web.util.HierarchicalUriComponents.(HierarchicalUriComponents.java:137)
		at org.springframework.web.util.UriComponentsBuilder.buildInternal(UriComponentsBuilder.java:471)
		at org.springframework.web.util.UriComponentsBuilder.build(UriComponentsBuilder.java:460)
		at org.springframework.cloud.client.loadbalancer.LoadBalancerUriTools.doReconstructURI(LoadBalancerUriTools.java:115)
		at org.springframework.cloud.client.loadbalancer.LoadBalancerUriTools.reconstructURI(LoadBalancerUriTools.java:97)
		at org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.reconstructURI(ReactiveLoadBalancerClientFilter.java:153)
		at org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.lambda$filter$2(ReactiveLoadBalancerClientFilter.java:132)
		at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:185)
		at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:120)
		at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:82)
		at reactor.core.publisher.FluxDematerialize$DematerializeSubscriber.onNext(FluxDematerialize.java:98)
		at reactor.core.publisher.FluxDematerialize$DematerializeSubscriber.onNext(FluxDematerialize.java:44)
		at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.drainAsync(FluxFlattenIterable.java:421)
		at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.drain(FluxFlattenIterable.java:686)
		at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.onNext(FluxFlattenIterable.java:250)
		at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:74)
		at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816)
		at reactor.core.publisher.MonoFlatMap$FlatMapInner.onNext(MonoFlatMap.java:249)
		at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.complete(MonoIgnoreThen.java:284)
		at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.onNext(MonoIgnoreThen.java:187)
		at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.subscribeNext(MonoIgnoreThen.java:232)
		at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.onComplete(MonoIgnoreThen.java:203)
		at reactor.core.publisher.MonoIgnoreElements$IgnoreElementsSubscriber.onComplete(MonoIgnoreElements.java:89)
		at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:260)
		at reactor.core.publisher.FluxDematerialize$DematerializeSubscriber.onComplete(FluxDematerialize.java:121)
		at reactor.core.publisher.FluxDematerialize$DematerializeSubscriber.onNext(FluxDematerialize.java:91)
		at reactor.core.publisher.FluxDematerialize$DematerializeSubscriber.onNext(FluxDematerialize.java:44)
		at reactor.core.publisher.FluxIterable$IterableSubscription.fastPath(FluxIterable.java:340)
		at reactor.core.publisher.FluxIterable$IterableSubscription.request(FluxIterable.java:227)
		at reactor.core.publisher.FluxDematerialize$DematerializeSubscriber.request(FluxDematerialize.java:127)
		at reactor.core.publisher.FluxPeek$PeekSubscriber.request(FluxPeek.java:138)
		at reactor.core.publisher.MonoIgnoreElements$IgnoreElementsSubscriber.onSubscribe(MonoIgnoreElements.java:72)
		at reactor.core.publisher.FluxPeek$PeekSubscriber.onSubscribe(FluxPeek.java:171)
		at reactor.core.publisher.FluxDematerialize$DematerializeSubscriber.onSubscribe(FluxDematerialize.java:77)
		at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:165)
		at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:87)
		at reactor.core.publisher.Mono.subscribe(Mono.java:4399)
		at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.subscribeNext(MonoIgnoreThen.java:255)
		at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:51)
		at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:157)
		at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816)
		at reactor.core.publisher.MonoCollectList$MonoCollectListSubscriber.onComplete(MonoCollectList.java:128)
		at reactor.core.publisher.DrainUtils.postCompleteDrain(DrainUtils.java:132)
		at reactor.core.publisher.DrainUtils.postComplete(DrainUtils.java:187)
		at reactor.core.publisher.FluxMaterialize$MaterializeSubscriber.onComplete(FluxMaterialize.java:141)
		at reactor.core.publisher.FluxTake$TakeSubscriber.onComplete(FluxTake.java:153)
		at reactor.core.publisher.FluxTake$TakeSubscriber.onNext(FluxTake.java:133)
		at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
		at reactor.core.publisher.SerializedSubscriber.onNext(SerializedSubscriber.java:99)
		at reactor.core.publisher.FluxTimeout$TimeoutMainSubscriber.onNext(FluxTimeout.java:180)
		at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1816)
		at reactor.core.publisher.MonoCollectList$MonoCollectListSubscriber.onComplete(MonoCollectList.java:128)
		at org.springframework.cloud.commons.publisher.FluxFirstNonEmptyEmitting$FirstNonEmptyEmittingSubscriber.onComplete(FluxFirstNonEmptyEmitting.java:325)
		at reactor.core.publisher.FluxSubscribeOn$SubscribeOnSubscriber.onComplete(FluxSubscribeOn.java:166)
		at reactor.core.publisher.FluxIterable$IterableSubscription.fastPath(FluxIterable.java:362)
		at reactor.core.publisher.FluxIterable$IterableSubscription.request(FluxIterable.java:227)
		at reactor.core.publisher.FluxSubscribeOn$SubscribeOnSubscriber.requestUpstream(FluxSubscribeOn.java:131)
		at reactor.core.publisher.FluxSubscribeOn$SubscribeOnSubscriber.onSubscribe(FluxSubscribeOn.java:124)
		at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:165)
		at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:87)
		at reactor.core.publisher.Flux.subscribe(Flux.java:8468)
		at reactor.core.publisher.FluxFlatMap.trySubscribeScalarMap(FluxFlatMap.java:200)
		at reactor.core.publisher.MonoFlatMapMany.subscribeOrReturn(MonoFlatMapMany.java:49)
		at reactor.core.publisher.FluxFromMonoOperator.subscribe(FluxFromMonoOperator.java:76)
		at reactor.core.publisher.FluxSubscribeOn$SubscribeOnSubscriber.run(FluxSubscribeOn.java:194)
		at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84)
		at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37)
		at java.util.concurrent.FutureTask.run(FutureTask.java:266)
		at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
		at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
		at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
		at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
		at java.lang.Thread.run(Thread.java:748)
原因分析

在LoadBalancerUriTools类下打印日志:

2023-01-16 15:06:10,443 [boundedElastic-1] INFO [o.s.cloud.client.loadbalancer.LoadBalancerUriTools] LoadBalancerUriTools.java:106 - LoadBalance的host:2400:379:1200:0:0:0:0:35
2023-01-16 15:06:10,444 [boundedElastic-1] INFO [o.s.cloud.client.loadbalancer.LoadBalancerUriTools] LoadBalancerUriTools.java:62 - getRawQuery:password=Ti%231234&grant_type=password&scope=server&username=lihc
2023-01-16 15:06:10,444 [boundedElastic-1] INFO [o.s.cloud.client.loadbalancer.LoadBalancerUriTools] LoadBalancerUriTools.java:63 - getRawPath:/oauth/token
2023-01-16 15:06:10,444 [boundedElastic-1] INFO [o.s.cloud.client.loadbalancer.LoadBalancerUriTools] LoadBalancerUriTools.java:64 - getRawFragment:null

URI里面带有%的会进行编码,编码的时候IPV6环境下会使用
org.springframework.web.util.HierarchicalUriComponents#getHostType()的方法
图示为具体源码,判断是否为IPV6过于粗糙了(判断以’['开始,但是注册到NACOS里的地址是不带[)

private Type getHostType() {return (this.host != null && this.host.startsWith("[") ? Type.HOST_IPV6 : Type.HOST_IPV4);
	}

目前发现需要解决两种IPV6的形式
1、浏览器访问http://[2400:379:1200::30]/
2、NACOS注册的地址 2400:379:1200:0:0:0:0:35

解决方案
1、引入工具类
2、重写方法
private Type getHostType() {return (this.host != null && InetAddressValidator.isIPv6Address(this.host) ? Type.HOST_IPV6 : Type.HOST_IPV4);
	}

引入的新工具类

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站名称:SpringGateway在IPV6环境发生请求的异常-创新互联
链接地址:http://cqcxhl.cn/article/phigi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP