검색

3.5. 패킷을 가로채서 메시지 감사

download PDF

브로커로 들어오거나 종료하는 패킷을 가로채서 패킷을 감사하거나 메시지를 필터링합니다. 인터셉터는 가로채는 패킷을 변경합니다. 이로 인해 인터셉터가 강력하지만 잠재적으로 위험할 수 있습니다.

비즈니스 요구 사항을 충족하기 위해 인터셉터를 개발합니다. 인터셉터는 프로토콜 고유의 프로토콜이며 적절한 인터페이스를 구현해야 합니다.

인터셉터는 부울 값을 반환하는 intercept() 메서드를 구현해야 합니다. 값이 true 이면 메시지 패킷이 계속됩니다. false 인 경우 프로세스가 중단되고 다른 인터셉터가 호출되지 않으며 메시지 패킷이 더 이상 처리되지 않습니다.

3.5.1. 인터셉터 생성

인터셉터는 가로채는 패킷을 변경할 수 있습니다. 자체 수신 및 발신 인터셉터를 생성할 수 있습니다. 모든 인터셉터는 프로토콜별 프로토콜이며 각각 서버를 입력하거나 종료하는 모든 패킷에 대해 호출됩니다. 이를 통해 패킷 감사와 같은 비즈니스 요구 사항을 충족하기 위해 인터셉터를 생성할 수 있습니다.

인터셉터 및 해당 종속 항목은 브로커의 Java 클래스 경로에 배치해야 합니다. 기본적으로 클래스 경로의 일부이므로 <broker_instance_dir> /lib 디렉터리를 사용할 수 있습니다.

다음 예제에서는 전달된 각 패킷의 크기를 확인하는 인터셉터를 만드는 방법을 보여줍니다.

참고

예제에서는 각 프로토콜에 대한 특정 인터페이스를 구현합니다.

프로세스

  1. 적절한 인터페이스를 구현하고 인터셉터 () 메서드를 재정의합니다.

    1. AMQP 프로토콜을 사용하는 경우 org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor 인터페이스를 구현합니다.

      package com.example;
      
      import org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage;
      import org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements AmqpInterceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        public boolean intercept(final AMQPMessage message, RemotingConnection connection)
        {
          int size = message.getEncodeSize();
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This AMQPMessage has an acceptable size.");
            return true;
          }
          return false;
        }
      }
    2. 코어 프로토콜을 사용하는 경우 인터셉터는 org.apache.artemis.activemq.api.core.Interceptor 인터페이스를 구현해야 합니다.

      package com.example;
      
      import org.apache.artemis.activemq.api.core.Interceptor;
      import org.apache.activemq.artemis.core.protocol.core.Packet;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements Interceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        boolean intercept(Packet packet, RemotingConnection connection)
        throws ActiveMQException
        {
          int size = packet.getPacketSize();
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This Packet has an acceptable size.");
            return true;
          }
          return false;
        }
      }
    3. MQTT 프로토콜을 사용하는 경우 org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor 인터페이스를 구현합니다.

      package com.example;
      
      import org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor;
      import io.netty.handler.codec.mqtt.MqttMessage;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements Interceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        boolean intercept(MqttMessage mqttMessage, RemotingConnection connection)
        throws ActiveMQException
        {
          byte[] msg = (mqttMessage.toString()).getBytes();
          int size = msg.length;
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This MqttMessage has an acceptable size.");
            return true;
          }
          return false;
        }
      }
    4. STOMP 프로토콜을 사용하는 경우 org.apache.activemq.artemis.core.protocol.stompFrameInterceptor 인터페이스를 구현합니다.

      package com.example;
      
      import org.apache.activemq.artemis.core.protocol.stomp.StompFrameInterceptor;
      import org.apache.activemq.artemis.core.protocol.stomp.StompFrame;
      import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
      
      public class MyInterceptor implements Interceptor
      {
        private final int ACCEPTABLE_SIZE = 1024;
      
        @Override
        boolean intercept(StompFrame stompFrame, RemotingConnection connection)
        throws ActiveMQException
        {
          int size = stompFrame.getEncodedSize();
          if (size <= ACCEPTABLE_SIZE) {
            System.out.println("This StompFrame has an acceptable size.");
            return true;
          }
          return false;
        }
      }

3.5.2. 인터셉터를 사용하도록 브로커 구성

사전 요구 사항

  • 인터셉터 클래스를 생성하고 브로커의 Java 클래스 경로에 추가(및 해당 종속 항목)합니다. 기본적으로 클래스 경로의 일부이므로 <broker_instance_dir> /lib 디렉터리를 사용할 수 있습니다.

프로세스

  1. Open <broker_instance_dir>/etc/broker.xml
  2. <broker _instance_dir> /etc/broker.xml에 구성을 추가하여 인터셉터를 사용하도록 브로커를 구성합니다.

    1. 인터셉터가 들어오는 메시지를 대상으로 하는 경우 해당 클래스 이름을 remoting- incoming-interceptors 목록에 추가합니다.

      <configuration>
        <core>
          ...
          <remoting-incoming-interceptors>
             <class-name>org.example.MyIncomingInterceptor</class-name>
          </remoting-incoming-interceptors>
          ...
        </core>
      </configuration>
    2. 인터셉터가 발신 메시지를 위한 경우 해당 class-nameremoting-outgoing-interceptors 목록에 추가합니다.

      <configuration>
        <core>
          ...
          <remoting-outgoing-interceptors>
             <class-name>org.example.MyOutgoingInterceptor</class-name>
          </remoting-outgoing-interceptors>
        </core>
      </configuration>

3.5.3. 클라이언트 측의 인터셉터

클라이언트는 인터셉터를 사용하여 클라이언트가 서버로 전송하거나 서버에 클라이언트에 의해 전송된 패킷을 가로챌 수 있습니다. 브로커 측 인터셉터가 false 값을 반환하는 경우 다른 인터셉터가 호출되지 않으며 클라이언트는 패킷을 추가로 처리하지 않습니다. 이 프로세스는 발신 패킷이 차단 방식으로 전송되지 않는 한 투명하게 수행됩니다. 이 경우 ActiveMQException 이 호출자에게 발생합니다. throw된 ActiveMQException 에는 false 값을 반환하는 인터셉터의 이름이 포함됩니다.

서버에서 클라이언트 인터셉터 클래스와 해당 종속 항목을 올바르게 인스턴스화하고 호출하려면 클라이언트의 Java 클래스 경로에 추가해야 합니다.

Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.