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 public class ActiveMqSecurityBrokerPlugin
extends BrokerPluginSupport
{
31 // private final static Log log = LogFactory
32 // .getLog(ActiveMqSecurityBrokerPlugin.class);
34 private AuthenticationManager authenticationManager
;
35 private String systemUsername
= InternalAuthentication
.DEFAULT_SYSTEM_USERNAME
;
36 private String systemRole
= InternalAuthentication
.DEFAULT_SYSTEM_ROLE
;
39 public void addConnection(ConnectionContext context
, ConnectionInfo info
)
41 String username
= info
.getUserName();
43 throw new ArgeoException("No user name provided");
44 String password
= info
.getPassword();
45 if (password
== null) {
46 password
= context
.getConnection().getRemoteAddress().substring(1);
47 password
= password
.substring(0, password
.lastIndexOf(':'));
50 SecurityContext securityContext
= SecurityContextHolder
.getContext();
52 final Authentication authRequest
;
53 if (username
.equals(systemUsername
))
54 authRequest
= new InternalAuthentication(password
, username
,
57 authRequest
= new UsernamePasswordAuthenticationToken(username
,
60 final Authentication auth
= authenticationManager
61 .authenticate(authRequest
);
62 securityContext
.setAuthentication(auth
);
63 context
.setSecurityContext(new ActiveMqSpringSecurityContext(
66 super.addConnection(context
, info
);
69 public void setAuthenticationManager(
70 AuthenticationManager authenticationManager
) {
71 this.authenticationManager
= authenticationManager
;
74 public void setSystemUsername(String systemUsername
) {
75 this.systemUsername
= systemUsername
;
78 public void setSystemRole(String systemRole
) {
79 this.systemRole
= systemRole
;