我也跟着上设置与Java EE的WebSocket的端点本教程:的WebSocket安全 - 安全的一个WebSocket的Java EE中
由于显而易见的原因有一些更多的工作有关的安全性做( 例如没有SSL和访问限制/认证)。
所以我的目标是使用SSL通过
- 改善的WebSocket安全(WSS://而不是WS://) - 完成
- 设置用户认证(web.xml中) - 完成
- 执行SSL通信(web.xml)中 - 进行
- 确保与令牌(有限的寿命)
我的问题:如何验证我在ServerEndpoint的LoginBean中创建的令牌?
Bonus问题:我是否错过了在Java EE中确保websockets安全的一些重要部分?
这是我到目前为止有:
ServerEndpoint
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/user/endpoint/{token}")
public class ThisIsTheSecuredEndpoint {
@OnOpen
public void onOpen(@PathParam("token") String incomingToken,
Session session) throws IOException {
//How can i check if the token is valid?
}
}
LoginBean
@ManagedBean
@SessionScoped
public class LoginBean {
public String login() {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest();
try {
request.login("userID", "password");
HttpSession session = request.getSession();
// here we put the token in the session
session.setAttribute("token", "someVeeeeryLongRandomValue123hfgrtwpqllkiw");
} catch (ServletException e) {
facesContext.addMessage(null, new FacesMessage("Login failed."));
return "error";
}
return "home";
}
}
javascipt的
这是我想用来连接到WebSocket的代码:
// use SSL
// retrive the token from session via EL-expression #{session.getAttribute("token")}
var wsUri = "wss://someHost.com/user/endpoint/#{session.getAttribute("token")}";
var websocket = new WebSocket(wsUri);
websocket.onerror = function(evt) { onError(evt) };
function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
// For testing purposes
var output = document.getElementById("output");
websocket.onopen = function(evt) { onOpen(evt) };
function writeToScreen(message) {
output.innerHTML += message + "<br>";
}
function onOpen() {
writeToScreen("Connected to " + wsUri);
}
网络的XML:
用固定 “/用户/ *” 目录登录并执行SSL通信
<security-constraint>
...
<web-resource-name>Secured Area</web-resource-name>
<url-pattern>pathToSecuredDicrtoy</url-pattern>
...
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
...
</security-constraint>
<login-config>
<auth-method>FORM</auth-method> ...
</login-config>
备注:我使用JSF
任何反馈将不胜感激。
为了使我加入' JSP servlet的名称> *的.js URL模式> servlet的映射>' JavaScript的EL评价工作 到'web.xml'对于我的用例来说这没问题,因为页面的访问速度非常低。 –