我调试了如何将审计添加到系统中以便成功登录,并发现使用 aop 调用 CustomAuditEventRepository.auditEventRepository()。add(AuditEvent事件)。是否有任何文档如何为任何自定义操作添加审核?
答案 0 :(得分:10)
我能用以下代码实现上述功能。
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();
}
}