简体   繁体   中英

wso2 identity server custom handler reading from properties file

 public class UserRegistrationCustomEventHandler extends AbstractEventHandler { JSONObject jsonObject = null; private static final Log log = LogFactory.getLog(UserRegistrationCustomEventHandler.class); @Override public String getName() { return "customClaimUpdate"; } 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(); String eventName = event.getEventName(); UserStoreManager userStoreManager = (UserStoreManager) eventProperties.get(IdentityEventConstants.EventProperty.USER_STORE_MANAGER); // String userStoreDomain = UserCoreUtil.getDomainName(userStoreManager.getRealmConfiguration()); @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); // Sample API //String apiValue = "http://192.168.1.X:8080/SomeService/user/updateUserEmail?email=sujith@gmail.com&userName=USERS/sujith"; try { URL url = new URL(cityAppUrl); 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); OutputStream os = con.getOutputStream(); os.write(cityAppUrl.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) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } @Override public void init(InitConfig configuration) throws IdentityRuntimeException { super.init(configuration); } @Override public int getPriority(MessageContext messageContext) { return 250; } }

I'm using wso2 identity server 5.10.0 and have to push the updated claim value to an API so I'm using a custom handler and have subscribed to POST_SET_USER_CLAIMS, i have to read the API value from deployment.toml file in jave code of the custom handler. So can any one please help here to read the value from deployment file

I can fetch the updated claim value in logs but im not able to get the API value. So can anyone help me here to read the value from deployment file.

Since the API path is required inside your custom event handler, let's define the API path value as one of the properties of the event handler.

Add the deployment.toml config as follows.

[[event_handler]]
name= "UserRegistrationCustomEventHandler"
subscriptions =["POST_SET_USER_CLAIMS"]
properties.apiPath = "http://192.168.1.X:8080/SomeService/user/updateUserEmail"

Once you restart the server identity-event.properties file populates the given configs.

In your custom event handler java code needs to read the config from identity-event.properties file. The file reading is done at the server startup and every config is loaded to the memory.

By adding this to your java code, you can load to configured value in the property.

configs.getModuleProperties().getProperty("UserRegistrationCustomEventHandler.apiPath")

NOTE: property name needs to be defined as <event_handler_name>.<property_name>

Here is a reference to such event hanlder's property loading code snippet https://github.com/wso2-extensions/identity-governance/blob/68e3f2d5e246b6a75f48e314ee1019230c662b55/components/org.wso2.carbon.identity.password.policy/src/main/java/org/wso2/carbon/identity/password/policy/handler/PasswordPolicyValidationHandler.java#L128-L133

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM