SHIRO-467: improving exception logging
Project: http://git-wip-us.apache.org/repos/asf/shiro/repo Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/56a5a0f8 Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/56a5a0f8 Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/56a5a0f8 Branch: refs/heads/1.2.x Commit: 56a5a0f8350ced59319937a9ad9cc9ad3e2c566d Parents: 0655e84 Author: bdemers <[email protected]> Authored: Fri Mar 25 16:26:31 2016 -0400 Committer: bdemers <[email protected]> Committed: Wed Apr 13 11:06:32 2016 -0400 ---------------------------------------------------------------------- .../shiro/authc/AbstractAuthenticator.java | 4 +- .../shiro/authc/AbstractAuthenticatorTest.java | 39 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/shiro/blob/56a5a0f8/core/src/main/java/org/apache/shiro/authc/AbstractAuthenticator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/shiro/authc/AbstractAuthenticator.java b/core/src/main/java/org/apache/shiro/authc/AbstractAuthenticator.java index b8bba7c..f55a7d8 100644 --- a/core/src/main/java/org/apache/shiro/authc/AbstractAuthenticator.java +++ b/core/src/main/java/org/apache/shiro/authc/AbstractAuthenticator.java @@ -188,7 +188,7 @@ public abstract class AbstractAuthenticator implements Authenticator, LogoutAwar public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException { if (token == null) { - throw new IllegalArgumentException("Method argumet (authentication token) cannot be null."); + throw new IllegalArgumentException("Method argument (authentication token) cannot be null."); } log.trace("Authentication attempt received for token [{}]", token); @@ -212,6 +212,8 @@ public abstract class AbstractAuthenticator implements Authenticator, LogoutAwar String msg = "Authentication failed for token submission [" + token + "]. Possible unexpected " + "error? (Typical or expected login exceptions should extend from AuthenticationException)."; ae = new AuthenticationException(msg, t); + if (log.isWarnEnabled()) + log.warn(msg, t); } try { notifyFailure(token, ae); http://git-wip-us.apache.org/repos/asf/shiro/blob/56a5a0f8/core/src/test/java/org/apache/shiro/authc/AbstractAuthenticatorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/shiro/authc/AbstractAuthenticatorTest.java b/core/src/test/java/org/apache/shiro/authc/AbstractAuthenticatorTest.java index f2350df..6d54b38 100644 --- a/core/src/test/java/org/apache/shiro/authc/AbstractAuthenticatorTest.java +++ b/core/src/test/java/org/apache/shiro/authc/AbstractAuthenticatorTest.java @@ -18,9 +18,16 @@ */ package org.apache.shiro.authc; +import org.apache.log4j.Appender; +import org.apache.log4j.Layout; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; +import org.apache.log4j.WriterAppender; import org.junit.Before; import org.junit.Test; +import java.io.ByteArrayOutputStream; + import static org.easymock.EasyMock.*; import static org.junit.Assert.*; @@ -152,4 +159,36 @@ public class AbstractAuthenticatorTest { abstractAuthenticator.authenticate(token); } + @Test + public void logExceptionAfterDoAuthenticateThrowsNonAuthenticationException() { + Logger logger = Logger.getLogger(AbstractAuthenticator.class); + + // NOTE: log4j is a test dependency + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Layout layout = new SimpleLayout(); + Appender appender = new WriterAppender(layout, out); + logger.addAppender(appender); + + final String expectedExceptionMessage = "exception thrown for test logExceptionAfterDoAuthenticateThrowsNonAuthenticationException"; + + abstractAuthenticator = new AbstractAuthenticator() { + protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws AuthenticationException { + throw new IllegalArgumentException(expectedExceptionMessage); + } + }; + AuthenticationToken token = newToken(); + + try{ + abstractAuthenticator.authenticate(token); + fail("the expected AuthenticationException was not thrown"); + }catch(AuthenticationException expectedException){ + } + + String logMsg = out.toString(); + assertTrue(logMsg.contains("WARN")); + assertTrue(logMsg.contains("java.lang.IllegalArgumentException: "+ expectedExceptionMessage)); + + logger.removeAppender(appender); + } + }
