1 18 19 package org.apache.activemq.security; 20 21 import org.apache.activemq.broker.Broker; 22 import org.apache.activemq.broker.BrokerFilter; 23 import org.apache.activemq.broker.ConnectionContext; 24 import org.apache.activemq.command.ConnectionInfo; 25 import org.apache.activemq.jaas.JaasCertificateCallbackHandler; 26 import org.apache.activemq.jaas.UserPrincipal; 27 import org.apache.activemq.security.JaasAuthenticationBroker.JaasSecurityContext; 28 29 import java.security.Principal ; 30 import java.security.cert.X509Certificate ; 31 import java.util.Iterator ; 32 33 import javax.security.auth.Subject ; 34 import javax.security.auth.callback.CallbackHandler ; 35 import javax.security.auth.login.LoginContext ; 36 37 46 public class JaasCertificateAuthenticationBroker extends BrokerFilter { 47 private final String jaasConfiguration; 48 49 55 public JaasCertificateAuthenticationBroker(Broker next, String jaasConfiguration) { 56 super(next); 57 58 this.jaasConfiguration = jaasConfiguration; 59 } 60 61 71 public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception { 72 73 if (context.getSecurityContext() == null) { 74 if (!( info.getTransportContext() instanceof X509Certificate [] )) { 75 throw new SecurityException ("Unable to authenticate transport without SSL certificate."); 76 } 77 78 ClassLoader original = Thread.currentThread().getContextClassLoader(); 80 Thread.currentThread().setContextClassLoader(JaasAuthenticationBroker.class.getClassLoader()); 81 try { 82 try { 84 CallbackHandler callback = 85 new JaasCertificateCallbackHandler((X509Certificate [])info.getTransportContext()); 86 LoginContext lc = new LoginContext (jaasConfiguration, callback); 87 lc.login(); 88 Subject subject = lc.getSubject(); 89 90 String dnName = ""; 91 92 for (Iterator iter = subject.getPrincipals().iterator(); iter.hasNext(); ) { 93 Principal nextPrincipal = (Principal)iter.next(); 94 if (nextPrincipal instanceof UserPrincipal) { 95 dnName = ((UserPrincipal)nextPrincipal).getName(); 96 break; 97 } 98 } 99 SecurityContext s = new JaasCertificateSecurityContext( 100 dnName, subject, (X509Certificate [])info.getTransportContext()); 101 context.setSecurityContext(s); 102 } catch (Exception e) { 103 throw new SecurityException ("User name or password is invalid: " + e.getMessage(), e); 104 } 105 } finally { 106 Thread.currentThread().setContextClassLoader(original); 107 } 108 } 109 super.addConnection(context, info); 110 } 111 112 115 public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable error) throws Exception { 116 super.removeConnection(context, info, error); 117 118 context.setSecurityContext(null); 119 } 120 } 121 | Popular Tags |