001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.security; 018 019import java.security.Principal; 020import java.util.Set; 021 022import javax.security.auth.Subject; 023import javax.security.auth.login.LoginContext; 024 025import org.apache.activemq.broker.Broker; 026import org.apache.activemq.broker.ConnectionContext; 027import org.apache.activemq.command.ConnectionInfo; 028import org.apache.activemq.jaas.JassCredentialCallbackHandler; 029 030/** 031 * Logs a user in using JAAS. 032 * 033 * 034 */ 035public class JaasAuthenticationBroker extends AbstractAuthenticationBroker { 036 037 private final String jassConfiguration; 038 039 public JaasAuthenticationBroker(Broker next, String jassConfiguration) { 040 super(next); 041 this.jassConfiguration = jassConfiguration; 042 } 043 044 static class JaasSecurityContext extends SecurityContext { 045 046 private final Subject subject; 047 048 public JaasSecurityContext(String userName, Subject subject) { 049 super(userName); 050 this.subject = subject; 051 } 052 053 @Override 054 public Set<Principal> getPrincipals() { 055 return subject.getPrincipals(); 056 } 057 } 058 059 @Override 060 public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception { 061 062 if (context.getSecurityContext() == null) { 063 // Set the TCCL since it seems JAAS needs it to find the login 064 // module classes. 065 ClassLoader original = Thread.currentThread().getContextClassLoader(); 066 Thread.currentThread().setContextClassLoader(JaasAuthenticationBroker.class.getClassLoader()); 067 try { 068 // Do the login. 069 try { 070 JassCredentialCallbackHandler callback = new JassCredentialCallbackHandler(info 071 .getUserName(), info.getPassword()); 072 LoginContext lc = new LoginContext(jassConfiguration, callback); 073 lc.login(); 074 Subject subject = lc.getSubject(); 075 076 SecurityContext s = new JaasSecurityContext(info.getUserName(), subject); 077 context.setSecurityContext(s); 078 securityContexts.add(s); 079 } catch (Exception e) { 080 throw (SecurityException)new SecurityException("User name [" + info.getUserName() + "] or password is invalid.") 081 .initCause(e); 082 } 083 } finally { 084 Thread.currentThread().setContextClassLoader(original); 085 } 086 } 087 super.addConnection(context, info); 088 } 089}