2 Security
Ralph Schaer edited this page 2026-10-30 16:08:52 +01:00

Security

wamp2spring integrates with Spring Security through dedicated servlet and reactive modules.

What the security modules provide

  • a Spring Security context interceptor that reads the transport's Authentication from WAMP message headers
  • an AuthenticationPrincipalArgumentResolver for handler methods
  • Spring Security authorization rules for inbound WAMP messages
  • destination-aware matching for procedures and topics

Artifacts

  • wamp2spring-security-servlet
  • wamp2spring-security-reactive

Both rely on the shared wamp2spring-security-core support.

Configurer base classes

Use one of these base classes to define rules:

  • AbstractSecurityWampServletConfigurer
  • AbstractSecurityWampReactiveConfigurer

Each base class adds the security context interceptor and lets you configure inbound authorization rules through configureInbound.

Enable the appropriate WAMP transport as well. HTTP authentication and endpoint access remain part of your application's servlet SecurityFilterChain or reactive SecurityWebFilterChain configuration.

Example

import org.springframework.context.annotation.Configuration;

import ch.rasc.wamp2spring.security.WampMessageSecurityMetadataSourceRegistry;
import ch.rasc.wamp2spring.security.servlet.AbstractSecurityWampServletConfigurer;

@Configuration
public class WampSecurityConfiguration extends AbstractSecurityWampServletConfigurer {

  @Override
  protected void configureInbound(WampMessageSecurityMetadataSourceRegistry messages) {
    messages.callMessage("admin.shutdown").hasRole("ADMIN");
    messages.publishMessage("audit.events").hasAuthority("SCOPE_audit.write");
    messages.subscribeMessage("public.topic").permitAll();
    messages.anyMessage().authenticated();
  }

}

Matching model

Security rules can target:

  • any WAMP message
  • register messages
  • call messages
  • subscribe messages
  • publish messages
  • procedure destinations
  • topic destinations

String arguments use exact matching. For prefix or wildcard rules, pass new DestinationMatch("app.admin.", MatchPolicy.PREFIX) (imports from ch.rasc.wamp2spring.config and ch.rasc.wamp2spring.pubsub). Prefix is a string prefix, and a wildcard uses an empty dot-separated component, not * or an Ant path pattern.

Rules are evaluated in registration order. Put specific destinations before anyMessage(). topicMatchers(DestinationMatch...) matches both publication and subscription; procedureMatchers(DestinationMatch...) matches both registration and call. Constraints include hasRole, hasAnyRole, hasAuthority, hasAnyAuthority, permitAll, denyAll, authenticated, fullyAuthenticated, anonymous, rememberMe, and access for an expression. hasRole("ADMIN") adds the ROLE_ prefix; do not pass it yourself.

Transport principal vs WAMP authentication

If the underlying transport already has an authenticated principal, wamp2spring can propagate it into the WAMP session.

WAMP authentication providers return a plain Java Principal. The modules do not convert it into Spring Security Authentication; a WAMP auth role is therefore not automatically a Spring Security role. Use the core authorizer below for WAMP identities, or supply an application bridge to Spring Security. @AuthenticationPrincipal resolves from the Spring Security context, while an ordinary Principal handler parameter resolves the WAMP message's principal.

For protocol-level authentication providers and handshake flow, see Authentication.

Core WAMP authorization

Register one or more WampAuthorizer beans to authorize REGISTER, CALL, SUBSCRIBE, and PUBLISH, without requiring Spring Security. The context exposes the action, original message, URI, match policy, principal, auth ID, and auth role. All authorizers must allow an operation; the first denial stops routing. With no authorizers, this layer permits operations.

For example, restrict the session-termination meta procedures:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ch.rasc.wamp2spring.WampError;
import ch.rasc.wamp2spring.authorization.WampAuthorizer;
import ch.rasc.wamp2spring.authorization.WampAuthorizationAction;
import ch.rasc.wamp2spring.authorization.WampAuthorizationDecision;

@Configuration
public class WampAuthorizationConfiguration {
  @Bean
  public WampAuthorizer sessionAdministration() {
    return context -> {
      String uri = context.getUri();
      if (context.getAction() == WampAuthorizationAction.CALL
          && uri != null && uri.startsWith("wamp.session.kill")
          && !"admin".equals(context.getAuthRole())) {
        return WampAuthorizationDecision.deny(WampError.NOT_AUTHORIZED);
      }
      return WampAuthorizationDecision.allow();
    };
  }
}

Return deny(WampError.NOT_AUTHORIZED) or deny(WampError.AUTHORIZATION_FAILED) to produce the corresponding WAMP operation error. Server-originated publications also pass through broker authorizers, including meta-event publications, and normally have no client principal. Account for those publications in a policy that requires authentication.

Meta procedures do not enforce an administrator role by themselves. Protect administrative calls and sensitive inspection/history APIs with your rules. Realms do not isolate any of these resources.

Source: security registry, authorization SPI.