import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
protected void publishAuthenticationEvents(HttpServletRequest request, YourMicroserviceUserDetails userDetails,
YourEntityEventHistory.EntityEventTagNames eventTag)
throws IOException, ServletException {
if (request == null || userDetails == null || eventTag == null) {
return;
}
Map eventTagProperties = new HashMap<>();
String requestSourceAddress = request.getRemoteAddr();
String requestSourcePort = Integer.toString(request.getRemotePort());
String requestSourceProtocol = request.getProtocol();
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR) != null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR));
requestSourceAddress = eventTagProperties.get(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_FOR);
}
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_XFORWARDED_PROTO) != 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) != 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) != null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_HOST,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_HOST));
}
if (request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_USER_AGENT) != null) {
eventTagProperties.put(YourEntityEventHistory.PROPERTY_TAG_NAME_USER_AGENT,
request.getHeader(YourEntityEventHistory.PROPERTY_TAG_NAME_USER_AGENT));
}
StringBuilder eventMessage = new StringBuilder();
eventMessage.append(eventTag.name());
eventMessage.append(" from ").append(requestSourceAddress).
append(" port: ").append(requestSourcePort).
append(" Protocol: ").append(requestSourceProtocol);
YourEntityEventHistory yourEntityEventHistory = new YourEntityEventHistory(
eventTag.name(),
eventMessage.toString(), eventTagProperties);
messagePublisherService.publishEntityEventHistory(userDetails.getPrincipalID(),
yourEntityEventHistory);
}