1 18 package org.apache.activemq.security; 19 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 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 ; 30 31 32 37 public class SimpleAuthenticationBroker extends BrokerFilter { 38 39 private final Map userPasswords; 40 private final Map userGroups; 41 private final CopyOnWriteArrayList securityContexts = new CopyOnWriteArrayList (); 42 43 public SimpleAuthenticationBroker(Broker next, Map userPasswords, Map userGroups) { 44 super(next); 45 this.userPasswords = userPasswords; 46 this.userGroups = userGroups; 47 } 48 49 public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception { 50 51 if( context.getSecurityContext()==null ) { 52 String pw = (String ) userPasswords.get(info.getUserName()); 54 if( pw == null || !pw.equals(info.getPassword()) ) 55 throw new SecurityException ("User name or password is invalid."); 56 57 final Set groups = (Set )userGroups.get(info.getUserName()); 58 SecurityContext s = new SecurityContext(info.getUserName()) { 59 public Set 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 error) throws Exception { 71 super.removeConnection(context, info, error); 72 if( securityContexts.remove(context.getSecurityContext()) ) { 73 context.setSecurityContext(null); 74 } 75 } 76 77 81 public void refresh() { 82 for (Iterator 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 |