View Javadoc
1   /*
2    * Copyright 2021 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 lombok.extern.slf4j.Slf4j;
25  import org.bremersee.data.ldaptive.reactive.ReactiveLdaptiveOperations;
26  import org.ldaptive.FilterTemplate;
27  import org.ldaptive.SearchRequest;
28  import org.ldaptive.SearchScope;
29  import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
30  import org.springframework.security.core.userdetails.UserDetails;
31  import reactor.core.publisher.Mono;
32  
33  /**
34   * The reactive ldaptive user details service.
35   *
36   * @author Christian Bremer
37   */
38  @ToString(callSuper = true, exclude = {"ldaptiveOperations"})
39  @Slf4j
40  public class ReactiveLdaptiveUserDetailsService extends AbstractUserDetailsService implements
41      ReactiveUserDetailsService {
42  
43    @Getter(value = AccessLevel.PROTECTED)
44    private final ReactiveLdaptiveOperations ldaptiveOperations;
45  
46    /**
47     * Instantiates a new reactive ldaptive user details service.
48     *
49     * @param ldaptiveOperations the ldaptive operations
50     * @param userBaseDn the user base dn
51     * @param userFindOneFilter the user find one filter
52     * @param userFindOneSearchScope the user find one search scope
53     * @param userAccountControlAttributeName the user account control attribute name
54     * @param authorities the authorities
55     * @param authorityAttributeName the authority attribute name
56     * @param authorityDn the authority dn
57     * @param authorityMap the authority map
58     * @param authorityPrefix the authority prefix
59     */
60    public ReactiveLdaptiveUserDetailsService(
61        ReactiveLdaptiveOperations ldaptiveOperations,
62        String userBaseDn,
63        String userFindOneFilter,
64        SearchScope userFindOneSearchScope,
65        String userAccountControlAttributeName,
66        List<String> authorities,
67        String authorityAttributeName,
68        boolean authorityDn,
69        Map<String, String> authorityMap,
70        String authorityPrefix) {
71  
72      super(
73          userBaseDn, userFindOneFilter, userFindOneSearchScope, userAccountControlAttributeName, authorities,
74          authorityAttributeName, authorityDn, authorityMap, authorityPrefix);
75      this.ldaptiveOperations = ldaptiveOperations;
76    }
77  
78    @Override
79    public Mono<UserDetails> findByUsername(String userName) {
80      return getLdaptiveOperations()
81          .findOne(
82              SearchRequest.builder()
83                  .dn(getUserBaseDn())
84                  .filter(FilterTemplate.builder()
85                      .filter(getUserFindOneFilter())
86                      .parameters(userName)
87                      .build())
88                  .scope(getUserFindOneSearchScope())
89                  .returnAttributes(returnAttributes())
90                  .sizeLimit(1)
91                  .build(),
92              getUserDetailsLdapMapper(userName));
93    }
94  
95  }