websocket.adoc 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. [[nsa-websocket-security]]
  2. = WebSocket Security
  3. Spring Security 4.0+ provides support for authorizing messages.
  4. One concrete example of where this is useful is to provide authorization in WebSocket based applications.
  5. [[nsa-websocket-message-broker]]
  6. == <websocket-message-broker>
  7. The `<websocket-message-broker>` element has two different modes.
  8. If the <<nsa-websocket-message-broker-id,`websocket-message-broker@id`>> is not specified, it does the following things:
  9. * Ensure that any `SimpAnnotationMethodMessageHandler` has the `AuthenticationPrincipalArgumentResolver` registered as a custom argument resolver.
  10. This allows the use of `@AuthenticationPrincipal` to resolve the principal of the current `Authentication`.
  11. * Ensures that the `SecurityContextChannelInterceptor` is automatically registered for the `clientInboundChannel`.
  12. This populates the `SecurityContextHolder` with the user that is found in the message.
  13. * Ensures that a `CsrfChannelInterceptor` is registered with the `clientInboundChannel`.
  14. This allows authorization rules to be specified for a message.
  15. * Ensures that a CsrfChannelInterceptor is registered with the clientInboundChannel.
  16. This ensures that only requests from the original domain are enabled.
  17. * Ensures that a `CsrfTokenHandshakeInterceptor` is registered with a `WebSocketHttpRequestHandler`, a `TransportHandlingSockJsService`, or a `DefaultSockJsService`.
  18. This ensures that the expected `CsrfToken` from the `HttpServletRequest` is copied into the WebSocket Session attributes.
  19. If additional control is necessary, you can specify the ID, and a `ChannelSecurityInterceptor` is assigned to the specified ID.
  20. You can then manually wire Spring's messaging infrastructure.
  21. This is more cumbersome, but doing so provides greater control over the configuration.
  22. [[nsa-websocket-message-broker-attributes]]
  23. === <websocket-message-broker> Attributes
  24. The `<websocket-message-broker>` element has the following attributes:
  25. [[nsa-websocket-message-broker-id]]
  26. `id`::
  27. A bean identifier, used to refer to the `ChannelSecurityInterceptor` bean elsewhere in the context.
  28. If specified, Spring Security requires explicit configuration within Spring Messaging.
  29. If not specified, Spring Security automatically integrates with the messaging infrastructure, as described in <<nsa-websocket-message-broker>>
  30. [[nsa-websocket-message-broker-same-origin-disabled]]
  31. `same-origin-disabled`::
  32. Disables the requirement for a CSRF token to be present in the Stomp headers.
  33. Default: `false`
  34. Changing the default lets other origins make SockJS connections.
  35. [[nsa-websocket-message-broker-children]]
  36. === Child Elements of <websocket-message-broker>
  37. The `<websocket-message-broker>` element has the following child elements:
  38. * xref:servlet/appendix/namespace/http.adoc#nsa-expression-handler[expression-handler]
  39. * <<nsa-intercept-message,intercept-message>>
  40. [[nsa-intercept-message]]
  41. == <intercept-message>
  42. The `<intercept-message>` defines an authorization rule for a message.
  43. [[nsa-intercept-message-parents]]
  44. === Parent Elements of <intercept-message>
  45. The parent element of the `<intercept-message>` element is the <<nsa-websocket-message-broker,`websocket-message-broker`>> element.
  46. [[nsa-intercept-message-attributes]]
  47. === <intercept-message> Attributes
  48. The `<intercept-message>` element has the following attributes:
  49. [[nsa-intercept-message-pattern]]
  50. `pattern`::
  51. An Ant-based pattern that matches on the message destination.
  52. For example, `/**` matches any message with a destination, while `/admin/**` matches any message that has a destination that starts with `/admin/`.
  53. [[nsa-intercept-message-type]]
  54. `type`::
  55. The type of message to match on.
  56. SimpMessageType defines the valid values: `CONNECT`, `CONNECT_ACK`, `HEARTBEAT`, `MESSAGE`, `SUBSCRIBE`, `UNSUBSCRIBE`, `DISCONNECT`, `DISCONNECT_ACK`, and `OTHER`).
  57. [[nsa-intercept-message-access]]
  58. `access`::
  59. The expression used to secure the message.
  60. Here are some examples:
  61. +
  62. * `denyAll`: Denies access to all of the matching messages.
  63. * `permitAll`: Grants access to all of the matching Messages.
  64. * `hasRole('ADMIN')`: Requires the current user to have a role of `ROLE_ADMIN` for the matching messages.