gpt4 book ai didi

java - 在 Java 中为 ws-security UsernameToken 实现密码摘要

转载 作者:行者123 更新时间:2023-11-29 10:05:10 27 4
gpt4 key购买 nike

我正在尝试从服务器调用 ws-security 安全网络服务,不幸的是,该服务器本身不支持此功能。我采用的方法是实现一个 .jsp,它充当实际端点 URL 的反向代理,在此过程中添加带有 ws-security 元素的元素。

这似乎工作得很好,我相信我已经使用正确的 namespace 等正确地构建了 XML。我已经通过比较 XML 与 SOAP-UI 生成的 XML 来验证这一点。

问题在于实现密码摘要生成器。我没有得到与 SOAP-UI 使用相同的 NOce 输入、xsd:dateTime 和密码以及以下代码所做的结果相同的结果。

StringBuffer passwordDigestStr_ = new StringBuffer();

// First append the NOnce from the SOAP header
passwordDigestStr_.append(Base64.decode("PzlbwtWRpmFWjG0JRIRn7A=="));

// Then append the xsd:dateTime in UTC timezone
passwordDigestStr_.append("2012-06-09T18:41:03.640Z");

// Finally append the password/secret
passwordDigestStr_.append("password");

System.out.println("Generated password digest: " + new String(com.bea.xbean.util.Base64.encode(org.apache.commons.codec.digest.DigestUtils.sha(passwordDigestStr_.toString())), "UTF-8"));

我认为问题在于实现前两个元素的散列,如 http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0.pdf 所解释的那样

Note that the nonce is hashed using the octet sequence of its decoded value while the timestamp is hashed using the octet sequence of its UTF8 encoding as specified in the contents of the element.

如果有人能帮我解决这个问题那就太好了,因为它开始让我发疯了!如果能提供源码就更好了。

最佳答案

我将在没有 SOAP-UI 的情况下尝试一下。散列函数的输入应该是字节,而不是字符串。 DigestUtils.sha() 将允许您使用字符串,但该字符串必须正确编码。当你写随机数时,你正在调用 StringBuffer.append(Object)最终调用 byte[].toString()。这给你类似 [B@3e25a5 的东西,绝对不是你想要的。通过在任何地方使用字节,你应该避免这个问题。请注意,下面的示例使用 org.apache.commons.codec.binary.Base64,而不是您使用的 Base64 类。没关系,那只是我手边的那个。

ByteBuffer buf = ByteBuffer.allocate(1000);
buf.put(Base64.decodeBase64("PzlbwtWRpmFWjG0JRIRn7A=="));
buf.put("2012-06-09T18:41:03.640Z".getBytes("UTF-8"));
buf.put("password".getBytes("UTF-8"));
byte[] toHash = new byte[buf.position()];
buf.rewind();
buf.get(toHash);
byte[] hash = DigestUtils.sha(toHash);
System.out.println("Generated password digest: " + Base64.encodeBase64String(hash));

关于java - 在 Java 中为 ws-security UsernameToken 实现密码摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10964434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com