Increment code coverage for Application
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-app / src / main / java / org / o / ran / oam / nf / oam / adopter / app / config / LoginAttemptsLogger.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.app.config;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.boot.actuate.audit.AuditEvent;
25 import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository;
26 import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent;
27 import org.springframework.context.annotation.Bean;
28 import org.springframework.context.event.EventListener;
29 import org.springframework.security.web.authentication.WebAuthenticationDetails;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 public class LoginAttemptsLogger {
34     private static final Logger LOG = LoggerFactory.getLogger(LoginAttemptsLogger.class);
35
36     /**
37      *  audit Application Event.
38      */
39     @EventListener
40     public void auditEventHappened(final AuditApplicationEvent auditApplicationEvent) {
41         final AuditEvent auditEvent = auditApplicationEvent.getAuditEvent();
42         final WebAuthenticationDetails details = (WebAuthenticationDetails) auditEvent.getData().get("details");
43         if (details == null) {
44             LOG.info("AUDIT: User: {} Event Type: {}", auditEvent.getPrincipal(), auditEvent.getType());
45             return;
46         }
47         LOG.info("AUDIT: User: {} Event Type: {} Remote IP address: {}",
48                 auditEvent.getPrincipal(), auditEvent.getType(), details.getRemoteAddress());
49     }
50
51     @Bean
52     public InMemoryAuditEventRepository auditEventRepository() throws Exception {
53         return new InMemoryAuditEventRepository();
54     }
55 }