2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.security
.activemq
;
19 import org
.apache
.activemq
.broker
.BrokerPluginSupport
;
20 import org
.apache
.activemq
.broker
.ConnectionContext
;
21 import org
.apache
.activemq
.command
.ConnectionInfo
;
22 import org
.argeo
.ArgeoException
;
23 import org
.argeo
.security
.core
.InternalAuthentication
;
24 import org
.springframework
.security
.Authentication
;
25 import org
.springframework
.security
.AuthenticationManager
;
26 import org
.springframework
.security
.context
.SecurityContext
;
27 import org
.springframework
.security
.context
.SecurityContextHolder
;
28 import org
.springframework
.security
.providers
.UsernamePasswordAuthenticationToken
;
30 @SuppressWarnings("unchecked")
31 public class ActiveMqSecurityBrokerPlugin
extends BrokerPluginSupport
{
32 // private final static Log log = LogFactory
33 // .getLog(ActiveMqSecurityBrokerPlugin.class);
35 private AuthenticationManager authenticationManager
;
36 private String systemUsername
= InternalAuthentication
.DEFAULT_SYSTEM_USERNAME
;
37 private String systemRole
= InternalAuthentication
.DEFAULT_SYSTEM_ROLE
;
40 public void addConnection(ConnectionContext context
, ConnectionInfo info
)
42 String username
= info
.getUserName();
44 throw new ArgeoException("No user name provided");
45 String password
= info
.getPassword();
46 if (password
== null) {
47 password
= context
.getConnection().getRemoteAddress().substring(1);
48 password
= password
.substring(0, password
.lastIndexOf(':'));
51 SecurityContext securityContext
= SecurityContextHolder
.getContext();
53 final Authentication authRequest
;
54 if (username
.equals(systemUsername
))
55 authRequest
= new InternalAuthentication(password
, username
,
58 authRequest
= new UsernamePasswordAuthenticationToken(username
,
61 final Authentication auth
= authenticationManager
62 .authenticate(authRequest
);
63 securityContext
.setAuthentication(auth
);
64 context
.setSecurityContext(new ActiveMqSpringSecurityContext(
67 super.addConnection(context
, info
);
70 public void setAuthenticationManager(
71 AuthenticationManager authenticationManager
) {
72 this.authenticationManager
= authenticationManager
;
75 public void setSystemUsername(String systemUsername
) {
76 this.systemUsername
= systemUsername
;
79 public void setSystemRole(String systemRole
) {
80 this.systemRole
= systemRole
;