向SAML响应中添加自定义声明–(如何为WSO2 Identity Server编写自定义声明处理程序)...
總覽
最新版本的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)容。
在此響應(yīng)中,我們具有一個(gè)本地屬性(即角色)和另外兩個(gè)屬性http://pushpalanka.org/claims/keplerNumber和http://pushpalanka.org/claims/status,這些屬性已從其他方法中檢索出在我們的擴(kuò)展名中定義。
怎么樣?
- 定制實(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;} }在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)題。
- 上一篇: 项目类别是什么 项目类别的解释
- 下一篇: 使用JAX-RS的HTTP缓存