如何在Apex的SOQL Web服务调用中将Type添加到passwordtext?

时间:2018-07-12 23:14:30

标签: soap wsdl apex-code apex wsse

我有一个webservice调用,其中来自以下安全类的xml输出来自oasisopenorgwssSecurity

<script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>

<div id="app">
  <div>
    <twitch v-for="(item, index) in videos" :name="item" :key="index"></twitch>
  </div>
</div>

{

<wsse:UsernameToken>
            <wsse:Username>XXXXXXXX</wsse:Username>
            <wsse:Password>xxxxxxxxxxxx</wsse:Password></wsse:UsernameToken>
public class OasisOpenOrgWssSecuritySecext 

}

上面的代码需要进行转换,以便Web服务调用的输出如下所示

// UserToken Class
public class UsernameToken 
{
    // Constructor for UsernameToken used to pass in username and password parameters
    public UsernameToken(String username, String password)
    {
        this.Username = username;
        this.Password = password;
        this.Nonce = generateNounce();
        this.Created = generateTimestamp();
    }

    public String Username;
    public String Password;
    public String Nonce;
    public String Created;
    private String[] Username_type_info = new String[]{'Username','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Password_type_info = new String[]{'Password','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Nonce_type_info = new String[]{'Nonce','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Created_type_info = new String[]{'wsu:Created','http://www.w3.org/2001/XMLSchema','string','0','1','false'};        
    private String[] apex_schema_type_info = new String[]{'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','true','false'};
    private String[] field_order_type_info = new String[]{'Username','Password','Nonce','Created'};

    // Generate Nounce, random number base64 encoded
    public String generateNounce()
    {
        Long randomLong = Crypto.getRandomLong();
        return EncodingUtil.base64Encode(Blob.valueOf(String.valueOf(randomLong)));
    }

    // Generate timestamp in GMT
    public String generateTimestamp()
    {
        return Datetime.now().formatGmt('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');
    }
}

// SecurityHeaderType Class
public class SecurityHeaderType 
{       
    // Constructor for SecurityHeaderType used to pass in username and password parameters and instantiate the UsernameToken object     
    public SecurityHeaderType(String username, String password)
    {
        this.UsernameToken = new OasisOpenOrgWssSecuritySecext.UsernameToken(username, password);
    }

    public String wsuNamespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';              
    public OasisOpenOrgWssSecuritySecext.UsernameToken UsernameToken;
    private String[] UsernameToken_type_info = new String[]{'UsernameToken','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','UsernameToken','1','1','false'};
    private String[] wsuNamespace_att_info = new String[]{'xmlns:wsu'};               
    private String[] apex_schema_type_info = new String[]{'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','true','false'};
    private String[] field_order_type_info = new String[]{'UsernameToken'};
}

我不知道如何向密码添加TYPE参数。请帮助我!

3 个答案:

答案 0 :(得分:0)

尝试一下:

// UserToken Class
public class UsernameToken 
{
    // Constructor for UsernameToken used to pass in username and password parameters
    public UsernameToken(String username, String password)
    {
        this.Username = username;
        this.Password = password;
        this.Nonce = generateNounce();
        this.Created = generateTimestamp();
        this.Type_x = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    }

    public String Username;
    public String Password;
    public String Nonce;
    public String Created;
    public String Type_x;
    private String[] Username_type_info = new String[]{'Username','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Password_type_info = new String[]{'Password','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Nonce_type_info = new String[]{'Nonce','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Created_type_info = new String[]{'wsu:Created','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Type_x_att_info = new String[]{'Type'};
    private String[] apex_schema_type_info = new String[]{'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','true','false'};
    private String[] field_order_type_info = new String[]{'Username','Password','Nonce','Created'};

    // Generate Nounce, random number base64 encoded
    public String generateNounce()
    {
        Long randomLong = Crypto.getRandomLong();
        return EncodingUtil.base64Encode(Blob.valueOf(String.valueOf(randomLong)));
    }

    // Generate timestamp in GMT
    public String generateTimestamp()
    {
        return Datetime.now().formatGmt('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');
    }
}

答案 1 :(得分:0)

公共类OasisOpenOrgWssSecuritySecext {

// UserToken Class
public class UsernameToken 
{
    // Constructor for UsernameToken used to pass in username and password parameters
    public UsernameToken(String username, String password)
    {
        this.Username = username;
        PasswordToken = new Password_element(password);
        this.Nonce = generateNounce();
        this.Created = generateTimestamp();
    }

    public String Username;
    public Password_element PasswordToken;
    public String Nonce;
    public String Created;
    private String[] Username_type_info = new String[]{'Username','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] PasswordToken_type_info = new String[]{'Password','http://www.w3.org/2001/XMLSchema','String','0','1','false'};
    private String[] Nonce_type_info = new String[]{'Nonce','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
    private String[] Created_type_info = new String[]{'wsu:Created','http://www.w3.org/2001/XMLSchema','string','0','1','false'};        
    private String[] apex_schema_type_info = new String[]{'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','true','false'};
    private String[] field_order_type_info = new String[]{'Username','PasswordToken','Nonce','Created'};

    // Generate Nounce, random number base64 encoded
    public String generateNounce()
    {
        Long randomLong = Crypto.getRandomLong();
        return EncodingUtil.base64Encode(Blob.valueOf(String.valueOf(randomLong)));
    }

    // Generate timestamp in GMT
    public String generateTimestamp()
    {
        return Datetime.now().formatGmt('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');
    }
}

public class Password_element {
    public Password_element(String passwords){
        this.password = passwords;
     }

    public String password;
    public String Type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    private String[] password_type_info = new String[]{'password','http://www.w3.org/2001/XMLSchema','String','0','1','false'};
    private String[] Type_att_info = new String[]{'Type'};
    private String[] apex_schema_type_info = new String[]{'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','true','false'};
    private String[] field_order_type_info = new String[]{};
}

// SecurityHeaderType Class
public class SecurityHeaderType 
{       
    // Constructor for SecurityHeaderType used to pass in username and password parameters and instantiate the UsernameToken object     
    public SecurityHeaderType(String username, String password)
    {
        this.UsernameToken = new OasisOpenOrgWssSecuritySecext.UsernameToken(username, password);
    }

    public String wsuNamespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';              
    public OasisOpenOrgWssSecuritySecext.UsernameToken UsernameToken;
    private String[] UsernameToken_type_info = new String[]{'UsernameToken','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','UsernameToken','1','1','false'};
    private String[] wsuNamespace_att_info = new String[]{'xmlns:wsu'};               
    private String[] apex_schema_type_info = new String[]{'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','true','false'};
    private String[] field_order_type_info = new String[]{'UsernameToken'};
}

}

输出为TYPE,但不使用我添加的密码

<Password Type=""/>

如果我按如下所示更改password_token类,则输出显示为

<Password Type=""><Password>xxxxx</Password></Password>

public class Password_element {
    public Password_element(String passwords){
        this.password = passwords;
     }

    public String password;
    public String Type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
    public String Type_x = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';  
    private String[] password_type_info = new String[]{'password','http://www.w3.org/2001/XMLSchema','String','0','1','false'};
    private String[] Type_att_info = new String[]{'Type'};
    private String[] apex_schema_type_info = new String[]{'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','true','false'};
    private String[] field_order_type_info = new String[]{'Password'};
}

答案 2 :(得分:0)

我在这里发现了类似的问题:What are the parameters for the Salesforce WebServiceCallout.invoke method?

显然,不可能在简单元素上创建属性。