KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > jee > listeners > ContextListener


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.jee.listeners;
29
30 import java.security.Policy JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.security.auth.login.Configuration JavaDoc;
36 import javax.servlet.ServletContext JavaDoc;
37 import javax.servlet.ServletContextEvent JavaDoc;
38 import javax.servlet.ServletContextListener JavaDoc;
39
40 import net.sf.jguard.core.CoreConstants;
41 import net.sf.jguard.core.authentication.configuration.JGuardConfiguration;
42 import net.sf.jguard.core.authorization.policy.AccessControllerUtils;
43 import net.sf.jguard.core.authorization.policy.MultipleAppPolicy;
44 import net.sf.jguard.core.authorization.policy.PolicyHelper;
45 import net.sf.jguard.ext.SecurityConstants;
46 import net.sf.jguard.ext.authentication.manager.AuthenticationHelper;
47 import net.sf.jguard.ext.authentication.manager.AuthenticationManager;
48 import net.sf.jguard.ext.authorization.AuthorizationHelper;
49 import net.sf.jguard.ext.authorization.manager.AuthorizationManager;
50 import net.sf.jguard.ext.java5.authentication.jmx.JMXHelper;
51 import net.sf.jguard.jee.authentication.http.HttpConstants;
52 import net.sf.jguard.jee.util.WebappUtil;
53
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56
57
58
59 /**
60  * handle webapp classloader reference in the
61  * JGuardPolicy classloaders repository.
62  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
63  * @author <a HREF="mailto:vberetti@users.sourceforge.net">Vincent Beretti</a>
64  */

65 public class ContextListener implements ServletContextListener JavaDoc {
66
67
68     private static final Log logger = LogFactory.getLog(ContextListener.class);
69
70
71     /**
72      * method called when the webapp shutdown:
73      * this method unregister the webapp in the JGuardPolicy repository.
74      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
75      */

76     public void contextDestroyed(ServletContextEvent JavaDoc servletContextEvent) {
77         logger.debug(" context destroyed ");
78         ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
79         if(Policy.getPolicy() instanceof MultipleAppPolicy){
80           MultipleAppPolicy policy = (MultipleAppPolicy) Policy.getPolicy();
81           policy.unregisterPermissionProvider(contextClassLoader);
82         }
83
84         servletContextEvent.getServletContext().removeAttribute(SecurityConstants.CAPTCHA_SERVICE);
85         servletContextEvent.getServletContext().removeAttribute(HttpConstants.USERS_IN_SESSION);
86     }
87
88     /**
89      * method called when the webapp start.
90      * install jGuard Configuration and Policy
91      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
92      */

93     public void contextInitialized(ServletContextEvent JavaDoc contextEvent) {
94         logger.debug("##### initializing ContextListener ... #####");
95         ServletContext JavaDoc context = contextEvent.getServletContext();
96         context.setAttribute(HttpConstants.USERS_IN_SESSION,new ArrayList JavaDoc());
97
98         String JavaDoc webappHomePath = WebappUtil.getWebappHomePath(context,"/");
99         String JavaDoc applicationName = context.getServletContextName();
100
101         context.setAttribute(CoreConstants.APPLICATION_NAME,applicationName);
102
103         initAuthentication(context, webappHomePath, applicationName);
104         Configuration JavaDoc configuration = (Configuration JavaDoc)context.getAttribute(HttpConstants.JGUARD_CONFIGURATION);
105         AccessControllerUtils authUtils = initAuthorization(context, webappHomePath, applicationName);
106         
107         //JMX MBeanServer creation
108
String JavaDoc enableJMX=(String JavaDoc)context.getInitParameter(SecurityConstants.ENABLE_JMX);
109         if(enableJMX != null && "true".equalsIgnoreCase(enableJMX)){
110             Map JavaDoc options = new HashMap JavaDoc();
111             options.put(SecurityConstants.MBEAN_SERVER_FOR_CONNECTOR,context.getInitParameter(SecurityConstants.MBEAN_SERVER_FOR_CONNECTOR));
112             options.put(SecurityConstants.RMI_REGISTRY_HOST,context.getInitParameter(SecurityConstants.RMI_REGISTRY_HOST));
113             options.put(SecurityConstants.RMI_REGISTRY_PORT,context.getInitParameter(SecurityConstants.RMI_REGISTRY_PORT));
114             options.put(SecurityConstants.MBEAN_SERVER_FOR_CONNECTOR,context.getInitParameter(SecurityConstants.MBEAN_SERVER_FOR_CONNECTOR));
115             JMXHelper.enableJMXSecurity(applicationName,options,configuration,authUtils);
116         }
117
118         logger.debug("##### ContextListener initialized #####");
119     }
120
121     private AccessControllerUtils initAuthorization(ServletContext JavaDoc context, String JavaDoc webappHomePath, String JavaDoc applicationName) {
122         // use default authorizationConfigurationLocation unless it is explicitly defined
123
String JavaDoc authorizationConfigurationLocation=null;
124         if(context.getInitParameter(HttpConstants.AUTHORIZATION_CONFIGURATION_LOCATION)!=null){
125             authorizationConfigurationLocation = WebappUtil.getWebappHomePath(context,context.getInitParameter(HttpConstants.AUTHORIZATION_CONFIGURATION_LOCATION));
126         }else{
127             authorizationConfigurationLocation=WebappUtil.getWebappHomePath(context,HttpConstants.DEFAULT_AUTHORIZATION_CONFIGURATION_LOCATION);
128         }
129         
130     
131         Map JavaDoc authorizationSettings = AuthorizationHelper.loadConfiguration(authorizationConfigurationLocation,webappHomePath);
132         //handle authorization scope
133
String JavaDoc authorizationScope = null;
134         if(authorizationSettings.get(SecurityConstants.SCOPE)!=null){
135             authorizationScope = (String JavaDoc)authorizationSettings.get(SecurityConstants.SCOPE);
136         }else{
137             authorizationScope = SecurityConstants.LOCAL_SCOPE;
138             context.setAttribute(SecurityConstants.AUTHORIZATION_SCOPE, authorizationScope);
139         }
140         logger.info(" authorization scope = "+authorizationScope);
141         
142         
143         AuthorizationManager authorizationManager = AuthorizationHelper.initAuthorization(authorizationSettings,applicationName);
144         AccessControllerUtils authorizationUtils = null;
145         MultipleAppPolicy policy = null;
146         if(SecurityConstants.JVM_SCOPE.equalsIgnoreCase(authorizationScope)){
147             PolicyHelper.installPolicyOnJVM();
148             //Register the new authorization manager with jguard policy provider
149
policy = (MultipleAppPolicy)Policy.getPolicy();
150             policy.registerPermissionProvider(Thread.currentThread().getContextClassLoader(),authorizationManager);
151             authorizationUtils = new AccessControllerUtils();
152         }else{
153             policy = new MultipleAppPolicy();
154             authorizationUtils = new AccessControllerUtils(policy);
155         }
156         
157         policy.registerPermissionProvider(Thread.currentThread().getContextClassLoader(),authorizationManager);
158         context.setAttribute(HttpConstants.AUTHZ_UTILS, authorizationUtils);
159         context.setAttribute(SecurityConstants.AUTHORIZATION_MANAGER,authorizationManager);
160         return authorizationUtils;
161     }
162
163     private void initAuthentication(ServletContext JavaDoc context, String JavaDoc webappHomePath, String JavaDoc applicationName) {
164         //use default authenticationConfigurationLocation unless it is explicitly defined
165
String JavaDoc authenticationConfigurationLocation=null;
166         if(context.getInitParameter(HttpConstants.AUTHENTICATION_CONFIGURATION_LOCATION)!=null){
167             authenticationConfigurationLocation = WebappUtil.getWebappHomePath(context,context.getInitParameter(HttpConstants.AUTHENTICATION_CONFIGURATION_LOCATION));
168         }else{
169             authenticationConfigurationLocation=WebappUtil.getWebappHomePath(context,HttpConstants.DEFAULT_AUTHENTICATION_CONFIGURATION_LOCATION);
170         }
171         
172         
173         Map JavaDoc authenticationSettings = AuthenticationHelper.loadConfiguration(authenticationConfigurationLocation,webappHomePath);
174         //handle authentication scope
175
String JavaDoc authenticationScope = null;
176         if(authenticationSettings.get(SecurityConstants.SCOPE)!=null){
177             authenticationScope = (String JavaDoc)authenticationSettings.get(SecurityConstants.SCOPE);
178         }else{
179             authenticationScope = SecurityConstants.LOCAL_SCOPE;
180             context.setAttribute(SecurityConstants.AUTHENTICATION_SCOPE, authenticationScope);
181         }
182         logger.info(" authentication scope = "+authenticationScope);
183
184         
185         
186         JGuardConfiguration jGuardConf = null;
187         logger.info(" authentication scope = "+authenticationScope);
188         if(SecurityConstants.JVM_SCOPE.equalsIgnoreCase(authenticationScope)){
189             jGuardConf = (JGuardConfiguration) Configuration.getConfiguration();
190         }else{
191             jGuardConf = new JGuardConfiguration();
192             context.setAttribute(HttpConstants.JGUARD_CONFIGURATION,jGuardConf);
193         }
194         
195         AuthenticationManager authenticationManager = AuthenticationHelper.initAuthentication(jGuardConf,authenticationSettings,applicationName);
196         context.setAttribute(SecurityConstants.AUTHENTICATION_MANAGER,authenticationManager);
197     }
198
199     
200 }
201
Popular Tags