I debugged how audit is getting added into system for successful login and figured out that CustomAuditEventRepository.auditEventRepository().add(AuditEvent event) is getting invoked using aop. Is there any documentation how to add audits for any custom actions?
I was able to achieve the above thing with the following code.
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AuditEventPublisher implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void publish(AuditEvent event) {
if (this.publisher != null)
this.publisher.publishEvent(new AuditApplicationEvent(event));
}
}
@RestController
@RequestMapping("/api")
public class UserXAuthTokenController {
@Inject
private AuditEventPublisher auditPublisher;
.....
.....
@RequestMapping(value = "/logout",
method = RequestMethod.POST)
@Timed
public void logout(@RequestParam String authToken) {
String principal = tokenProvider.getUserNameFromToken(authToken);
AuditEvent event = new AuditEvent(principal, "LOGOUT_START", new HashMap<String, Object>());
auditPublisher.publish(event);
SecurityContextHolder.clearContext();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With