0Pricing
WebSockets & Real-Time Systems with Spring · レッスン

Spring SecurityによるSTOMPエンドポイントの保護

SpringでSTOMPメッセージングを認証・認可し、ハンドシェイク、宛先、ユーザーごとのメッセージを不正アクセスから保護する方法を学びます。

「Spring SecurityによるSTOMPエンドポイントの保護」はCoddyKit上の無料WebSockets & Real-Time Systems with Springレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWebSockets & Real-Time Systems with Spring学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Secure STOMP?

An open STOMP endpoint lets anyone subscribe to and publish on any destination. Securing STOMP ensures only authenticated users connect and only authorized users access specific destinations.

This lesson layers Spring Security onto STOMP messaging.

Two Layers of Security

STOMP security operates at two levels:

  • Handshake: authenticate the user when the WebSocket connection opens
  • Message: authorize each SUBSCRIBE and SEND to a destination

Both layers are needed for real protection.

Authenticating the Handshake

The connection should carry the user's identity. With session-based auth, Spring Security propagates the HTTP session principal into the WebSocket session automatically.

Token Auth on CONNECT

For token-based auth, read the token from the STOMP CONNECT frame headers using a ChannelInterceptor and set the authenticated principal on the message.

@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
    StompHeaderAccessor acc = StompHeaderAccessor.wrap(message);
    if (StompCommand.CONNECT.equals(acc.getCommand())) {
        String token = acc.getFirstNativeHeader('Authorization');
        Authentication user = tokenService.validate(token);
        acc.setUser(user);
    }
    return message;
}

Authorizing Destinations

Spring provides AbstractSecurityWebSocketMessageBrokerConfigurer to define which roles may access which destinations, similar to HTTP security rules.

@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
    messages
        .simpDestMatchers('/app/admin/**').hasRole('ADMIN')
        .simpSubscribeDestMatchers('/topic/public').permitAll()
        .anyMessage().authenticated();
}

Per-User Destinations

The /user/** prefix routes messages to a single user's private queue. Spring resolves these against the authenticated principal, so users only receive their own messages.

// Server side: send to a specific user
messagingTemplate.convertAndSendToUser(
    username, '/queue/notifications', payload);

CSRF Considerations

The WebSocket handshake is an HTTP request and can be subject to CSRF. Validate origins and, where applicable, require a CSRF token so attackers cannot open connections from malicious pages.

Restricting Origins

Always lock down allowed origins for the STOMP endpoint. An open origin policy lets any website connect on behalf of a logged-in user.

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint('/ws')
            .setAllowedOrigins('https://app.example.com')
            .withSockJS();
}

Validating Message Payloads

Authentication is not enough; validate the content of every message. Reject oversized payloads, unexpected fields, and malformed data to prevent injection and resource exhaustion.

  • Enforce size limits
  • Validate against a schema
  • Reject unknown destinations

Logging Security Events

Log failed connections, denied subscriptions, and authorization failures. These events feed monitoring and help detect abuse or probing of your messaging layer.

Defense in Depth

Combine handshake auth, destination authorization, origin restriction, and payload validation. No single control is sufficient; layered controls keep your real-time messaging secure even if one fails.

Quick Check

Test your understanding of STOMP security.

Recap

You learned to secure STOMP at two layers: authenticate the handshake (session or token), then authorize destinations with Spring Security rules. Per-user destinations, origin restrictions, payload validation, and event logging together provide defense in depth for real-time messaging.

よくある質問

「Spring SecurityによるSTOMPエンドポイントの保護」レッスンは無料ですか?

はい。「Spring SecurityによるSTOMPエンドポイントの保護」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebSockets & Real-Time Systems with Springコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebSockets & Real-Time Systems with Springコースには全4レッスンが含まれています。

「Spring SecurityによるSTOMPエンドポイントの保護」で何を学びますか?

SpringでSTOMPメッセージングを認証・認可し、ハンドシェイク、宛先、ユーザーごとのメッセージを不正アクセスから保護する方法を学びます。 ブラウザで直接実行するハンズオンコードでWebSockets & Real-Time Systems with Springを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

WebSockets & Real-Time Systems with Springを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWebSockets & Real-Time Systems with Springは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「Spring SecurityによるSTOMPエンドポイントの保護」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWebSockets & Real-Time Systems with Springレッスンでコードを書いて実行できますか?

はい。すべてのWebSockets & Real-Time Systems with Springレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. STOMPプロトコルの概要
  2. SpringでのSTOMP設定
  3. STOMPメッセージの送受信
  4. Spring SecurityによるSTOMPエンドポイントの保護
← WebSockets & Real-Time Systems with Springに戻る