]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
230709b2300cbc2bec83f6d4a326d045b2cc1a39
[tmp/jakarta-migration.git] /
1 package org.collectionspace.authentication.spring;
2
3 import java.io.IOException;
4
5 import javax.servlet.FilterChain;
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.springframework.security.core.Authentication;
11 import org.springframework.security.core.context.SecurityContextHolder;
12 import org.springframework.web.filter.OncePerRequestFilter;
13
14 /**
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.
18  */
19 public class CSpaceUserAttributeFilter extends OncePerRequestFilter {
20     public static final String ATTRIBUTE_NAME = "org.collectionspace.authentication.user";
21     
22     @Override
23     protected void doFilterInternal(HttpServletRequest request,
24             HttpServletResponse response, FilterChain chain)
25             throws ServletException, IOException {
26         chain.doFilter(request, response);
27
28         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
29
30         if (authentication != null) {
31             request.setAttribute(ATTRIBUTE_NAME, authentication.getName());
32         }
33     }
34 }