をWebSocketのセキュア:のWebSocketセキュリティ - 私は、Java EEでのWebSocketエンドポイントを設定するには、このチュートリアルに従ったJava EEで
明白な理由のために、セキュリティに関して行うことにいくつかのより多くの作業が(そこにあります、SSLなし、アクセス制限/認証など)。
だから私の目標は、SSL(WSS://ではなく、WSの://)を使用して
- でのWebSocketのセキュリティを向上させることである -
- セットアップユーザー真偽(web.xml)を行って - を行ってを
- は、SSL通信(web.xml)を強制 -
- を行うトークン(限られた寿命)
私の質問: LoginBeanで作成したトークンをServerEndpointで確認するにはどうすればよいですか?
ボーナス質問: Java EEのWebソケットを保護するための重要な部分がありますか?
これは私がこれまで持っているものです。
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を使用しています
フィードバックは高く評価されます。