WSO2 Identity Server自定义Handler读取deployment.toml配置问题
在WSO2 Identity Server 5.10.0中从deployment.toml读取API地址到自定义EventHandler
步骤1:在deployment.toml中添加API地址配置
在<IS_HOME>/repository/conf/deployment.toml中添加以下配置,注意event_handler.customClaimUpdate的customClaimUpdate需与你的Handler类getName()方法返回的名称完全一致:
[[event_handler]] name = "customClaimUpdate" subscriptions = ["POST_SET_USER_CLAIMS"] [event_handler.customClaimUpdate] api_url = "http://192.168.1.X:8080/SomeService/user/updateUserEmail"
步骤2:修改自定义EventHandler代码读取配置
更新UserRegistrationCustomEventHandler类,添加成员变量存储API地址,并在init方法中读取配置:
public class UserRegistrationCustomEventHandler extends AbstractEventHandler { JSONObject jsonObject = null; private static final Log log = LogFactory.getLog(UserRegistrationCustomEventHandler.class); // 新增成员变量存储API地址 private String apiUrl; @Override public String getName() { return "customClaimUpdate"; } @Override public void handleEvent(IdentityEvent event) throws IdentityEventException { if (IdentityEventConstants.Event.POST_SET_USER_CLAIMS.equals(event.getEventName())) { String tenantDomain = (String) event.getEventProperties() .get(IdentityEventConstants.EventProperty.TENANT_DOMAIN); String userName = (String) event.getEventProperties().get(IdentityEventConstants.EventProperty.USER_NAME); Map<String, Object> eventProperties = event.getEventProperties(); UserStoreManager userStoreManager = (UserStoreManager) eventProperties.get(IdentityEventConstants.EventProperty.USER_STORE_MANAGER); @SuppressWarnings("unchecked") Map<String, String> claimValues = (Map<String, String>) eventProperties.get(IdentityEventConstants.EventProperty .USER_CLAIMS); String emailId = claimValues.get("http://wso2.org/claims/emailaddress"); userName = "USERS/"+userName; JSONObject json = new JSONObject(); json.put("userName", userName); json.put("emailId", emailId); log.info("JSON:::::::" + json); // 校验API地址是否配置 if (apiUrl == null || apiUrl.isEmpty()) { log.error("API URL未在deployment.toml中配置"); return; } try { URL url = new URL(apiUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5000); con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); con.setDoOutput(true); con.setDoInput(true); con.setRequestMethod("POST"); log.info("CONN:::::::::::::" + con); // 修复原代码错误:发送构造好的JSON数据,而非API地址字符串 OutputStream os = con.getOutputStream(); os.write(json.toString().getBytes("UTF-8")); os.close(); InputStream in = new BufferedInputStream(con.getInputStream()); String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); jsonObject = new JSONObject(result); log.info("JSON OBJECT:::::::::" + jsonObject); } catch (MalformedURLException e) { log.error("无效的API地址: " + apiUrl, e); } catch (IOException e) { log.error("连接API失败: " + apiUrl, e); } } } @Override public void init(InitConfig configuration) throws IdentityRuntimeException { super.init(configuration); // 从配置中读取API地址 if (configuration.getProperties() != null) { this.apiUrl = (String) configuration.getProperties().get("api_url"); if (log.isInfoEnabled()) { log.info("成功加载API地址: " + apiUrl); } } } @Override public int getPriority(MessageContext messageContext) { return 250; } }
关键说明
- 配置节
event_handler.customClaimUpdate的名称必须与Handler的getName()返回值一致,否则配置无法传递到init方法。 - 修复了原代码中错误发送API地址字符串的问题,改为发送构造好的JSON数据。
- 添加了配置为空的校验,避免空指针异常。
内容的提问来源于stack exchange,提问作者Sujith Fredrick
相关产品推荐
相关产品推荐

