Error message:

java.lang.IllegalArgumentException: a header value contains a prohibited character ‘\f’: Bearer error = “unauthorized” error_description = “& present ao8 present Tu ¡ X” at io.netty.handler.codec.http.DefaultHttpHeaders HeaderValueConverterAndValidator.validateValueChar(DefaultHttpHeaders.java:477)atio.netty.handler.codec.http.DefaultHttp HeadersHeaderValueConverterAndValidator.validateValueChar(DefaultHttpHeaders.java:477) at io.netty.handler.codec.http.DefaultHttpHeadersHeaderValueConverterAndValidator.validateValueChar(DefaultHttpHeaders.java :477)atio.netty.handler.codec.http.DefaultHttpHeadersHeaderValueConverterAndValidator.convertObject(DefaultHttpHeaders.j ava:453) at io.netty.handler.codec.http.DefaultHttpHeaders HeaderValueConverterAndValidator.convertObject(DefaultHttpHeaders.java:444)atio.netty.handler.codec.DefaultHeaders.addOb ject(DefaultHeaders.java:327)atio.netty.handler.codec.http.DefaultHttpHeaders.add(DefaultHttpHeaders.java:135)atio.netty .handler.codec.http.HttpObjectDecoder.readHeaders(HttpObjectDecoder.java:610)atio.netty.handler.codec.http.HttpObjectDec oder.decode(HttpObjectDecoder.java:257)atio.netty.handler.codec.http.HttpClientCodecHeaderValueConverterAndValidator.con vertObject(DefaultHttpHeaders.java:444) at io.netty.handler.codec.DefaultHeaders.addObject(DefaultHeaders.java:327) at io.netty.handler.codec.http.DefaultHttpHeaders.add(DefaultHttpHeaders.java:135) at io.netty.handler.codec.http.HttpObjectDecoder.readHeaders(HttpObjectDecoder.java:610) at io.netty.handler.codec.http.HttpObjectDecoder.decode(HttpObjectDecoder.java:257) at io.netty.handler.codec.http.HttpClientCodecHeaderValueConverterAndValidator.convertObject(DefaultHttpHeaders.java:444)at io.netty.handler.codec.DefaultHeaders.addObject(DefaultHeaders.java:327)atio.netty.handler.codec.http.DefaultHttpHeaders .add(DefaultHttpHeaders.java:135)atio.netty.handler.codec.http.HttpObjectDecoder.readHeaders(HttpObjectDecoder.java:610) atio.netty.handler.codec.http.HttpObjectDecoder.decode(HttpObjectDecoder.java:257)atio.netty.handler.codec.http.HttpClie ntCodecDecoder.decode(HttpClientCodec.java:225) at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:508) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:447) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.channel.DefaultChannelPipeline HeadContext.channelRead(DefaultChannelPipeline.java:1410)atio.netty.channel.AbstractChannelHandlerContext.invokeChannelR ead(AbstractChannelHandlerContext.java:379)atio.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractCh annelHandlerContext.java:365)atio.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)a tio.netty.channel.nio.AbstractNioByteChannelHeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioByteChannelHeadContext.channelRead(DefaultChannelPipeline.java:1410)atio.netty.channel.A bstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)atio.netty.channel.AbstractChannel HandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)atio.netty.channel.DefaultChannelPipeline.fireCh annelRead(DefaultChannelPipeline.java:919)atio.netty.channel.nio.AbstractNioByteChannelNioByteUnsafe.read(AbstractNioByt eChannel.java:166) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor 4.run(SingleThreadEventExecutor.java:989)atio.netty.util.internal.ThreadExecutorMap4.run(SingleThreadEventExecutor.java: 989) at io.netty.util.internal.ThreadExecutorMap4.run(SingleThreadEventExecutor.java:989)atio.netty.util.internal.ThreadExecutor Map2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.base/java.lang.Thread.run(Thread.java:834)

Investigation scene:

Throw New UserNotFoundException(” User information error, please contact “);

A header value contains a Prohibited character ‘\ F ‘: a header value contains a Prohibited character ‘\ F ‘: Bearer error = “unauthorized” error_description = “& present ao8 present Tu ¡ X”

2. Postman accessed the service alone and could not return the response body

Strangely, it is normal for abnormal packets to be “incorrect user name or password”, without Chinese garbled characters

Looked at a lot of source code, found

In fact, both scenes are caused by an error, in OAuth2 custom exception handling logic

There’s a piece of code that says:

if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
   headers.set("WWW-Authenticate", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
}
Copy the code

In LLDB etSummary(), the response body is faulty if the error message is [Chinese]

The above code logic is executed because I inherited the AuthenticationException exception from my custom exception

When www-authenticate is added [error=”unauthorized”, error_description=” User information error, please contact “], Chinese gargled characters will appear causing errors in the first and second phases

Preliminary solution:

Change the code to the following, the core is to use Base64 encryption transmission, avoid Chinese:

if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
    String encoded = Base64.getEncoder().encodeToString(e.getSummary().getBytes(StandardCharsets.UTF_8));
    headers.set("WWW-Authenticate", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, encoded));
}
Copy the code

Conclusion:

This problem is very hidden, it took a long time to find the problem of WWw-authenticate Chinese value