3

我尝试使用 OpenSAML 为 SalesForce 实施 SSO。我的代码生成有效的 SAML 断言,由 salesforce SAML 验证器验证。但是当我尝试向 salesforce 发送断言时,我总是遇到这个错误:

{"error_uri":"https://na4.salesforce.comnull/setup/secur/SAMLValidationPage.apexp","error":"invalid_grant","error_description":"invalid assertion"}

我使用以下代码向销售人员发送请求:

    SAMLResponseGenerator responseGenerator = new SalesforceSAMLResponseGenerator(container, strIssuer, strNameID, strNameQualifier, sessionId);

    String samlAssertion = Base64.encodeBase64String(responseGenerator.generateSAMLAssertionString());
    try {
        HttpClient httpClient = createHttpClient();
        HttpPost httpPost = new HttpPost("https://login.salesforce.com/services/oauth2/token");
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("grant_type", new StringBody("assertion"));
        entity.addPart("assertion_type", new StringBody("urn:oasis:names:tc:SAML:2.0:profiles:SSO:browser"));
        entity.addPart("assertion", new StringBody(samlAssertion));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpClient.execute(httpPost);

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = rd.readLine()) != null) {
            buffer.append(line);
            buffer.append("\n");
        }
        rd.close();
        httpClient.getConnectionManager().shutdown();
        System.out.println(buffer.toString());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我的生成器生成了有效的 SAML(如果我可以信任 salesforce SAML 验证器的结果)。似乎 salesforce 无法解码断言,因为当我发送随机数据而不是 samlAssertion 时,我收到了相同的错误消息。

我还尝试使用 Base64.encodeBase64URLSafeString() 进行编码,但没有积极的结果。

谁能帮我解决这个问题?

4

1 回答 1

2

我的问题的解决方案非常简单。不要信任 SalesForce 的文档,只信任协议规范:) 根据规范,我需要在 SAMLResponse 参数中发送 Base64 编码的 SAML。就这些。

我使用以下代码说明了解决方案:

    HttpClient httpClient = initHttpClient();
    HttpPost httpPost = new HttpPost("https://login.salesforce.com/");
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
    entity.addPart("SAMLResponse", new StringBody(Base64.encodeBase64String(samlAssertion)));
    httpPost.setEntity(entity);
    HttpResponse httpResponse = httpClient.execute(httpPost);

    Header location = httpResponse.getFirstHeader("Location");
    if (null != location) {
        System.out.println(location.getValue());
    }
于 2012-09-06T10:50:15.017 回答