Skip to content

Commit 32fb0ff

Browse files
author
noobandy
committed
Authentication logging
1 parent fc50cce commit 32fb0ff

21 files changed

+366
-29
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@
174174
<version>5.1.22</version>
175175
</dependency>
176176
<!-- Hibernate -->
177+
<!-- <dependency>
178+
<groupId>cglib</groupId>
179+
<artifactId>cglib</artifactId>
180+
<version>3.1</version>
181+
</dependency>
182+
-->
177183
<dependency>
178184
<groupId>org.hibernate</groupId>
179185
<artifactId>hibernate-entitymanager</artifactId>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
*
3+
*/
4+
package in.anandm.apps.template.application.impl;
5+
6+
import in.anandm.apps.template.domain.model.user.FailedLogin;
7+
import in.anandm.apps.template.domain.model.user.HostAddress;
8+
import in.anandm.apps.template.domain.model.user.IFailedLoginRepository;
9+
import in.anandm.apps.template.domain.model.user.IUserRepository;
10+
import in.anandm.apps.template.domain.model.user.User;
11+
12+
import javax.servlet.http.HttpServletRequest;
13+
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.context.ApplicationListener;
16+
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
17+
import org.springframework.security.core.userdetails.UserDetails;
18+
import org.springframework.stereotype.Component;
19+
import org.springframework.transaction.annotation.Transactional;
20+
import org.springframework.web.context.request.RequestContextHolder;
21+
import org.springframework.web.context.request.ServletRequestAttributes;
22+
23+
/**
24+
* @author anandm
25+
*
26+
*/
27+
@Component
28+
public class AuthenticationFailureListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
29+
30+
@Autowired
31+
private IUserRepository userRepository;
32+
@Autowired
33+
private IFailedLoginRepository failedLoginRepository;
34+
35+
@Transactional
36+
@Override
37+
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
38+
39+
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
40+
.getRequest();
41+
String ipAddress = request.getRemoteAddr();
42+
String userId = "";
43+
Object principal = event.getAuthentication().getPrincipal();
44+
if(principal instanceof String){
45+
userId = (String) principal;
46+
}else{
47+
UserDetails userDetails = (UserDetails) principal;
48+
userId = userDetails.getUsername();
49+
}
50+
51+
User user = userRepository.getUserByUserId(userId);
52+
53+
if(user != null){
54+
failedLoginRepository.saveFailedLogin(new FailedLogin(event.getTimestamp(), new HostAddress(ipAddress), user));
55+
}
56+
57+
}
58+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
*
3+
*/
4+
package in.anandm.apps.template.application.impl;
5+
6+
import in.anandm.apps.template.application.vo.CustomUserDetails;
7+
import in.anandm.apps.template.domain.model.user.HostAddress;
8+
import in.anandm.apps.template.domain.model.user.ISuccessfullLoginRepository;
9+
import in.anandm.apps.template.domain.model.user.IUserRepository;
10+
import in.anandm.apps.template.domain.model.user.IUserSessionRepository;
11+
import in.anandm.apps.template.domain.model.user.SuccessfullLogin;
12+
import in.anandm.apps.template.domain.model.user.User;
13+
import in.anandm.apps.template.domain.model.user.UserSession;
14+
15+
import javax.servlet.http.HttpServletRequest;
16+
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.context.ApplicationListener;
19+
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
20+
import org.springframework.security.core.context.SecurityContextHolder;
21+
import org.springframework.stereotype.Component;
22+
import org.springframework.transaction.annotation.Transactional;
23+
import org.springframework.web.context.request.RequestContextHolder;
24+
import org.springframework.web.context.request.ServletRequestAttributes;
25+
26+
/**
27+
* @author anandm
28+
*
29+
*/
30+
@Component
31+
public class AutheticationSuccessListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
32+
33+
@Autowired
34+
private IUserRepository userRepository;
35+
@Autowired
36+
private ISuccessfullLoginRepository successfullLoginRepository;
37+
@Autowired
38+
private IUserSessionRepository userSessionRepository;
39+
40+
@Transactional
41+
@Override
42+
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
43+
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
44+
.getRequest();
45+
String ipAddress = request.getRemoteAddr();
46+
String userId = "";
47+
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
48+
49+
if(principal instanceof String) {
50+
userId = (String) principal;
51+
}else{
52+
CustomUserDetails userDetails = (CustomUserDetails) principal;
53+
userId = userDetails.getUsername();
54+
}
55+
56+
User user = userRepository.getUserByUserId(userId);
57+
58+
successfullLoginRepository.saveSuccessfullLogin(new SuccessfullLogin(event.getTimestamp(), new HostAddress(ipAddress), user));
59+
60+
userSessionRepository.saveUserSession(new UserSession(RequestContextHolder.currentRequestAttributes().getSessionId(),event.getTimestamp(), new HostAddress(ipAddress), user));
61+
}
62+
63+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
*/
4+
package in.anandm.apps.template.application.impl;
5+
6+
import in.anandm.apps.template.domain.model.user.IUserSessionRepository;
7+
import in.anandm.apps.template.domain.model.user.UserSession;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.context.ApplicationListener;
11+
import org.springframework.security.core.session.SessionDestroyedEvent;
12+
import org.springframework.stereotype.Component;
13+
import org.springframework.transaction.annotation.Transactional;
14+
15+
/**
16+
* @author anandm
17+
*
18+
*/
19+
@Component
20+
public class SessionDestroyedEventListener implements ApplicationListener<SessionDestroyedEvent> {
21+
22+
@Autowired
23+
private IUserSessionRepository userSessionRepository;
24+
25+
@Transactional
26+
@Override
27+
public void onApplicationEvent(SessionDestroyedEvent event) {
28+
UserSession userSession = userSessionRepository.getUserSessionBySessionId(event.getId());
29+
if(userSession != null){
30+
userSession.endSession();
31+
userSessionRepository.saveUserSession(userSession);
32+
}
33+
}
34+
35+
}

src/main/java/in/anandm/apps/template/domain/model/user/FailedLoginAttempt.java renamed to src/main/java/in/anandm/apps/template/domain/model/user/FailedLogin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818
@Entity
19-
public class FailedLoginAttempt {
19+
public class FailedLogin {
2020

2121
private Long failedOn;
2222

@@ -27,7 +27,7 @@ public class FailedLoginAttempt {
2727

2828

2929

30-
public FailedLoginAttempt(Long failedOn, HostAddress hostAddress,User
30+
public FailedLogin(Long failedOn, HostAddress hostAddress,User
3131
user) {
3232
super();
3333
this.failedOn = failedOn;
@@ -66,7 +66,7 @@ public Long getId() {
6666
private Long id;
6767

6868

69-
FailedLoginAttempt() {
69+
FailedLogin() {
7070
super();
7171
// TODO Auto-generated constructor stub
7272
}

src/main/java/in/anandm/apps/template/domain/model/user/HostAddress.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,15 @@ public HostAddress(String ip) {
2929
public String getIp() {
3030
return ip;
3131
}
32+
33+
/**
34+
*
35+
*/
36+
HostAddress() {
37+
super();
38+
39+
}
40+
41+
3242

3343
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
*
3+
*/
4+
package in.anandm.apps.template.domain.model.user;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author anandm
10+
*
11+
*/
12+
public interface IFailedLoginRepository {
13+
14+
void saveFailedLogin(FailedLogin failedLogin);
15+
16+
List<FailedLogin> getFailedLoginofUserWithId(String userId);
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
*
3+
*/
4+
package in.anandm.apps.template.domain.model.user;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author anandm
10+
*
11+
*/
12+
public interface ISuccessfullLoginRepository {
13+
14+
void saveSuccessfullLogin(SuccessfullLogin successfullLogin);
15+
16+
List<SuccessfullLogin> getSuccessfullLoginofUserWithId(String userId);
17+
}

src/main/java/in/anandm/apps/template/domain/model/user/IUserRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*/
1414
public interface IUserRepository {
1515

16-
void addUser(User user);
16+
void saveUser(User user);
1717
User getUserByUserId(String userId);
18-
18+
1919
DataTable<User> getDataTable(Map<String, String> params);
2020
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*
3+
*/
4+
package in.anandm.apps.template.domain.model.user;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author anandm
10+
*
11+
*/
12+
public interface IUserSessionRepository {
13+
14+
void saveUserSession(UserSession userSession);
15+
16+
List<UserSession> getUserSessionofUserWithId(String userId);
17+
18+
UserSession getUserSessionBySessionId(String sessionId);
19+
}

0 commit comments

Comments
 (0)