我正在编写一个SOAP服务(我们称之为X),它充当各种“转发代理”,替换正文中的多个元素,然后调用另一个SOAP服务(Y)。我想使用与我在Y中收到的相同的WS-Security凭证(明文用户名和密码),但是我在检索Password元素的值时遇到了问题。
我在wsit-package.service.xml
文件中声明的政策引用了com.sun.xml.wss.impl.callback.PasswordValidationCallback.PasswordValidator
的实施:
<wsp1_2:Policy wsu:Id="UsernameToken"
xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702"
xmlns:wsss="http://schemas.sun.com/2006/03/wss/server"
xmlns:wspp="http://java.sun.com/xml/ns/wsit/policy">
<wsp1_2:ExactlyOne>
<wsp1_2:All>
<sp:SupportingTokens>
<wsp1_2:Policy>
<sp:UsernameToken sp:IncludeToken="http://docs.oasis-open.org/
ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient" />
<sp:IncludeTimestamp />
</wsp1_2:Policy>
</sp:SupportingTokens>
<wsss:ValidatorConfiguration wspp:visibility="private" >
<wsss:Validator name="usernameValidator"
classname="com.service.validator.SecurityValidator" />
</wsss:ValidatorConfiguration>
</wsp1_2:All>
</wsp1_2:ExactlyOne>
</wsp1_2:Policy>
我可以在验证器中访问密码:
@Override
public boolean validate(Request request) throws PasswordValidationException {
String password = ((PlainTextPasswordRequest) request).getPassword();
return true;
}
但是,由于验证程序无法访问WebServiceContext,因此没有一个方便的位置来存储我的服务端点可访问的内容。
使用其他标头,例如WS-Addressing,我能够使用Handler(SOAPHandler<SOAPMessageContext>
的实现)来提取值,然后将它们放回到我的端点要检索的应用程序范围下的上下文中。 SOAP消息到达我的处理程序链时,WS-Security标头已被剥离,因此无法在Handler中检索它们的值。
如果没有像使用验证器将密码存储在数据库/全局映射/ threadlocal存储中这样做,那么我有没有办法检索我的端点提供的WS-Security密码? /强>
我应该注意,我可以通过Subject subj = SubjectAccessor.getRequesterSubject(context)
访问我的终端上的WS-Security用户名信息,但这似乎不包含密码。
答案 0 :(得分:1)
由于缺乏更好的解决方案,我最终使用ThreadLocal存储来访问服务端点中的WS-Security用户名和密码:
package com.my.ws.validator;
import com.sun.xml.wss.impl.callback.PasswordValidationCallback.PasswordValidationException;
import com.sun.xml.wss.impl.callback.PasswordValidationCallback.PasswordValidator;
import com.sun.xml.wss.impl.callback.PasswordValidationCallback.PlainTextPasswordRequest;
import com.sun.xml.wss.impl.callback.PasswordValidationCallback.Request;
public class SecurityValidator implements PasswordValidator {
private static final ThreadLocal<String> username = new ThreadLocal<String>();
private static final ThreadLocal<String> password = new ThreadLocal<String>();
@Override
public boolean validate(final Request request) throws PasswordValidationException {
if (request instanceof PlainTextPasswordRequest) {
PlainTextPasswordRequest plainText = (PlainTextPasswordRequest) request;
if (null == plainText.getUsername() || plainText.getUsername().trim().isEmpty())
throw new PasswordValidationException("A username must be provided");
else
username.set(plainText.getUsername());
if (null == plainText.getPassword() || plainText.getPassword().trim().isEmpty())
throw new PasswordValidationException("A password must be provided");
else
password.set(plainText.getPassword());
return true;
}
return false;
}
public static String getUsername() {
String user = username.get();
username.remove();
return user;
}
public static String getPassword() {
String pass = password.get();
password.remove();
return pass;
}
}