I'm working on a requirement to log the following details "username, authentication_status, remote_ip, request_url, response_code" to an Oracle database for each GET request made to a Spring Boot application for both successful and unsuccessful requests.
I've currently got the below class which is recording the username, auth status and requesting ip address okay but I'm not able to get the requested url and response code. Not overly familiar with Spring Boot but is it possible for me to get these details easily?
@Component
public class AuditLogger {
@Autowired
JdbcTemplate jdbcTemplate;
@EventListener
public void auditEventHappened(AuditApplicationEvent auditApplicationEvent) {
AuditEvent auditEvent = auditApplicationEvent.getAuditEvent();
WebAuthenticationDetails details = (WebAuthenticationDetails) auditEvent.getData().get("details");
// Log this to the database
String sqlStatement = "INSERT INTO API_AUDIT(USERNAME,AUTHENTICATION_STATUS,REMOTE_IP,REQUEST_URL,RESPONSE_CODE) VALUES ("
+ "'" + auditEvent.getPrincipal() + "', "
+ "'" + auditEvent.getType() + "', "
+ "'" + details.getRemoteAddress() + "',"
+ "null," // This should be the request url
+ "null" // This should be the response code
+ ")";
jdbcTemplate.execute(sqlStatement);
}
}