import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* publishAuthenticationEvents
*
* @param request Request reference
* @param userDetails UserDetailsReference Object
* @param eventTag Type of Event to be Published.
* @throws java.io.IOException - Throws If IOException
* @throws javax.servlet.ServletException - Throws if Servlet Exception Detected.
*/
protected void publishAuthenticationEvents(HttpServletRequest request, YourMicroserviceUserDetails userDetails,
YourEntityEventHistory.EntityEventTagNames eventTag)
throws IOException, ServletException {
/**
* Check to ensure all Parameters are Available?
*/
if (request == null || userDetails == null || eventTag == null) {
return;
}
/**
* WA-500
* Publish our Successful Login Event.
*/
Map eventTagProperties &#61; new HashMap<>();
/**
* Save All Applicable Headers, assume defaults...
*/
String requestSourceAddress &#61; request.getRemoteAddr();
String requestSourcePort &#61; Integer.toString(request.getRemotePort());
String requestSourceProtocol &#61; request.getProtocol();
/**
* Check for Where request originated if we are behind a Load Balancer.
*/
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR) !&#61; null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR));
requestSourceAddress &#61; eventTagProperties.get(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR);
}
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_PROTO) !&#61; null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_PROTO,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_PROTO));
}
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_PORT) !&#61; null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_PORT,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_PORT));
}
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_HOST) !&#61; null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_HOST,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_HOST));
}
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_USER_AGENT) !&#61; null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_USER_AGENT,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_USER_AGENT));
}
/**
* Construct the Message
*/
StringBuilder eventMessage &#61; new StringBuilder();
eventMessage.append(eventTag.name());
eventMessage.append(" from ").append(requestSourceAddress).
append(" port: ").append(requestSourcePort).
append(" Protocol: ").append(requestSourceProtocol);
/**
* Instantiate the Person Event History And Publish the Event.
*/
YourEntityEventHistory yourEntityEventHistory &#61; new YourEntityEventHistory(
eventTag.name(),
eventMessage.toString(), eventTagProperties);
messagePublisherService.publishEntityEventHistory(userDetails.getPrincipalID(),
yourEntityEventHistory);
}