directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Turny <turny.da...@gmail.com>
Subject [ApacheDS] How to get client IP address in an interceptor?
Date Tue, 09 Aug 2016 11:13:17 GMT
Hi,

I would like to write an interceptor that do things based on the
client IP address.
Unfortunately I couldn't get the client IP.
This is what I've done so far:
I've installed the apacheds-2.0.0-M20-x86_64.rpm on a linux box. The
server is working
well.
I've extended the BaseInterceptor and overwritten the bind method.
I've tried to get
the IP through a CoreSession object and through a LdapPrincipal object as well.
I've put the jar the right place and registered it as an interceptor.
It is the first in the
first place of the chain.

This is the code:

public class FirstTestInterceptor extends BaseInterceptor {

   private static final Logger log = LoggerFactory.
           getLogger(FirstTestInterceptor.class);

   String message = "Hello, this is my interceptor!!!";

   @Override
   public void bind(BindOperationContext bindContext) throws LdapException {
      log.info(message);
      log.warn(message);
      try {
         log.info("Get the session object.");
         CoreSession session = bindContext.getSession();
         if (null != session) {
            log.info("Get the socketAddress object.");
            SocketAddress socketAddress = session.getClientAddress();
            if (null != socketAddress) {
               log.info("Get the IP address.");
               if (socketAddress instanceof InetSocketAddress) {
                  InetSocketAddress inetSocketAddress =
(InetSocketAddress) socketAddress;
                  log.info("This is the IP: " +
inetSocketAddress.getAddress() + " and this is the port: " +
inetSocketAddress.getPort());
               }
            } else {
               log.info("The socketAddress object is null.");
            }

         } else {
            log.info("The session object is null.");
         }

      } catch (Exception e) {
         log.error("An exception was thrown: ", e);
      }

      try {
         log.info("Get the effective Principal.");
         LdapPrincipal ldapPrincipal = bindContext.getEffectivePrincipal();
         if (null != ldapPrincipal) {
            log.info("Get the socketAddress object.");
            SocketAddress socketAddress = ldapPrincipal.getClientAddress();
            if (null != socketAddress) {
               log.info("Get the IP address.");
               if (socketAddress instanceof InetSocketAddress) {
                  InetSocketAddress inetSocketAddress =
(InetSocketAddress) socketAddress;
                  log.info("This is the IP: " +
inetSocketAddress.getAddress() + " and this is the port: " +
inetSocketAddress.getPort());
               }
            } else {
               log.info("The socketAddress object is null.");
            }
         } else {
            log.info("The effective principal object is null.");
         }
      } catch (Exception e) {
         log.error("An exception was thrown: ", e);
      }

      super.bind(bindContext);
      next(bindContext);
   }

}


After connecting with Apache Directory Studio I can see this in the log:


[09:34:36] INFO [hu.inew.apacheds.interceptor.FirstTestInterceptor] -
Hello, this is my interceptor!!!
[09:34:36] WARN [hu.inew.apacheds.interceptor.FirstTestInterceptor] -
Hello, this is my interceptor!!!
[09:34:36] INFO [hu.inew.apacheds.interceptor.FirstTestInterceptor] -
Get the session object.
[09:34:36] INFO [hu.inew.apacheds.interceptor.FirstTestInterceptor] -
The session object is null.
[09:34:36] INFO [hu.inew.apacheds.interceptor.FirstTestInterceptor] -
Get the effective Principal.
[09:34:36] ERROR [hu.inew.apacheds.interceptor.FirstTestInterceptor] -
An exception was thrown:
java.lang.NullPointerException
        at org.apache.directory.server.core.api.interceptor.context.AbstractOperationContext.getEffectivePrincipal(AbstractOperationContext.java:387)
        at hu.inew.apacheds.interceptor.FirstTestInterceptor.bind(FirstTestInterceptor.java:60)
        at org.apache.directory.server.core.DefaultOperationManager.bind(DefaultOperationManager.java:439)
        at org.apache.directory.server.ldap.handlers.request.BindRequestHandler.handleSimpleAuth(BindRequestHandler.java:184)
        at org.apache.directory.server.ldap.handlers.request.BindRequestHandler.handle(BindRequestHandler.java:636)
        at org.apache.directory.server.ldap.handlers.request.BindRequestHandler.handle(BindRequestHandler.java:66)
        at org.apache.directory.server.ldap.handlers.LdapRequestHandler.handleMessage(LdapRequestHandler.java:193)
        at org.apache.directory.server.ldap.handlers.LdapRequestHandler.handleMessage(LdapRequestHandler.java:56)
        at org.apache.mina.handler.demux.DemuxingIoHandler.messageReceived(DemuxingIoHandler.java:221)
        at org.apache.directory.server.ldap.LdapProtocolHandler.messageReceived(LdapProtocolHandler.java:217)
        at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:854)
        at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
        at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
        at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:943)
        at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:74)
        at org.apache.mina.core.session.IoEvent.run(IoEvent.java:63)
        at org.apache.mina.filter.executor.UnorderedThreadPoolExecutor$Worker.runTask(UnorderedThreadPoolExecutor.java:475)
        at org.apache.mina.filter.executor.UnorderedThreadPoolExecutor$Worker.run(UnorderedThreadPoolExecutor.java:429)
        at java.lang.Thread.run(Thread.java:745)


I don't understand why the session object is null and how can the
getEffectivePrincipal() method call cause a NullPointerException.
Is this behaviour normal?
Is it possible to get the client IP in this interceptor?
If yes, then what should be the correct way to do it?

I would appreciate any help. I am totally get stuck.
Thank you!

Best regards,
David

Mime
View raw message