在我的Web应用程序中,加密不是由应用程序服务器进行的,而是由反向代理进行的。我想保持这种状态。 相关技术栈如下:
- 应用服务器:payara 5.181 full Java EE 8/JSF 2.3;莫贾拉2.4.0-m01.payara-p5
由于代理加密,应用程序服务器被配置为不在 web.xml 中使用加密:
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
现在,websocket 连接无法与主动代理一起使用,因为它是由客户端以非加密方式 (ws://...) 建立的。当内容已经以 https 形式传递时,浏览器会阻止此操作。
根据我的理解,浏览器和应用程序服务器的行为似乎都是正确的。虽然浏览器避免降级,但 websocket 规范第 8.3 节“传输保证”指出:
A transport guarantee of NONE must be interpreted by the container as allowing unencrypted ws:// connections to the websocket [WSC-8.3-1]
尽管在应用服务器上使用传输保证 NONE,但是否有办法强制对 f:websocket 进行加密(使用 wss://)?或者有其他设置或更好的方法吗?
目前,我无法找到一种方法来适应 f:websocket 的行为,不得不切换回使用纯 javascript websocket 与经典的 websocket serverendpoint 结合使用。
最佳答案
Is there a way to force encryption (the use of wss://) for the f:websocket, although using transport guarantee NONE on the app server?
websocket URL 通过 ExternalContext#encodeWebsocketURL()
编码。与 JSF 中的惯例一样,几乎所有内容都可以通过工厂进行修饰甚至替换,ExternalContext
也是如此。
下面是一个启动示例,它将 websocket URL 中的 ws://
盲目替换为 wss://
。
public class CustomExternalContext extends ExternalContextWrapper {
public CustomExternalContext(ExternalContext wrapped) {
super(wrapped);
}
@Override
public String encodeWebsocketURL(String url) {
return super.encodeWebsocketURL(url).replaceFirst("ws://", "wss://");
}
public static class Factory extends ExternalContextFactory {
public Factory(ExternalContextFactory wrapped) {
super(wrapped);
}
@Override
public ExternalContext getExternalContext(Object context, Object request, Object response) throws FacesException {
return new CustomExternalContext(getWrapped().getExternalContext(context, request, response));
}
}
}
为了让它运行,请在faces-config.xml
中按如下方式注册它:
<factory>
<external-context-factory>com.example.CustomExternalContext$Factory</external-context-factory>
</factory>
关于JSF 2.3 f :websocket encryption setting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49657004/