KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > security > JaasAuthenticationBroker


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.security;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.security.auth.Subject JavaDoc;
24 import javax.security.auth.login.LoginContext JavaDoc;
25
26 import org.apache.activemq.broker.Broker;
27 import org.apache.activemq.broker.BrokerFilter;
28 import org.apache.activemq.broker.ConnectionContext;
29 import org.apache.activemq.command.ConnectionInfo;
30
31 import org.apache.activemq.jaas.JassCredentialCallbackHandler;
32
33 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
34
35
36 /**
37  * Logs a user in using JAAS.
38  *
39  * @version $Revision$
40  */

41 public class JaasAuthenticationBroker extends BrokerFilter {
42
43     private final String JavaDoc jassConfiguration;
44     private final CopyOnWriteArrayList JavaDoc securityContexts = new CopyOnWriteArrayList JavaDoc();
45
46     public JaasAuthenticationBroker(Broker next, String JavaDoc jassConfiguration) {
47         super(next);
48         this.jassConfiguration = jassConfiguration;
49     }
50     
51     static class JaasSecurityContext extends SecurityContext {
52
53         private final Subject JavaDoc subject;
54
55         public JaasSecurityContext(String JavaDoc userName, Subject JavaDoc subject) {
56             super(userName);
57             this.subject = subject;
58         }
59
60         public Set JavaDoc getPrincipals() {
61             return subject.getPrincipals();
62         }
63         
64     }
65     
66     public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception JavaDoc {
67
68         if( context.getSecurityContext()==null ) {
69             // Set the TCCL since it seems JAAS needs it to find the login module classes.
70
ClassLoader JavaDoc original = Thread.currentThread().getContextClassLoader();
71             Thread.currentThread().setContextClassLoader(JaasAuthenticationBroker.class.getClassLoader());
72             try {
73                 // Do the login.
74
try {
75                     JassCredentialCallbackHandler callback = new JassCredentialCallbackHandler(info.getUserName(), info.getPassword());
76                     LoginContext JavaDoc lc = new LoginContext JavaDoc(jassConfiguration, callback);
77                     lc.login();
78                     Subject JavaDoc subject = lc.getSubject();
79                     
80                     SecurityContext s = new JaasSecurityContext(info.getUserName(), subject);
81                     context.setSecurityContext(s);
82                     securityContexts.add(s);
83                 } catch (Exception JavaDoc e) {
84                     throw (SecurityException JavaDoc)new SecurityException JavaDoc("User name or password is invalid.").initCause(e);
85                 }
86             } finally {
87                 Thread.currentThread().setContextClassLoader(original);
88             }
89         }
90         super.addConnection(context, info);
91     }
92     
93     public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable JavaDoc error) throws Exception JavaDoc {
94         super.removeConnection(context, info, error);
95         if( securityContexts.remove(context.getSecurityContext()) ) {
96             context.setSecurityContext(null);
97         }
98     }
99     
100     /**
101      * Previously logged in users may no longer have the same access anymore. Refresh
102      * all the logged into users.
103      */

104     public void refresh() {
105         for (Iterator JavaDoc iter = securityContexts.iterator(); iter.hasNext();) {
106             SecurityContext sc = (SecurityContext) iter.next();
107             sc.getAuthorizedReadDests().clear();
108             sc.getAuthorizedWriteDests().clear();
109         }
110     }
111 }
112
Popular Tags