]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-7093: Add filter to set username into a request attribute.
authorRay Lee <rhlee@berkeley.edu>
Sat, 13 May 2017 21:09:50 +0000 (14:09 -0700)
committerRay Lee <rhlee@berkeley.edu>
Sat, 13 May 2017 21:10:39 +0000 (14:10 -0700)
services/JaxRsServiceProvider/src/main/webapp/WEB-INF/applicationContext-security.xml
services/authentication/service/src/main/java/org/collectionspace/authentication/spring/CSpaceUserAttributeFilter.java [new file with mode: 0644]

index 6617ed90aad6b5ed137b11d7693d3a8a4b0655fd..a1f23a938139b1997089164f0e775be6eb0cddbd 100644 (file)
@@ -66,6 +66,9 @@
         <sec:intercept-url method="OPTIONS" pattern="/**" access="isAnonymous()"/>
         <sec:cors configuration-source-ref="corsSource" />
         
+        <!-- Insert the username from the security context into a request attribute for logging -->
+        <sec:custom-filter ref="userAttributeFilter" after="SECURITY_CONTEXT_FILTER" />
+
         <!-- Handle token auth -->
         <sec:custom-filter ref="oauthResourceServerFilter" before="PRE_AUTH_FILTER" />
     </sec:http>
             </util:map>
         </property>
     </bean>
+
+    <bean id="userAttributeFilter"
+        class="org.collectionspace.authentication.spring.CSpaceUserAttributeFilter">
+    </bean>
 </beans>
diff --git a/services/authentication/service/src/main/java/org/collectionspace/authentication/spring/CSpaceUserAttributeFilter.java b/services/authentication/service/src/main/java/org/collectionspace/authentication/spring/CSpaceUserAttributeFilter.java
new file mode 100644 (file)
index 0000000..230709b
--- /dev/null
@@ -0,0 +1,34 @@
+package org.collectionspace.authentication.spring;
+
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+/**
+ * A filter that sets a request attribute containing the username of the
+ * authenticated CollectionSpace user. This attribute may then be used
+ * to log the username via tomcat's standard access log valve.
+ */
+public class CSpaceUserAttributeFilter extends OncePerRequestFilter {
+    public static final String ATTRIBUTE_NAME = "org.collectionspace.authentication.user";
+    
+    @Override
+    protected void doFilterInternal(HttpServletRequest request,
+            HttpServletResponse response, FilterChain chain)
+            throws ServletException, IOException {
+        chain.doFilter(request, response);
+
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+
+        if (authentication != null) {
+            request.setAttribute(ATTRIBUTE_NAME, authentication.getName());
+        }
+    }
+}