日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

向SAML响应中添加自定义声明–(如何为WSO2 Identity Server编写自定义声明处理程序)...

發(fā)布時(shí)間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 向SAML响应中添加自定义声明–(如何为WSO2 Identity Server编写自定义声明处理程序)... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

總覽

最新版本的WSO2 Identity Server(版本5.0.0)配備了“應(yīng)用程序身份驗(yàn)證框架”,該框架提供了很大的靈活性,可以對(duì)來(lái)自使用異構(gòu)協(xié)議的各種服務(wù)提供商的用戶(hù)進(jìn)行身份驗(yàn)證。 它具有多個(gè)擴(kuò)展點(diǎn),可用于滿足企業(yè)系統(tǒng)中常見(jiàn)的幾個(gè)自定義要求。 在這篇文章中,我將分享使用這樣一個(gè)擴(kuò)展點(diǎn)的細(xì)節(jié)。


功能擴(kuò)展

在企業(yè)系統(tǒng)中使用SAML單一登錄時(shí),依賴(lài)方通過(guò)SAML響應(yīng)來(lái)了解用戶(hù)是否已通過(guò)身份驗(yàn)證。 在這一點(diǎn)上,依賴(lài)方尚不知道其為業(yè)務(wù)和授權(quán)目的可能需要的已認(rèn)證用戶(hù)的其他屬性。 為了向依賴(lài)方提供這些屬性詳細(xì)信息,SAML規(guī)范允許在SAML響應(yīng)中也發(fā)送屬性。 WSO2 Identity Server通過(guò)為管理員提供的GUI開(kāi)箱即用地支持此功能。 有關(guān)此功能和配置的詳細(xì)信息,請(qǐng)參閱[1]。

當(dāng)我們需要向SAML響應(yīng)中添加除下劃線用戶(hù)存儲(chǔ)中可用的屬性之外的其他屬性時(shí),此特定擴(kuò)展提供的靈活性會(huì)派上用場(chǎng)。 為了提供依賴(lài)方請(qǐng)求的所有屬性,可能需要尋找外部數(shù)據(jù)源。

在這里我要描述的樣本中,我們將研究一個(gè)場(chǎng)景,該系統(tǒng)需要提供一些存儲(chǔ)在用戶(hù)存儲(chǔ)中的用戶(hù)本地屬性,以及一些我希望從外部數(shù)據(jù)源中檢索到的其他屬性。
遵循SAML響應(yīng)是我們需要從WSO2 IS發(fā)送給依賴(lài)方的內(nèi)容。

<saml2p:Response Destination="https://localhost:9444/acs" ID="faibaccbcepemkackalbbjkihlegenhhigcdjbjk"InResponseTo="kbedjkocfjdaaadgmjeipbegnclbelfffbpbophe" IssueInstant="2014-07-17T13:15:05.032Z"Version="2.0" xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol"xmlns:xs="http://www.w3.org/2001/XMLSchema"><saml2:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity"xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">localhost</saml2:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">..........</ds:Signature><saml2p:Status><saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></saml2p:Status><saml2:Assertion ID="phmbbieedpcfdhcignelnepkemobepgaaipbjjdk" IssueInstant="2014-07-17T13:15:05.032Z" Version="2.0"xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xs="http://www.w3.org/2001/XMLSchema"><saml2:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">localhost</saml2:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">.........</ds:Signature><saml2:Subject><saml2:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">Administrator</saml2:NameID><saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><saml2:SubjectConfirmationData InResponseTo="kbedjkocfjdaaadgmjeipbegnclbelfffbpbophe"NotOnOrAfter="2014-07-17T13:20:05.032Z"Recipient="https://localhost:9444/acs"/></saml2:SubjectConfirmation></saml2:Subject><saml2:Conditions NotBefore="2014-07-17T13:15:05.032Z" NotOnOrAfter="2014-07-17T13:20:05.032Z"><saml2:AudienceRestriction><saml2:Audience>carbonServer2</saml2:Audience></saml2:AudienceRestriction></saml2:Conditions><saml2:AuthnStatement AuthnInstant="2014-07-17T13:15:05.033Z"><saml2:AuthnContext><saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml2:AuthnContextClassRef></saml2:AuthnContext></saml2:AuthnStatement><saml2:AttributeStatement><saml2:Attribute Name="http://wso2.org/claims/role"NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Internal/carbonServer2,Internal/everyone</saml2:AttributeValue></saml2:Attribute><saml2:AttributeStatement><saml2:Attribute Name="http://pushpalanka.org/claims/keplerNumber"NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">E90836W19881010</saml2:AttributeValue></saml2:Attribute><saml2:Attribute Name="http://pushpalanka.org/claims/status"NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">active</saml2:AttributeValue></saml2:Attribute></saml2:AttributeStatement></saml2:AttributeStatement></saml2:Assertion> </saml2p:Response>

在此響應(yīng)中,我們具有一個(gè)本地屬性(即角色)和另外兩個(gè)屬性http://pushpalanka.org/claims/keplerNumber和http://pushpalanka.org/claims/status,這些屬性已從其他方法中檢索出在我們的擴(kuò)展名中定義。

怎么樣?

  • 實(shí)施定制邏輯以獲取外部聲明。 我們僅需注意兩個(gè)事實(shí)。
    • 定制實(shí)現(xiàn)應(yīng)實(shí)現(xiàn)接口“ org.wso2.carbon.identity.application.authentication.framework.handler.claims.ClaimHandler”,或擴(kuò)展接口“ org.wso2.carbon.identity.application.authentication”的默認(rèn)實(shí)現(xiàn)。 framework.handler.claims.impl.DefaultClaimHandler”。
    • 在方法“ public Map <String,String> handleClaimMappings”處返回的映射應(yīng)包含我們要添加到SAML響應(yīng)中的所有屬性。

    以下是我按照上面編寫(xiě)的示例代碼。 外部聲明可能已從數(shù)據(jù)庫(kù)中查詢(xún),從文件中讀取或根據(jù)需要使用任何其他機(jī)制。

    public class CustomClaimHandler implements ClaimHandler {private static Log log = LogFactory.getLog(CustomClaimHandler.class);private static volatile CustomClaimHandler instance;private String connectionURL = null;private String userName = null;private String password = null;private String jdbcDriver = null;private String sql = null;public static CustomClaimHandler getInstance() {if (instance == null) {synchronized (CustomClaimHandler.class) {if (instance == null) {instance = new CustomClaimHandler();}}}return instance;}public Map<String, String> handleClaimMappings(StepConfig stepConfig,AuthenticationContext context, Map<String, String> remoteAttributes,boolean isFederatedClaims) throws FrameworkException {String authenticatedUser = null;if (stepConfig != null) {//calling from StepBasedSequenceHandlerauthenticatedUser = stepConfig.getAuthenticatedUser();} else {//calling from RequestPathBasedSequenceHandlerauthenticatedUser = context.getSequenceConfig().getAuthenticatedUser();}Map<String, String> claims = handleLocalClaims(authenticatedUser, context);claims.putAll(handleExternalClaims(authenticatedUser));return claims;}/*** @param context* @return* @throws FrameworkException*/protected Map<String, String> handleLocalClaims(String authenticatedUser,AuthenticationContext context) throws FrameworkException {....}private Map<String, String> getFilteredAttributes(Map<String, String> allAttributes,Map<String, String> requestedClaimMappings, boolean isStandardDialect) {....}protected String getDialectUri(String clientType, boolean claimMappingDefined) {....}/*** Added method to retrieve claims from external sources. This results will be merged to the local claims when* returning final claim list, to be added to the SAML response, that is sent back to the SP.** @param authenticatedUser : The user for whom we require claim values* @return*/private Map<String, String> handleExternalClaims(String authenticatedUser) throws FrameworkException {Map<String, String> externalClaims = new HashMap<String, String>();externalClaims.put("http://pushpalanka.org/claims/keplerNumber","E90836W19881010");externalClaims.put("http://pushpalanka.org/claims/status","active");return externalClaims;} }
  • 將已編譯的OSGI軟件包放在IS_HOME / repository / components / dropins中。 (我們將其開(kāi)發(fā)為OSGI捆綁軟件,因?yàn)槲覀冞€需要使用RealmService獲得本地聲明。 您可以在此處找到完整的捆綁軟件和源代碼 )
  • 使WSO2 Identity Server使用我們擁有的新的自定義實(shí)現(xiàn)。
  • 在IS_HOME / repository / conf / security / applicationauthentication.xml中,配置新的處理程序名稱(chēng)。 (在“ ApplicationAuthentication.Extensions.ClaimHandler”元素中。)

    ? ?<ClaimHandler>com.wso2.sample.claim.handler.CustomClaimHandler</ClaimHandler>

    現(xiàn)在,如果查看生成的SAML響應(yīng),我們將看到添加的外部屬性。

    干杯!

    [1] – https://docs.wso2.com/display/IS500/Adding+a+Service+Provider

    翻譯自: https://www.javacodegeeks.com/2014/08/adding-custom-claims-to-the-saml-response-how-to-write-a-custom-claim-handler-for-wso2-identity-server.html

    總結(jié)

    以上是生活随笔為你收集整理的向SAML响应中添加自定义声明–(如何为WSO2 Identity Server编写自定义声明处理程序)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。