13
13
import org .springframework .security .core .userdetails .UsernameNotFoundException ;
14
14
import org .springframework .stereotype .Service ;
15
15
16
+ import javax .servlet .http .HttpServletRequest ;
16
17
import java .util .ArrayList ;
17
18
import java .util .List ;
18
19
import java .util .Optional ;
30
31
@ Service
31
32
public class UserDetailServiceImpl implements UserDetailsService {
32
33
private static final Logger LOGGER = LoggerFactory .getLogger (UserDetailServiceImpl .class );
33
-
34
34
private final UserService userService ;
35
+ private HttpServletRequest request ;
36
+ private LoginAttemptService loginAttemptService ;
35
37
36
38
@ Autowired
37
- public UserDetailServiceImpl (UserService userService ) {
39
+ public UserDetailServiceImpl (HttpServletRequest request , LoginAttemptService loginAttemptService , UserService userService ) {
40
+ this .request = request ;
41
+ this .loginAttemptService = loginAttemptService ;
38
42
this .userService = userService ;
39
43
}
40
44
41
-
42
45
/**
43
46
* Locates the user based on the username. In the actual implementation, the search
44
47
* may possibly be case sensitive, or case insensitive depending on how the
@@ -52,8 +55,14 @@ public UserDetailServiceImpl(UserService userService) {
52
55
* GrantedAuthority
53
56
*/
54
57
@ Override
55
- public UserDetails loadUserByUsername (String username ) throws UsernameNotFoundException {
58
+ public UserDetails loadUserByUsername (String username ) throws UsernameNotFoundException , SecurityException {
59
+ String ip = getClientIP (request );
60
+ if (loginAttemptService .isBlocked (ip )) {
61
+ throw new SecurityException ("blocked" );
62
+ }
63
+
56
64
Optional <User > userOptional = Optional .ofNullable (userService .findByUsername (username ));
65
+
57
66
if (!userOptional .isPresent ()) {
58
67
throw new UsernameNotFoundException ("Username & Password doesn't exist!" );
59
68
}
@@ -68,10 +77,11 @@ private List<GrantedAuthority> getGrantedAuthorities(User user) {
68
77
69
78
groupAuthorities .add (user .getTeam ().getGrpAuth ());
70
79
71
-
72
80
for (GrpAuth userAuth : groupAuthorities ) {
73
81
LOGGER .info ("User Auth: " + userAuth .toString ());
74
- authorities .add (new SimpleGrantedAuthority (USER_ROLE_PREFIX + userAuth .getAuthorities ().iterator ().next ().getLevel ()));
82
+ while (userAuth .getAuthorities ().iterator ().hasNext ()) {
83
+ authorities .add (new SimpleGrantedAuthority (USER_ROLE_PREFIX + userAuth .getAuthorities ().iterator ().next ().getLevel ()));
84
+ }
75
85
}
76
86
} catch (Exception e ) {
77
87
LOGGER .error (e .getMessage (), e );
@@ -86,4 +96,19 @@ private List<GrantedAuthority> getGrantedAuthorities(User user) {
86
96
}
87
97
88
98
99
+ private String getClientIP (HttpServletRequest request ) {
100
+ String clientIP ;
101
+ String xfHeader = request .getHeader ("X-Fowarded-For" );
102
+ if (xfHeader == null ) {
103
+ String remoteIP = request .getRemoteAddr ();
104
+ LOGGER .info ("Remote-IP: {}" , remoteIP );
105
+ return remoteIP ;
106
+ }
107
+
108
+ clientIP = xfHeader .split ("," )[0 ];
109
+ LOGGER .info ("Client-IP: {}" , clientIP );
110
+ return clientIP ;
111
+ }
112
+
113
+
89
114
}
0 commit comments