【问题标题】:Spring LDAP - Creation of LdapTemplate in standalone java program - Using Spring LDAP as CDI ResourceSpring LDAP - 在独立 java 程序中创建 LdapTemplate - 使用 Spring LDAP 作为 CDI 资源
【发布时间】:2019-07-09 21:19:38
【问题描述】:

我正在尝试构建一个使用弹簧数据的 LdapTemplate 对象。

 public class LDAPTemplate {

        public static void main(String[] args) {
            LdapContextSource lcs = new LdapContextSource();
            lcs.setUrl("ldap://localhost:389/");
            lcs.setUserDn("cn=Manager, dc=example, dc=com");
            lcs.setPassword("secret1");
            lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
            LdapTemplate ldap = new LdapTemplate(lcs);
            ldap.lookup("cn=aaa");

        }

    }

我想知道的是实例化 ldap 模板对象的正确方法。因为当我执行查找时,它会抛出 NPE。

我试图在 CDI 上下文中使用 LDAP Spring,而根本不使用 spring。如果您对此有指示会很好​​。 Spring LDAP是否依赖spring?

【问题讨论】:

    标签: spring ldap spring-ldap


    【解决方案1】:

    LdapContextSourceInitializingBean 所以你需要打电话给afterPropertiesSet...

    还有 JavaDoc:

    在 Spring 上下文之外使用此类的实现时 当所有属性都被调用时,必须调用 afterPropertiesSet() 设置,以完成初始化。

    【讨论】:

      【解决方案2】:

      正确代码

      public class LDAPTemplate {
          public static void main(String[] args) {
              LdapContextSource lcs = new LdapContextSource();
              lcs.setUrl("ldap://localhost:389/");
              lcs.setUserDn("cn=Manager, dc=example, dc=com");
              lcs.setPassword("secret1");
              lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
              lcs.afterPropertiesSet();
              LdapTemplate ldap = new LdapTemplate(lcs);
              ldap.lookup("cn=aaa");
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        解决方案:在 CDI 中使用 Spring LDAP 而不使用 Spring IoC

        1. 为 LDAP 模板创建资源生产者。

          public class Resources {
          private LdapTemplate template;
          
          @Produces
          //It is a custom qualifier
          @CustomeLDAPTemplate
          public LdapTemplate getTemplate() {
              LdapContextSource lcs = new LdapContextSource();
              lcs.setUrl("ldap://localhost:389/");
              lcs.setUserDn("cn=Manager, dc=example, dc=com");
              lcs.setPassword("secret1");
              lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
              lcs.afterPropertiesSet();
              template = new LdapTemplate(lcs);
              return template;
          }
          
          public void setTemplate(LdapTemplate template) {
              this.template = template;
           }
          }
          
        2. 创建一个自定义限定符 - 说我想要 LdapTemplate 和 CustomeLDAPTemplate 类型的 tempate 对象

          @Qualifier  
          @Retention(RUNTIME)  
          @Target({TYPE,CONSTRUCTOR, METHOD, FIELD})  
          public @interface CustomeLDAPTemplate {}  
          
        3. 实现上——我使用了一个 JAX-WS 类来验证。

          @Path("/users")
          @RequestScoped
          public class UserResource {
          
              @Inject
              @CustomeLDAPTemplate
              private LdapTemplate template;
          
              @POST
              @Consumes(MediaType.APPLICATION_XML)
              public Response createUser(InputStream is){
                  User user = readStream(is);
                  System.out.println("LDAP Look up " + template.lookup("cn=aaa,ou=Org1, dc=example, dc=com").toString());
                  uRepo.save(user);
                  return Response.created(URI.create("/users/" + user.getUser_id())).build();
              }   
          }
          

        【讨论】:

          【解决方案4】:
           /**
           * contextSource
           * @return 
           */
          @Bean
          public LdapContextSource contextSource() {
              LdapContextSource contextSource = new LdapContextSource();
              contextSource.setUrl(properties.getProperty("ldap.url"));
              contextSource.setBase(properties.getProperty("ldap.base.dn"));
              contextSource.setUserDn(properties.getProperty("ldap.principal"));
              contextSource.setPassword(properties.getProperty("ldap.password"));
              contextSource.setReferral("ignore");
              return contextSource;
          }
          
          /**
           * Create Ldap Templelate Instance
           * @return 
           */
          @Bean
          public LdapTemplate ldapTemplate() {
              LdapTemplate ldapTemplate = new LdapTemplate();
              try {
                  ldapTemplate = new LdapTemplate(contextSource());
              } catch (Exception e) {
                  log.error("error while creating LDap Template", e);
              }
              return ldapTemplate;
          }
          
          /**
           * this Method check if the username and password are valid
           * then return either true if exists and false if not
           * @param username
           * @param password
           * @return
           */
          public Boolean authenticateUser(final String username, final String password) {
              boolean auth = false;
              LdapTemplate ldapTemplate = new LdapTemplate(contextSource());
              try {
                  ldapTemplate.setIgnorePartialResultException(true);
                  log.info("ldapTemplate-->" + ldapTemplate);
          
                  final AndFilter filter = new AndFilter().and(new EqualsFilter("objectclass", OBJECT_CLASS)).and(new EqualsFilter(NETWORK_USER_ENTITY, username));
                  auth = ldapTemplate.authenticate(BASE_DN, filter.encode(), password);
                  log.info("is Valid user :" + auth);
              } catch (Exception e) {
                  log.error("error while creating LDap Template", e);
              }
              return auth;
          }
          

          【讨论】:

            猜你喜欢
            • 2022-10-21
            • 2013-02-25
            • 1970-01-01
            • 2018-03-21
            • 1970-01-01
            • 2016-07-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多