Question

The situation is the following: there is a framework that is extended by my application. So I have no main() or any other entry points declared by me (I override methods provided by that framework and it arranges everything else). This framework can be made secure; the security is built on top of apache mina.

When an unsecure connection is made to the app, mina throws an exception, here's the stack trace:

Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
        at sun.security.ssl.EngineInputRecord.bytesInCompletePacket(EngineInputRecord.java:171) ~[na:1.7.0_21]
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:845) ~[na:1.7.0_21]
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758) ~[na:1.7.0_21]
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624) ~[na:1.7.0_21]
        at org.apache.mina.filter.support.SSLHandler.unwrap0(SSLHandler.java:657) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.support.SSLHandler.unwrapHandshake(SSLHandler.java:613) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.support.SSLHandler.handshake(SSLHandler.java:493) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.support.SSLHandler.messageReceived(SSLHandler.java:306) ~[mina-filter-ssl-1.1.7.jar:na]
        at org.apache.mina.filter.SSLFilter.messageReceived(SSLFilter.java:392) ~[mina-filter-ssl-1.1.7.jar:na]
        ... 12 common frames omitted

I wanted to catch this using an AOP trick, so I can inform the user in a nice way, and can send an alert. I came up with this aspect:

@Aspect
public class SSLExceptionCatcherAdvice {

    private static final Logger LOG = LoggerFactory.getLogger(SSLExceptionCatcherAdvice.class);

    @Around("execution(* *.unwrap(..))")
    public void catchException(ProceedingJoinPoint pjp) {
        try {
            pjp.proceed();
        } catch (Throwable exception) {
            LOG.info("########################################");
        }
    }

}

But, it does not get invoked at all. The aspect is fine, btw, it can catch other method invocations from my codebase.

Any advice on this? Thx in advance.

Was it helpful?

Solution

Spring by default uses a proxy based AOP solution and as a result of that only Spring managed beans will be able to have those aspects applied. It will not work on non-spring managed beans. See this section of the reference guide.

You are trying to intercept the exection on a javax. package which is a special case and will only work in a load-time weaving environment and not with a proxy-based or compile-time based solution. Also weaving them with loadtime weaving might also be tricky as those classes are probably already loaded before loadtime-weaving kicks in.

The aspect is fine, btw, it can catch other method invocations from my codebase.

Actually your aspect is flawed, an @Around advice should always return Object and should always return the result of calling the proceed() method (unless you are rethrowing the Exception. Your aspect breaks properly returning the result and now effectivly every method returns null.

Links:

  1. Proxying Mechanisms | reference guide
  2. Load-time weaving with AspectJ in the Spring Framework | reference guide
  3. Around advice | reference guide
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top