Package io.goobi.viewer.websockets
Class WebSocketTools
java.lang.Object
io.goobi.viewer.websockets.WebSocketTools
Shared guards for
ServerEndpoint @OnOpen handlers.
Provides three building blocks every endpoint that should restrict its caller needs:
requireUser(HttpSession, Session)- enforce that the captured HTTP session contains a logged-inUser, close the WebSocket otherwise.requireAllowedOrigin(EndpointConfig, Session)- validate theOriginheader captured byGetHttpSessionConfiguratoragainst the same allowlist used byCSRFRequestFilter; only enforced whenConfiguration.isWebSocketOriginValidationEnabled()is on. Decoupled from the REST CSRF switch so the two transports can be hardened independently.closeSession(Session, CloseCode, String)- best-effort polite close used by the guards above and by callers that detect a fatal handshake-time condition (unknown resource etc.).
Origin validation is gated by webapi.websocket.originValidation[@enabled],
separate from the REST webapi.csrf[@enabled] so operators can harden each
transport independently. The user check is always enforced when the endpoint calls it.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringKey under whichGetHttpSessionConfiguratorstashes theOriginheader. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseSession(Session session, CloseReason.CloseCode code, String reason) Best-effort polite close.static booleanrequireAllowedOrigin(EndpointConfig config, Session ws) Validates theOriginheader captured at handshake time againstConfiguration.getViewerBaseUrl()andConfiguration.getCsrfAdditionalAllowedOrigins().static UserrequireUser(HttpSession httpSession, Session ws) Returns theUserbound tohttpSession, or closeswswithCloseReason.CloseCodes.VIOLATED_POLICYand returnsnullif no user is present.
-
Field Details
-
ORIGIN_PROPERTY
Key under whichGetHttpSessionConfiguratorstashes theOriginheader.- See Also:
-
-
Method Details
-
closeSession
Best-effort polite close. SwallowsIOException- the peer may already be gone, and there is nothing useful the caller can do at that point.- Parameters:
session- WebSocket session to closecode- close code to sendreason- short human-readable reason (will be truncated by the container if >123 bytes)
-
requireUser
Returns theUserbound tohttpSession, or closeswswithCloseReason.CloseCodes.VIOLATED_POLICYand returnsnullif no user is present. Callers shouldreturnfrom@OnOpenimmediately when the result isnull.- Parameters:
httpSession- HTTP session captured byGetHttpSessionConfiguratorws- WebSocket session to close on failure- Returns:
- the authenticated
Userornullif the socket was closed
-
requireAllowedOrigin
Validates theOriginheader captured at handshake time againstConfiguration.getViewerBaseUrl()andConfiguration.getCsrfAdditionalAllowedOrigins(). No-op (returnstrue) whenConfiguration.isWebSocketOriginValidationEnabled()isfalse.- Parameters:
config- endpoint config, expected to carry theORIGIN_PROPERTYvaluews- WebSocket session to close on failure- Returns:
trueif the origin is allowed (or validation is disabled);falseif the socket was closed
-