2014-10-30 157 views
0

我開發了一個Java Web應用程序,並且我想實施SAML。這些是我認爲正確實施SAML的步驟。基於SAML請求創建SAML響應

  1. 服務提供商(SP,我的應用程序在這種情況下)向IdP發送SAML身份驗證請求。
  2. 然後,IdP驗證它並創建一個SAML響應聲明並將其與證書籤名併發送回SP。
  3. SP隨後使用密鑰庫中的證書的公鑰對其進行驗證,然後基於此證據進行進一步處理。

我有一個示例代碼,我能夠創造SAML請求及其類似這樣

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" 
    ID="_c7b796f4-bc16-4fcc-8c1d-36befffc39c2" Version="2.0" 
    IssueInstant="2014-10-30T11:21:08Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" 
    AssertionConsumerServiceURL="http://localhost:8080/mywebapp/consume.jsp"> 
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://localhost:8080/mywebapp 
    </saml:Issuer> 
    <samlp:NameIDPolicy 
     Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified" 
     AllowCreate="true"></samlp:NameIDPolicy> 
    <samlp:RequestedAuthnContext Comparison="exact"> 
     <saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport 
     </saml:AuthnContextClassRef> 
    </samlp:RequestedAuthnContext> 
</samlp:AuthnRequest> 

我可以編碼併發送國內流離失所者。

我想創建示例Java代碼來獲取此SAML請求,然後創建一個SAML響應。 如何解碼請求並驗證它並創建響應?我是否需要在證書上簽名saml回覆?然後發送回SP?

謝謝。

回答

2

您列出的步驟或多或少都正確。我唯一指出的是,如果單詞發送(例如「SP ...向IdP發送SAML身份驗證請求」),則必須注意含義。 SAML允許身份驗證方案與SP和IdP之間的零直接通信。

另一個小的補充是,SP也可以簽署他的請求,所以你可能在雙方都有簽名驗證。 SP方面的驗證是強制性的。

如果要實施SAML,可能需要檢查其中一個現有解決方案,例如Shibboleth。如果你使用Spring和JBoss平臺,你可能需要檢查Spring Security SAMLJBoss PicketLink。如果您想要更低級別,請選擇OpenSAML

在我的公司,我們有JBoss作爲標準,並且對PicketLink非常滿意。

+0

我想測試我的應用程序。我已經創建了SAML請求。我嘗試過一些IdP,但它不是免費的。所以我正在嘗試創建自己的SAML響應。我有一個樣品證書。我想簽署回覆併發送給我的申請。 – iUser 2014-10-30 08:56:23

+0

我想你可以免費使用SalesForce進行測試。以下是PicketLink的文檔,但您也可以將其應用於SP:https://docs.jboss.org/author/display/PLINK/Picketlink+as+SP,+Salesforce+as+IDP – lexicore 2014-10-30 09:21:43

+0

我們如何獲得SAMLAssertion從SAMLResponse使用Java代碼? – 2015-04-10 06:10:26

5

雖然這是一箇舊帖子,但我添加了示例代碼和引用,我發現它們很有用。

SAMLResponse = hreq.getParameter("SAMLResponse"); 
InputSource inputSource = new InputSource(new StringReader(SAMLResponse)); 
SAMLReader samlReader = new SAMLReader();     
response2 = org.opensaml.saml2.core.Response)samlReader.readFromFile(inputSource); 

現在驗證數字簽名:

org.opensaml.saml2.core.Response response2 = (org.opensaml.saml2.core.Response)samlReader.readFromFile(inputSource); 
//To fetch the digital signature from the response. 
Signature signature = response2.getSignature(); 
X509Certificate certificate = (X509Certificate) keyStore.getCertificate(domainName); 
//pull out the public key part of the certificate into a KeySpec 
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(certificate.getPublicKey().getEncoded()); 
//get KeyFactory object that creates key objects, specifying RSA - java.security.KeyFactory 
KeyFactory keyFactory = KeyFactory.getInstance("RSA");     
//generate public key to validate signatures 
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); 
//we have the public key      
BasicX509Credential publicCredential = new BasicX509Credential(); 
//add public key value 
publicCredential.setPublicKey(publicKey); 
//create SignatureValidator 
SignatureValidator signatureValidator = new SignatureValidator(publicCredential); 
//try to validate 
try{ 
signatureValidator.validate(signature); 
catch(Exception e){ 
// 
} 

現在取斷言圖:

samlDetailsMap = setSAMLDetails(response2); 

在上述邏輯中使用下面私有方法的拉所有斷言屬性。最後,您將獲得發送給您的所有字段的地圖。

private Map<String, String> setSAMLDetails(org.opensaml.saml2.core.Response response2){ 
     Map<String, String> samlDetailsMap = new HashMap<String, String>(); 
     try { 
      List<Assertion> assertions = response2.getAssertions(); 
      LOGGER.error("No of assertions : "+assertions.size()); 
      for(Assertion assertion:assertions){ 
       List<AttributeStatement> attributeStatements = assertion.getAttributeStatements(); 
       for(AttributeStatement attributeStatement: attributeStatements){ 
        List<Attribute> attributes = attributeStatement.getAttributes(); 
        for(Attribute attribute: attributes){ 
         String name = attribute.getName();       
         List<XMLObject> attributes1 = attribute.getAttributeValues(); 
         for(XMLObject xmlObject : attributes1){ 
          if(xmlObject instanceof XSString){ 
           samlDetailsMap.put(name, ((XSString) xmlObject).getValue()); 
           LOGGER.error("Name is : "+name+" value is : "+((XSString) xmlObject).getValue()); 
          }else if(xmlObject instanceof XSAnyImpl){ 
           String value = ((XSAnyImpl) xmlObject).getTextContent(); 

           samlDetailsMap.put(name, value); 

          }   
        } 
       } 
      }  
     } 
     } catch (Exception e) {    
      LOGGER.error("Exception occurred while setting the saml details");   
     }  
     LOGGER.error("Exiting from setSAMLDetails method"); 
     return samlDetailsMap; 
    } 

添加如下新類SAMLReader:

import java.io.IOException; 
import java.io.InputStream; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.opensaml.DefaultBootstrap; 
import org.opensaml.xml.Configuration; 
import org.opensaml.xml.XMLObject; 
import org.opensaml.xml.io.UnmarshallingException; 
import org.w3c.dom.Element; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 


public class SAMLReader { 

private static DocumentBuilder builder; 

static{ 
     try{ 
      DefaultBootstrap.bootstrap(); 
      DocumentBuilderFactory factory = 
        DocumentBuilderFactory.newInstance(); 
       factory.setNamespaceAware (true);   
      builder = factory.newDocumentBuilder(); 
     }catch (Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 



/** 
* 
* @param filename 
* @return 
* @throws IOException 
* @throws UnmarshallingException 
* @throws SAXException 
*/ 
public XMLObject readFromFile (String filename) 
      throws IOException, UnmarshallingException, SAXException{ 
      return fromElement (builder.parse (filename).getDocumentElement());  
} 
/** 
*  
* @param is 
* @return 
* @throws IOException 
* @throws UnmarshallingException 
* @throws SAXException 
*/ 
public XMLObject readFromFile (InputStream is) 
       throws IOException, UnmarshallingException, SAXException{ 
       return fromElement (builder.parse (is).getDocumentElement());  
} 
/** 
*  
* @param is 
* @return 
* @throws IOException 
* @throws UnmarshallingException 
* @throws SAXException 
*/ 
public XMLObject readFromFile (InputSource is) 
       throws IOException, UnmarshallingException, SAXException{     
       return fromElement (builder.parse (is).getDocumentElement());  
} 

/** 
* 
* @param element 
* @return 
* @throws IOException 
* @throws UnmarshallingException 
* @throws SAXException 
*/ 
public static XMLObject fromElement (Element element) 
      throws IOException, UnmarshallingException, SAXException{ 
    return Configuration.getUnmarshallerFactory() 
       .getUnmarshaller (element).unmarshall (element);  
} 

}