gpt4 book ai didi

java - 如何在 jax-ws 请求中添加安全 header

转载 作者:行者123 更新时间:2023-12-03 20:23:20 24 4
gpt4 key购买 nike

我正在使用 apache cxf 创建一个 jax-ws 客户端。我正在为 spring cotext 配置而苦苦挣扎。我只需要将此 header 添加到我的 soap 请求中:

<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken wsu:Id="usernametoken">
<wsse:Username>login</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>

我有三个参数:usernametoken、password、login。

    <jaxws:client id="***" name="***"
endpointName="***"
serviceName="***"
address="***"
serviceClass="***"
username="***"
password="***"
xmlns:tns="***">
</jaxws:client>

上面的代码可以工作并发送 soap 消息,但没有安全 header !你能给我一些关于如何添加标题的想法吗?

最佳答案

使用这个配置

<jaxws:client   etc...>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken"/>
<entry key="user" value="login"/>
<entry key="passwordType" value="PasswordText"/>
<entry key="passwordCallbackRef" value-ref="myPasswordCallback"/>
</map>
</constructor-arg>
</bean>
</jaxws:outInterceptors>
</jaxws:client>

<bean id="myPasswordCallback" class="client.ClientPasswordCallback"/>

还有这个类来管理密码

public class ClientPasswordCallback implements CallbackHandler {

public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];

if ("login".equals(pc.getIdentifier())) {
pc.setPassword("thepassword");
} // else {...} - can add more users, access DB, etc.
}
}

如果你更喜欢Java代码,也是可以的

Client client = ClientProxy.getClient(port);
Endpoint cxfEndpoint = client.getEndpoint();
Map<String,Object> outProps = new HashMap<String,Object>();
outProps.put("action", "UsernameToken");
outProps.put("user", "login");
outProps.put("passwordType","PasswordText");
ClientPasswordCallback c = new ClientPasswordCallback();
outProps.put("passwordCallbackRef",c);

WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
cxfEndpoint.getOutInterceptors().add(wssOut);

关于java - 如何在 jax-ws 请求中添加安全 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37550397/

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