如何手动加密SOAP消息?

问题描述 投票:12回答:2

我使用JBoss 4.2.3.GA.在之前的任务中,我使用了JBoss支持的基本加密机制(WS-Security)。即我使用密钥库,信任库文件进行加密和签名消息。通常(以标准方式)在jboss-wsse- *文件中定义了必须在加密过程中使用的密钥别名。我在Action book中使用了JBoss的ws安全配置。

没关系。加密工作正常。

但在我当前的任务中,我需要手动和动态地为键指定别名。任务描述:

  • 我有几个档案。在每个配置文件中,可以是必须用于加密消息的公钥的别名别名。
  • 我有密钥库包含服务器的私钥/公钥和客户端的公钥,它们将向服务器发送消息
  • 我需要使用此别名指定的公钥从配置文件获取别名并加密消息(在客户端)。
  • 所以我需要以某种方式从密钥库加载数据(它必须驻留在文件系统文件夹,即外耳文件),从中获取适当的公钥,然后进行加密。
  • 之后,我需要向具有私钥进行解密的远程Web服务(服务器端)发送消息。
  • 在这里,我看到了服务器端逻辑的几种变体:Web服务使用标准JBoss机制进行解密,或者我可以手动加载密钥库数据并手动进行解密。

所以问题是关于:

  1. 有没有办法为JBoss指定文件系统目录来加载密钥库?
  2. 我可以为标准JBoss WSS机制指定加密别名,以允许jboss在crypt进程中使用此信息吗?
  3. 如果我必须进行手动加密/解密,那么如何将多个Java对象包装到WS消息中,然后使用必要的别名加密它以及如何手动将此消息发送到远程Web服务?

我只是不知道如何开始,使用什么框架,甚至有必要使用外部(非JBoss)框架...

java web-services security jboss ws-security
2个回答
3
投票

如果可能,您可以使用Axis2和Rampart。我在类似的情况下成功地使用了它们。

Rampart是一个用于处理安全性的axis2模块,它公开了一个API,允许您定义要使用的密钥存储位置和别名,从而允许您动态定义它。

Axis2

Rampart

示例代码:

private static final String CONFIGURATION_CTX = "src/ctx";  
private static final String KEYSTORE_TYPE = "org.apache.ws.security.crypto.merlin.keystore.type";
private static final String KEYSTORE_FILE = "org.apache.ws.security.crypto.merlin.file";
private static final String KEYSTORE_PWD = "org.apache.ws.security.crypto.merlin.keystore.password";
private static final String PROVIDER = "org.apache.ws.security.components.crypto.Merlin";

private static void engageRampartModules(Stub stub)
throws AxisFault, FileNotFoundException, XMLStreamException {
    ServiceClient serviceClient = stub._getServiceClient();

    engageAddressingModule(stub);   
    serviceClient.engageModule("rampart");
    serviceClient.engageModule("rahas");

    RampartConfig rampartConfig = prepareRampartConfig();  

    attachPolicy(stub,rampartConfig);
}

/**
 * Sets all the required security properties.
 * @return rampartConfig - an object containing rampart configurations
 */
private static RampartConfig prepareRampartConfig() {
    String certAlias = "alias";             //The alias of the public key in the jks file
    String keyStoreFile = "ctx/client.ks";
    String keystorePassword = "pwd";
    String userName = "youusename";


    RampartConfig rampartConfig = new RampartConfig();
    //Define properties for signing and encription
    Properties merlinProp = new Properties();  
    merlinProp.put(KEYSTORE_TYPE, "JKS");  
    merlinProp.put(KEYSTORE_FILE,keyStoreFile);  
    merlinProp.put(KEYSTORE_PWD, keystorePassword); 

    CryptoConfig cryptoConfig = new CryptoConfig();  
    cryptoConfig.setProvider(PROVIDER);  
    cryptoConfig.setProp(merlinProp);  

    //Rampart configurations
    rampartConfig.setUser(userName);
    rampartConfig.setUserCertAlias(certAlias);  
    rampartConfig.setEncryptionUser(certAlias);  
    rampartConfig.setPwCbClass("com.callback.tests.PasswordCallbackHandler"); //Password Callbak class

    rampartConfig.setSigCryptoConfig(cryptoConfig);  
    rampartConfig.setEncrCryptoConfig(cryptoConfig);
    return rampartConfig;
}

/**
 * attach the security policy to the stub.
 * @param stub
 * @param rampartConfig
 * @throws XMLStreamException
 * @throws FileNotFoundException
 */
private static void attachPolicy(Stub stub, RampartConfig rampartConfig) throws XMLStreamException, FileNotFoundException {
    Policy policy = new Policy();
    policy.addAssertion(rampartConfig);
    stub._getServiceClient().getAxisService().getPolicySubject().attachPolicy(policy);
}

PasswordCallbackHandler:

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

public class PasswordCallbackHandler implements CallbackHandler {

// @Override
public void handle(Callback[] callbacks) throws IOException,
        UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
        WSPasswordCallback pwcb = (WSPasswordCallback) callbacks[i];
        String id = pwcb.getIdentifer();
        switch (pwcb.getUsage()) {
            case WSPasswordCallback.USERNAME_TOKEN: {
                if (id.equals("pwd")) {
                    pwcb.setPassword("pwd");
                }
            }
        }
    }
}

}


0
投票

1&2:为jboss定义密钥库:

<jboss-ws-security xmlns="http://www.jboss.com/ws-security/config" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/ws-security/config 
http://www.jboss.com/ws-security/schema/jboss-ws-security_1_0.xsd">
  <key-store-file>WEB-INF/wsse.keystore</key-store-file>
  <key-store-password>jbossws</key-store-password>
  <trust-store-file>WEB-INF/wsse.truststore</trust-store-file>
 <trust-store-password>jbossws</trust-store-password>
  <config>
     <sign type="x509v3" alias="wsse"/>
     <requires>
        <signature/>
        </requires>
     </config>
</jboss-ws-security>

3:此处针对axis2:http://www.javaranch.com/journal/2008/10/web-service-security-encryption-axis2.html描述的加密替换(以及手动)示例

© www.soinside.com 2019 - 2024. All rights reserved.