以编程方式为WS-Notification使用者配置SSL

时间:2019-05-29 15:40:02

标签: java ssl cxf

我正试图与之建立联系的一家公司使用WS-Notification标准(或至少是他们的标准)来发布数据。我正在处理的旧项目已经具有Apache CXF,并且可以通过XML配置或使用Spring来以编程方式配置SSL。底部的代码示例。是否可以对ws-notifications做类似的事情?我怀疑情况有很大不同,因为在这种情况下我实际上不是客户。

我正在浏览Apache CXF下载上的ws-notification示例,整个客户端源如下:

        Consumer consumer = new Consumer( new Consumer.Callback()
        {
            public void notify( NotificationMessageHolderType message )
            {
                Object o = message.getMessage().getAny();
                System.out.println( message.getMessage().getAny() );
                if( o instanceof Element )
                {
                    System.out.println( ( ( Element ) o ).getTextContent() );
                }
            }
        }, url );

        // Create a subscription for a Topic on the broker
        NotificationBroker notificationBroker
                = new NotificationBroker( "https://localhost:" + wsnPort + "/wsn/NotificationBroker" );
        Subscription subscription = notificationBroker.subscribe( consumer, "MyTopic" );

以下是我们过去作为客户端进行此操作的代码片段。基本上,寻找如何以编程方式设置这些相同的属性,以便对上面的Consumer和NotificationBroker实例进行相应的配置。

// Configure wss4j properties for SSL
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass( WebService.class );
    factory.setAddress( serviceUrl );

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

    // configure WS-Security interceptor
    Map<String, Object> outProps = Maps.newHashMap();

    java.util.Properties sig_props = new java.util.Properties();
    sig_props.put( "org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin" );
    sig_props.put( "org.apache.ws.security.crypto.merlin.keystore.type", "PKCS12" );
    sig_props.put( "org.apache.ws.security.crypto.merlin.keystore.file", tempPathForCertFileKeystore );
    sig_props.put( "org.apache.ws.security.crypto.merlin.keystore.alias", pfxFileAlias );
    sig_props.put( "org.apache.ws.security.crypto.merlin.keystore.password", pfxFilePassword );
    sig_props.put( "org.apache.ws.security.crypto.merlin.keystore.private.password", pfxFilePassword );
    sig_props.put( "org.apache.ws.security.crypto.merlin.truststore.type", "PKCS12" );
    sig_props.put( "org.apache.ws.security.crypto.merlin.truststore.file", tempPathForCertFileKeystore );
    sig_props.put( "org.apache.ws.security.crypto.merlin.truststore.password", pfxFilePassword );
    sig_props.put( "org.apache.ws.security.crypto.merlin.truststore.private.password", pfxFilePassword );

    outProps.put( WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE + " " + WSHandlerConstants.TIMESTAMP );
    outProps.put( WSHandlerConstants.USER, pfxFileAlias );
    outProps.put( WSHandlerConstants.SIG_KEY_ID, "DirectReference" );
    outProps.put( WSHandlerConstants.SIGNATURE_USER, pfxFileAlias );
    outProps.put( WSHandlerConstants.PW_CALLBACK_REF, new Utility.ClientPasswordHandler( pfxFilePassword ) );

    outProps.put( "cryptoProperties", sig_props );
    outProps.put( WSHandlerConstants.SIG_PROP_REF_ID, "cryptoProperties" );

    webService = ( WebService ) factory.create();

    Client cp = ClientProxy.getClient( webService );

    HTTPConduit httpConduit = ( HTTPConduit ) cp.getConduit();

    TLSClientParameters params =
            httpConduit.getTlsClientParameters();

    if( params == null )
    {
        params = new TLSClientParameters();
        httpConduit.setTlsClientParameters( params );
    }

    SSLContext sslContext = SSLContext.getInstance( "TLS" );

    char[] sslTslKeyPairPasswordCharArray = sslTslKeyPairPassword.toCharArray();

    //clear out the keystore, we create a new one every time
    if( Files.exists( Paths.get( tempPathForCertFileKeystore ) ) )
        Files.delete( Paths.get( tempPathForCertFileKeystore ) );

    KeyStore ks = KeyStore.getInstance( "PKCS12" );

    ks.load( new FileInputStream( sslTlsKeyPairPath ), sslTslKeyPairPasswordCharArray );

    // Store away the newly created keystore.
    FileOutputStream fos = new FileOutputStream( tempPathForCertFileKeystore );
    ks.store( fos, sslTslKeyPairPasswordCharArray );
    fos.close();

    for( String certFilePath : serverCertFilePaths )
        AddCertToKeystore( ks, sslTslKeyPairPasswordCharArray, tempPathForCertFileKeystore, certFilePath, new File( certFilePath ).getName().replace( '.', '_' ) );

    TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
    tmf.init( ks );

    KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
    kmf.init( ks, sslTslKeyPairPasswordCharArray );

    sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom() );

    SSLContext.setDefault( sslContext );

    params.setSSLSocketFactory( sslContext.getSocketFactory() );

    params.setTrustManagers( tmf.getTrustManagers() );
    params.setKeyManagers( kmf.getKeyManagers() );

    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    httpClientPolicy.setConnectionTimeout( 36000 );
    httpClientPolicy.setAllowChunking( false );
    httpConduit.setClient( httpClientPolicy );

0 个答案:

没有答案