さまざまなサービスを保護するために、春のセキュリティ oauth2 を実装しました。
ここで、クライアント IP、クライアント ID、リクエスト サービス、リクエスト URL、および「認証失敗」、「認証失敗」などのイベント タイプをログに記録する必要があります。
私は次のことを思いついた
public class Events implements ApplicationListener<ApplicationEvent>{
private static Logger log = LoggerFactory.getLogger(Events.class);
@Override
public void onApplicationEvent(ApplicationEvent appEvent) {
if (appEvent instanceof AuthenticationSuccessEvent) {
AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) appEvent;
long timestamp = event.getTimestamp();
User user = (User) event.getAuthentication().getPrincipal();
System.out.println("client " + user.getUsername() + " has been authenticated successfully.");
UsernamePasswordAuthenticationToken source = (UsernamePasswordAuthenticationToken) event.getSource();
WebAuthenticationDetails details = (WebAuthenticationDetails) source.getDetails();
System.out.println("remote ip is " + details.getRemoteAddress());
}
if (appEvent instanceof AuthorizationFailureEvent) {
//TODO
((AuthorizationFailureEvent) appEvent).getAccessDeniedException();
}
if (appEvent instanceof AbstractAuthenticationFailureEvent) {
System.out.println("Sorry, authenticated for client " + appEvent.getSource().toString() + " failure.");
}
}
}
リクエストは非同期であるため、コンテキストからリクエストを取得する方法がわかりません。HttpServletRequest リクエストを取得できれば、ほぼすべてのものを取得できます。