使用 jfinal undertow 客户端连接总出现这个错误!

使用 jfinal undertow 客户端连接总出现这个错误!查了cookie也没有存中文字符呀

2019-09-19 10:34:01[ERROR]-[Thread: XNIO-1 task-29]-[io.undertow.servlet.api.LoggingExceptionHandler.handleThrowable()]: UT005023: Exception handling request to /websocket.ws

java.lang.IllegalArgumentException: UT000173: An invalid control character [179] was present in the cookie value or attribute

at io.undertow.util.LegacyCookieSupport.isHttpSeparator(LegacyCookieSupport.java:149)

at io.undertow.util.Cookies.parseCookie(Cookies.java:274)

at io.undertow.util.Cookies.parseRequestCookies(Cookies.java:216)

at io.undertow.util.Cookies.parseRequestCookies(Cookies.java:206)

at io.undertow.util.Cookies.parseRequestCookies(Cookies.java:202)

at io.undertow.server.HttpServerExchange.getRequestCookies(HttpServerExchange.java:1128)

at io.undertow.server.session.SessionCookieConfig.findSessionId(SessionCookieConfig.java:84)

at io.undertow.servlet.spec.SessionCookieConfigImpl.findSessionId(SessionCookieConfigImpl.java:61)

at io.undertow.servlet.spec.ServletContextImpl$ServletContextSessionConfig.findSessionId(ServletContextImpl.java:1225)

at io.undertow.server.session.InMemorySessionManager.getSession(InMemorySessionManager.java:215)

at io.undertow.servlet.spec.ServletContextImpl.getSession(ServletContextImpl.java:858)

at io.undertow.servlet.spec.ServletContextImpl.getSession(ServletContextImpl.java:938)

at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:69)

at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)

at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)

at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)

at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)

at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)

at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)

at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)

at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)

at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)

at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)

at io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)

at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)


评论区

JFinal

2019-09-19 10:56

cookie 规范有些字符是不允许存放的:
https://blog.csdn.net/iteye_7245/article/details/81931128

所以你碰到了这个异常:
java.lang.IllegalArgumentException: UT000173: An invalid control character [179] was present in the cookie value or attribute

解决办法很简单,不要存放这些字符,或者存之前进行转码

不建议在 cookie 中存放不支持的字符

scocai

2019-09-19 14:48

跟我这里的代码有关吗?

public class GetCookieConfigurator extends Configurator{

@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
Map> headers = request.getHeaders();
List cookieList = headers.get("Cookie");
Map kv = new HashMap<>();
if(cookieList!=null) {
String cookieStr = cookieList.get(0);
String[] cookieArray = cookieStr.split("; ");
for(String cookie : cookieArray){
System.out.println("websocket cookie:"+cookie);
String[] cookieKv = cookie.split("=");
if(cookieKv.length==2) {
kv.put(cookieKv[0], cookieKv[1]);
}
}
}
String sessionId = kv.get(LoginService.sessionIdName);
sec.getUserProperties().put(String.class.getName(),sessionId);

}


}

JFinal

2019-09-19 15:49

@scocai 单步调试一下,看往 cookie 中存放的是什么数据出错的,然后改一改就可以了

scocai

2019-09-19 16:56

没法调试呀,本地测试没有问题,主要是客户使用后出现。

zzufish

2019-09-19 17:47

安卓端登录时也出现过类似问题