KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.activemq.broker.Broker;
25 import org.apache.activemq.broker.BrokerFilter;
26 import org.apache.activemq.broker.ConnectionContext;
27 import org.apache.activemq.command.ConnectionInfo;
28
29 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
30
31
32 /**
33  * Handles authenticating a users against a simple user name/password map.
34  *
35  * @version $Revision$
36  */

37 public class SimpleAuthenticationBroker extends BrokerFilter {
38     
39     private final Map JavaDoc userPasswords;
40     private final Map JavaDoc userGroups;
41     private final CopyOnWriteArrayList JavaDoc securityContexts = new CopyOnWriteArrayList JavaDoc();
42     
43     public SimpleAuthenticationBroker(Broker next, Map JavaDoc userPasswords, Map JavaDoc userGroups) {
44         super(next);
45         this.userPasswords = userPasswords;
46         this.userGroups = userGroups;
47     }
48     
49     public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception JavaDoc {
50
51         if( context.getSecurityContext()==null ) {
52             // Check the username and password.
53
String JavaDoc pw = (String JavaDoc) userPasswords.get(info.getUserName());
54             if( pw == null || !pw.equals(info.getPassword()) )
55                 throw new SecurityException JavaDoc("User name or password is invalid.");
56         
57             final Set JavaDoc groups = (Set JavaDoc)userGroups.get(info.getUserName());
58             SecurityContext s = new SecurityContext(info.getUserName()) {
59                 public Set JavaDoc getPrincipals() {
60                     return groups;
61                 }
62             };
63             
64             context.setSecurityContext(s);
65             securityContexts.add(s);
66         }
67         super.addConnection(context, info);
68     }
69     
70     public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable JavaDoc error) throws Exception JavaDoc {
71         super.removeConnection(context, info, error);
72         if( securityContexts.remove(context.getSecurityContext()) ) {
73             context.setSecurityContext(null);
74         }
75     }
76     
77     /**
78      * Previously logged in users may no longer have the same access anymore. Refresh
79      * all the logged into users.
80      */

81     public void refresh() {
82         for (Iterator JavaDoc iter = securityContexts.iterator(); iter.hasNext();) {
83             SecurityContext sc = (SecurityContext) iter.next();
84             sc.getAuthorizedReadDests().clear();
85             sc.getAuthorizedWriteDests().clear();
86         }
87     }
88     
89 }
90
Popular Tags