How can I get a cookie value inside websocket end-point

纵然是瞬间 提交于 2019-12-05 06:03:57
Joakim Erdfelt

Access to request parameters is done via the @ServerEndpoint(configurator=MyConfigurator.class) technique.

See other answer on how to access the HttpSession, as its techniques are very similar.

While Joakim's answer does provide a hint in the right direction I believe it does not fully answer the question, or at least can be complemented.

In order to retrieve the value of a cookie you must get the headers of the HandshakeRequest object, and look for the header named "cookie". Your modifyHandshake implementation will look something like:

public class MyEndpointConfigurator extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig config, 
                                HandshakeRequest request, 
                                HandshakeResponse response)
    {
        Map<String,List<String>> headers = request.getHeaders();
        config.getUserProperties().put("cookie",headers.get("cookie"));
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!