211.6. Samples
根据上面的 Spring 配置,以下示例中的代码示例发送 LDAP 请求来过滤组以获取成员。然后,从响应中提取通用名称。
ProducerTemplate<Exchange> template = exchange .getContext().createProducerTemplate(); Collection<?> results = (Collection<?>) (template .sendBody( "ldap:ldapserver?base=ou=mygroup,ou=groups,ou=system", "(member=uid=huntc,ou=users,ou=system)")); if (results.size() > 0) { // Extract what we need from the device's profile Iterator<?> resultIter = results.iterator(); SearchResult searchResult = (SearchResult) resultIter .next(); Attributes attributes = searchResult .getAttributes(); Attribute deviceCNAttr = attributes.get("cn"); String deviceCN = (String) deviceCNAttr.get(); ...
如果不需要特定的过滤器 - 例如,您只需查找单个条目 - 指定一个通配符过滤器表达式。例如,如果 LDAP 条目有一个通用名称,请使用类似如下的过滤器表达式:
(cn=*)
211.6.1. 使用凭证绑定
Camel 最终用户发出此示例代码,他用于使用凭证绑定到 ldap 服务器。
Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); props.setProperty(Context.PROVIDER_URL, "ldap://localhost:389"); props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.jndi.url"); props.setProperty(Context.REFERRAL, "ignore"); props.setProperty(Context.SECURITY_AUTHENTICATION, "simple"); props.setProperty(Context.SECURITY_PRINCIPAL, "cn=Manager"); props.setProperty(Context.SECURITY_CREDENTIALS, "secret"); SimpleRegistry reg = new SimpleRegistry(); reg.put("myldap", new InitialLdapContext(props, null)); CamelContext context = new DefaultCamelContext(reg); context.addRoutes( new RouteBuilder() { public void configure() throws Exception { from("direct:start").to("ldap:myldap?base=ou=test"); } } ); context.start(); ProducerTemplate template = context.createProducerTemplate(); Endpoint endpoint = context.getEndpoint("direct:start"); Exchange exchange = endpoint.createExchange(); exchange.getIn().setBody("(uid=test)"); Exchange out = template.send(endpoint, exchange); Collection<SearchResult> data = out.getOut().getBody(Collection.class); assert data != null; assert !data.isEmpty(); System.out.println(out.getOut().getBody()); context.stop();