KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > acegi > GuestProcessingFilter


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.acegi;
18
19 import info.jtrac.Jtrac;
20 import info.jtrac.domain.Space;
21 import info.jtrac.domain.User;
22 import info.jtrac.util.UserUtils;
23 import java.io.IOException JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.servlet.Filter JavaDoc;
26 import javax.servlet.FilterChain JavaDoc;
27 import javax.servlet.FilterConfig JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletRequest JavaDoc;
30 import javax.servlet.ServletResponse JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import org.acegisecurity.context.SecurityContextHolder;
34 import org.acegisecurity.ui.AuthenticationDetailsSource;
35 import org.acegisecurity.ui.AuthenticationDetailsSourceImpl;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * Custom Acegi Servlet Filter designed to plug along with other Acegi
41  * Filters and implement our custom "anonymous" Authentication strategy
42  * This allows users to browse projects that have "Guest Allowed"
43  * without signing on.
44  */

45 public class GuestProcessingFilter implements Filter JavaDoc {
46     
47     private Jtrac jtrac;
48     private AuthenticationDetailsSource authenticationDetailsSource = new AuthenticationDetailsSourceImpl();
49     
50     public void setJtrac(Jtrac jtrac) {
51         this.jtrac = jtrac;
52     }
53     
54     private final Log logger = LogFactory.getLog(getClass());
55     
56     public void init(FilterConfig JavaDoc filterConfig) {
57         // ignored
58
}
59
60     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
61         if (SecurityContextHolder.getContext().getAuthentication() == null) {
62             List JavaDoc<Space> spaces = jtrac.findSpacesWhereGuestAllowed();
63             if (spaces.size() > 0) {
64                 User guestUser = new User();
65                 guestUser.setLoginName("guest");
66                 guestUser.setName("Guest");
67                 guestUser.addSpaceWithRole(null, "ROLE_GUEST");
68                 for (Space space : spaces) {
69                     guestUser.addSpaceWithRole(space, "ROLE_GUEST");
70                 }
71                 GuestAuthenticationToken authentication = new GuestAuthenticationToken(guestUser, guestUser.getAuthorities());
72                 authentication.setDetails(authenticationDetailsSource.buildDetails((HttpServletRequest JavaDoc) request));
73                 SecurityContextHolder.getContext().setAuthentication(authentication);
74                 if (logger.isDebugEnabled()) {
75                     logger.debug("populated SecurityContextHolder with guest user: " + guestUser);
76                 }
77                 // this only happens once, see the hack in header.jsp for more
78
request.setAttribute("principal", authentication.getPrincipal());
79                 UserUtils.refreshLocale((HttpServletRequest JavaDoc) request, (HttpServletResponse JavaDoc) response, jtrac.getDefaultLocale());
80             }
81         }
82         chain.doFilter(request, response);
83     }
84
85     public void destroy() {
86         // ignored
87
}
88     
89 }
90
Popular Tags