You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by bd...@apache.org on 2016/04/13 17:21:41 UTC

[2/5] shiro git commit: SHIRO-467: improving exception logging

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/549ec9dd
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/549ec9dd
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/549ec9dd

Branch: refs/heads/master
Commit: 549ec9dd585430996a6351d07c649ac23f0fef83
Parents: e21e986
Author: bdemers <bd...@apache.org>
Authored: Fri Mar 25 16:26:31 2016 -0400
Committer: bdemers <bd...@apache.org>
Committed: Fri Mar 25 16:26:31 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/549ec9dd/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/549ec9dd/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);
+    }
+
 }