java中的SAML:timezone自动从Eastern转换为UTC

时间:2014-09-29 20:55:42

标签: java jodatime saml-2.0

我正在使用opensaml在java应用程序中创建SAML断言。但是<saml2:Conditions>中的NotBefore和NotOnOrAfter时间总是变回UTC时区,即使我在joda时间专门使用DateTimeZone dtZone = DateTimeZone.forID("America/New_York");

我再次尝试将其转换回EST,但即使在此之后,我仍然得到UTC:conditions.setNotBefore(conditions.getNotBefore().toDateTime(dtZone));

由于这是一个奇怪的时区冲突,SAML会因此而失效,从而产生安全错误 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

OASIS SAML 2.0 specification 强制要求断言中的时间戳被编码/规范化为UTC

  

2.5.1元素&lt;条件&gt;

     

元素可以包含以下元素和   属性:

     

NotBefore [可选]

     

指定最早的时刻   断言有效。时间值以UTC编码,如   在1.3.3节中描述。

     

NotOnOrAfter [可选]

     

指定时间   断言已过期的瞬间。时间值被编码   在UTC中,如第1.3.3节所述。

...在规范的前面:

  

1.3.3时间价值

     

所有SAML时间值都具有类型xs:dateTime,它内置于W3C XML Schema数据类型规范中   [Schema2],必须以UTC格式表示,没有时区   零件。 SAML系统实体不应该依赖于时间分辨率   比毫秒更精细。实现绝不能产生时间   指定闰秒的瞬间。

我认为你需要你的断言创建应用程序来将所需的NotBeforeNotOnOrAfter转换为/来自UTC。既然你提到过使用jodatime,它就像new DateTime(DateTimeZone.UTC);那样。如果你希望断言消费者在两小时后或之后认为你的断言无效,你需要在当前时间增加两个小时。您可以使用DateTime“plus ...()/ minus ...()”API:

DateTime now = new DateTime(DateTimeZone.UTC);
DateTime twoHoursLater = now.plusHours(2);
DateTime myAssertionExpiry = twoHoursLater;
//use myAssertionExpiry for SAML NotOnOrAfter
DateTime fiveMinutesAgo = now.minusMinutes(5)
//could use fiveMinutesAgo for SAML NotBefore to allow recipient to have 5 minutes different clock time; use 'now' for NotBefore for more realtime/time-critical assertions