Class SignedEvent

java.lang.Object
cloud.opencode.base.event.Event
cloud.opencode.base.event.security.SignedEvent
All Implemented Interfaces:
VerifiableEvent

public abstract class SignedEvent extends Event implements VerifiableEvent
Signed Event Base Class 签名事件基类

Abstract base class for events that include cryptographic signatures.

包含加密签名的事件的抽象基类。

Features | 主要功能:

  • HMAC-SHA256 signature - HMAC-SHA256签名
  • Automatic signing on creation - 创建时自动签名
  • Signature verification - 签名验证

Usage Examples | 使用示例:

public class SecureOrderEvent extends SignedEvent {
    private final Order order;

    public SecureOrderEvent(Order order, String secret) {
        super(secret);
        this.order = order;
    }

    @Override
    protected String getPayload() {
        return order.getId().toString();
    }
}

// Create and verify
SecureOrderEvent event = new SecureOrderEvent(order, secretKey);
if (event.verify(secretKey)) {
    // Event is authentic
}

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
Since:
JDK 25, opencode-base-event V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • SignedEvent

      protected SignedEvent(String secret)
      Create signed event with automatic signing 使用自动签名创建签名事件
      Parameters:
      secret - the secret key for signing | 用于签名的密钥
    • SignedEvent

      protected SignedEvent(String source, String secret)
      Create signed event with source and automatic signing 使用来源和自动签名创建签名事件
      Parameters:
      source - the event source | 事件来源
      secret - the secret key for signing | 用于签名的密钥
  • Method Details

    • getPayload

      protected abstract String getPayload()
      Get the payload data for signing 获取用于签名的负载数据

      Subclasses must implement this method to provide the data that should be included in the signature.

      子类必须实现此方法以提供应包含在签名中的数据。

      Returns:
      the payload string | 负载字符串
    • getSignature

      public String getSignature()
      Description copied from interface: VerifiableEvent
      Get the event signature 获取事件签名
      Specified by:
      getSignature in interface VerifiableEvent
      Returns:
      the signature string | 签名字符串
    • verify

      public boolean verify(String secret)
      Description copied from interface: VerifiableEvent
      Verify the event signature 验证事件签名
      Specified by:
      verify in interface VerifiableEvent
      Parameters:
      secret - the secret key used for verification | 用于验证的密钥
      Returns:
      true if signature is valid | 如果签名有效返回true