【问题标题】:How to create Session-Id for Diameter Stack in java?如何在 java 中为 Diameter Stack 创建 Session-Id?
【发布时间】:2019-12-01 09:19:55
【问题描述】:

我想知道如何生成 Session-Id。有时 Diameter 接受没有会话 ID 的请求,所以我想为那些请求创建会话 ID,例如取消位置请求。

【问题讨论】:

  • 我们使用UUID.randomUUID().toString(),最简单的方法是生成唯一身份。如果性能很关键,那么您可以使用 UUID.randomUUID() 的内部组件构建一个数值型。

标签: java diameter-protocol


【解决方案1】:
public static String CreateSessionId()
{
    /*<DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]
      <high 32 bits> and <low 32 bits> are decimal representations of the
       high and low 32 bits of a monotonically increasing 64-bit value.  The
       64-bit value is rendered in two part to simplify formatting by 32-bit
       processors.  At startup, the high 32 bits of the 64-bit value MAY be
       initialized to the time, and the low 32 bits MAY be initialized to
       zero.  This will for practical purposes eliminate the possibility of
       overlapping Session-Ids after a reboot, assuming the reboot process
       takes longer than a second.  Alternatively, an implementation MAY
       keep track of the increasing value in non-volatile memory.

     * <optional value> is implementation specific but may include a modem's
       device Id, a layer 2 address, timestamp, etc.
       Example, in which there is no optional value:
          accesspoint7.acme.com;1876543210;523
       Example, in which there is an optional value:
          accesspoint7.acme.com;1876543210;523;mobile@200.1.1.88*/

    String SessionID = "";

    String hostIdentity;


    int low32 = Sequence.getNext();

    int high32 = 0;

    long id = ((long)low32) << 32 | high32;

    String optional = String.valueOf(Sequence.getNext());

    SessionID = hostIdentity + ";" + id + ";" + optional;

    return SessionID;
}

public final class Sequence
{
    private static int _value = -1;
    private static final Object m_lock = new Object();

    public static int getNext()
    {
        synchronized (m_lock)
        {
            if (_value == Integer.MAX_VALUE)
            {
                _value = -1;
            }
            return ++_value;
        }
    }
}

你可以试试这个

【讨论】:

  • 如果我理解正确,此解决方案可能会在重启后或 _value ==Integer.MAX_VALUE 后重复会话 ID。如果您有两次相同的会话 ID 用于不同的会话,您可能会遇到很大的问题。另请阅读 RFC 3588:“Session-Id 必须是全局且永远唯一的,因为它旨在唯一标识用户会话而不参考任何其他信息,并且可能需要将历史身份验证信息与会计信息相关联。”
  • 最好使用 DATE 而不是计数器
猜你喜欢
  • 2011-02-12
  • 1970-01-01
  • 2014-07-22
  • 2019-05-19
  • 2015-03-31
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多