简体   繁体   English

Apache CXF Java客户端的安全策略

[英]Security Policy for Apache CXF Java Client

I have been given the task of creating a Java client for an existing Soap web service with a security policy defined as the following: 我的任务是为现有的Soap Web服务创建Java客户端,其安全策略定义如下:

<wsp:Policy wsu:Id="security_policy_id">
    <wsp:ExactlyOne>
        <wsp:All>
            <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
                <wsp:Policy>
                    <sp:TransportToken>
                        <wsp:Policy>
                            <sp:HttpsToken RequireClientCertificate="false"/>
                        </wsp:Policy>
                    </sp:TransportToken>
                    <sp:AlgorithmSuite>
                        <wsp:Policy>
                            <sp:Basic256/>
                        </wsp:Policy>
                    </sp:AlgorithmSuite>
                    <sp:Layout>
                        <wsp:Policy>
                            <sp:Lax/>
                        </wsp:Policy>
                    </sp:Layout>
                    <sp:IncludeTimestamp/>
                </wsp:Policy>
            </sp:TransportBinding>
            <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
                <wsp:Policy>
                    <sp:UsernameToken
                            sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
                        <wsp:Policy>
                            <sp:WssUsernameToken10/>
                        </wsp:Policy>
                    </sp:UsernameToken>
                </wsp:Policy>
            </sp:SignedSupportingTokens>
            <sp:Wss10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
                <wsp:Policy/>
            </sp:Wss10>
        </wsp:All>
    </wsp:ExactlyOne>
</wsp:Policy>

I have generated the stubs via Maven and wsdl2java, but I haven't been successful with authentication. 我已经通过Maven和wsdl2java生成了存根,但是身份验证还没有成功。 I am not sure where to begin with fulfilling the policy in the client. 我不确定从哪里开始执行客户端中的策略。 I've found several examples using just a UsernameToken or X.509 certificate, but nothing with the apparent complexity of this policy. 我发现了几个仅使用UsernameToken或X.509证书的示例,但没有一个明显的复杂性。 I'm struggling to put all the pieces together. 我正在努力将所有部分放在一起。 Here is a sample from the CXF site which is hopefully a start to what I'm looking for. 这是CXF网站上的示例,希望这是我正在寻找的内容的起点。

        Client client = ClientProxy.getClient(greeter);
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("action", "UsernameToken");
        props.put("user", "alice");
        props.put("passwordType", "PasswordText");
        WSS4JOutInterceptor wss4jOut = new WSS4JOutInterceptor(props);

        client.getOutInterceptors().add(wss4jOut);    ((BindingProvider)greeter).getRequestContext().put("password","password");

Note: I have no control over the wsdl or web service. 注意:我无法控制wsdl或Web服务。

Since you are using wsdl2java maven plugin from CXF, a lot of classes would have been generated including a Service class. 由于您使用的是来自CXF的wsdl2java maven插件,因此将生成许多类,包括Service类。 So what you need to do is get the stub from the service class generated and inject username and password provided and then it should work. 因此,您需要做的是从生成的服务类中获取存根,并注入提供的用户名和密码,然后它应该可以工作。 Something like this: 像这样:

final YourService service = new YourService();
final YourStub stub = service.getService();

final Map ctx = ((BindingProvider)stub).getRequestContext();

ctx.put("ws-security.username", userName);
ctx.put("ws-security.password", password);

stub.callYourMethod();

PS: Please make sure you have the right libraries, I just used cxf-bundle and nothing else from cxf and it worked! PS:请确保您具有正确的库,我只是使用了cxf-bundle,而没有使用cxf的其他东西,它起作用了! Earlier it was not working as I had individually included libraries from cxf. 之前它不起作用,因为我单独包含了cxf的库。

The code will automatically take care of all the policies mentioned in the wsdl file 该代码将自动处理wsdl文件中提到的所有策略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM