1 package org.collectionspace.authentication.spring;
3 import java.io.IOException;
5 import javax.servlet.FilterChain;
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
10 import org.springframework.security.core.Authentication;
11 import org.springframework.security.core.context.SecurityContextHolder;
12 import org.springframework.web.filter.OncePerRequestFilter;
15 * A filter that sets a request attribute containing the username of the
16 * authenticated CollectionSpace user. This attribute may then be used
17 * to log the username via tomcat's standard access log valve.
19 public class CSpaceUserAttributeFilter extends OncePerRequestFilter {
20 public static final String ATTRIBUTE_NAME = "org.collectionspace.authentication.user";
23 protected void doFilterInternal(HttpServletRequest request,
24 HttpServletResponse response, FilterChain chain)
25 throws ServletException, IOException {
26 chain.doFilter(request, response);
28 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
30 if (authentication != null) {
31 request.setAttribute(ATTRIBUTE_NAME, authentication.getName());