incubator-shiro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbunt...@apache.org
Subject svn commit: r1433407 - in /shiro/trunk: core/src/main/java/org/apache/shiro/util/ThreadContext.java support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java
Date Tue, 15 Jan 2013 13:32:12 GMT
Author: jbunting
Date: Tue Jan 15 13:32:12 2013
New Revision: 1433407

URL: http://svn.apache.org/viewvc?rev=1433407&view=rev
Log:
SHIRO-391 -- ThreadContext.bind(Subject) also binds the subject's primary principal to slf4j's
MDC

Modified:
    shiro/trunk/core/src/main/java/org/apache/shiro/util/ThreadContext.java
    shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java

Modified: shiro/trunk/core/src/main/java/org/apache/shiro/util/ThreadContext.java
URL: http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/util/ThreadContext.java?rev=1433407&r1=1433406&r2=1433407&view=diff
==============================================================================
--- shiro/trunk/core/src/main/java/org/apache/shiro/util/ThreadContext.java (original)
+++ shiro/trunk/core/src/main/java/org/apache/shiro/util/ThreadContext.java Tue Jan 15 13:32:12
2013
@@ -22,6 +22,7 @@ import org.apache.shiro.mgt.SecurityMana
 import org.apache.shiro.subject.Subject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -52,6 +53,12 @@ public abstract class ThreadContext {
     public static final String SECURITY_MANAGER_KEY = ThreadContext.class.getName() + "_SECURITY_MANAGER_KEY";
     public static final String SUBJECT_KEY = ThreadContext.class.getName() + "_SUBJECT_KEY";
 
+	/**
+     * The key of the subject in SLF4Js Mapped Diagnostic Context ({@link MDC}). This can
be used to
+     * show the subject in the logs. The subject is displayed in the logs using the pattern
<pre>%X{shiroSubject}</pre>.
+     */
+    private static final String SUBJECT_KEY_MDC = "shiroSubject";
+
     private static final ThreadLocal<Map<Object, Object>> resources = new InheritableThreadLocalMap<Map<Object,
Object>>();
 
     /**
@@ -285,6 +292,7 @@ public abstract class ThreadContext {
     public static void bind(Subject subject) {
         if (subject != null) {
             put(SUBJECT_KEY, subject);
+            MDC.put(SUBJECT_KEY_MDC, String.valueOf(subject.getPrincipal()));
         }
     }
 
@@ -303,6 +311,7 @@ public abstract class ThreadContext {
      * @since 0.2
      */
     public static Subject unbindSubject() {
+        MDC.remove(SUBJECT_KEY_MDC);
         return (Subject) remove(SUBJECT_KEY);
     }
     

Modified: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java?rev=1433407&r1=1433406&r2=1433407&view=diff
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java
(original)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java
Tue Jan 15 13:32:12 2013
@@ -34,14 +34,14 @@ public class ShiroSessionScopeTest {
     public void testScope() throws Exception {
         Subject subject = createMock(Subject.class);
         try {
-            ThreadContext.bind(subject);
-
             final Key<SomeClass> key = Key.get(SomeClass.class);
             Provider<SomeClass> mockProvider = createMock(Provider.class);
             Session session = createMock(Session.class);
 
             SomeClass retuned = new SomeClass();
 
+            expect(subject.getPrincipal()).andReturn("testUser").anyTimes();
+
             expect(subject.getSession()).andReturn(session);
             expect(session.getAttribute(key)).andReturn(null);
             expect(mockProvider.get()).andReturn(retuned);
@@ -52,6 +52,8 @@ public class ShiroSessionScopeTest {
 
             replay(subject, mockProvider, session);
 
+            ThreadContext.bind(subject);
+
             ShiroSessionScope underTest = new ShiroSessionScope();
 
             // first time the session doesn't contain it, we expect the provider to be invoked



Mime
View raw message