1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.security.core.userdetails;
18
19 import java.util.List;
20 import java.util.Map;
21 import lombok.AccessLevel;
22 import lombok.Getter;
23 import lombok.ToString;
24 import org.bremersee.data.ldaptive.LdaptiveOperations;
25 import org.ldaptive.FilterTemplate;
26 import org.ldaptive.SearchRequest;
27 import org.ldaptive.SearchScope;
28 import org.springframework.security.core.userdetails.UserDetails;
29 import org.springframework.security.core.userdetails.UserDetailsService;
30 import org.springframework.security.core.userdetails.UsernameNotFoundException;
31
32
33
34
35
36
37 @ToString(callSuper = true, exclude = {"ldaptiveOperations"})
38 public class LdaptiveUserDetailsService extends AbstractUserDetailsService implements UserDetailsService {
39
40 @Getter(value = AccessLevel.PROTECTED)
41 private final LdaptiveOperations ldaptiveOperations;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public LdaptiveUserDetailsService(
58 LdaptiveOperations ldaptiveOperations,
59 String userBaseDn,
60 String userFindOneFilter,
61 SearchScope userFindOneSearchScope,
62 String userAccountControlAttributeName,
63 List<String> authorities,
64 String authorityAttributeName,
65 boolean authorityDn,
66 Map<String, String> authorityMap,
67 String authorityPrefix) {
68
69 super(
70 userBaseDn, userFindOneFilter, userFindOneSearchScope, userAccountControlAttributeName, authorities,
71 authorityAttributeName, authorityDn, authorityMap, authorityPrefix);
72 this.ldaptiveOperations = ldaptiveOperations;
73 }
74
75 @Override
76 public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
77 return getLdaptiveOperations()
78 .findOne(
79 SearchRequest.builder()
80 .dn(getUserBaseDn())
81 .filter(FilterTemplate.builder()
82 .filter(getUserFindOneFilter())
83 .parameters(userName)
84 .build())
85 .scope(getUserFindOneSearchScope())
86 .returnAttributes(returnAttributes())
87 .sizeLimit(1)
88 .build(),
89 getUserDetailsLdapMapper(userName))
90 .orElseThrow(() -> new UsernameNotFoundException("User '" + userName + "' was not found."));
91 }
92
93 }