]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ActiveMqSecurityBrokerPlugin.java
1a5a6cd035d2b3200f66644ea48360dfddb7c31c
[lgpl/argeo-commons.git] / ActiveMqSecurityBrokerPlugin.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.security.activemq;
18
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;
29
30 @SuppressWarnings("unchecked")
31 public class ActiveMqSecurityBrokerPlugin extends BrokerPluginSupport {
32 // private final static Log log = LogFactory
33 // .getLog(ActiveMqSecurityBrokerPlugin.class);
34
35 private AuthenticationManager authenticationManager;
36 private String systemUsername = InternalAuthentication.DEFAULT_SYSTEM_USERNAME;
37 private String systemRole = InternalAuthentication.DEFAULT_SYSTEM_ROLE;
38
39 @Override
40 public void addConnection(ConnectionContext context, ConnectionInfo info)
41 throws Exception {
42 String username = info.getUserName();
43 if (username == null)
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(':'));
49 }
50
51 SecurityContext securityContext = SecurityContextHolder.getContext();
52
53 final Authentication authRequest;
54 if (username.equals(systemUsername))
55 authRequest = new InternalAuthentication(password, username,
56 systemRole);
57 else
58 authRequest = new UsernamePasswordAuthenticationToken(username,
59 password);
60
61 final Authentication auth = authenticationManager
62 .authenticate(authRequest);
63 securityContext.setAuthentication(auth);
64 context.setSecurityContext(new ActiveMqSpringSecurityContext(
65 securityContext));
66
67 super.addConnection(context, info);
68 }
69
70 public void setAuthenticationManager(
71 AuthenticationManager authenticationManager) {
72 this.authenticationManager = authenticationManager;
73 }
74
75 public void setSystemUsername(String systemUsername) {
76 this.systemUsername = systemUsername;
77 }
78
79 public void setSystemRole(String systemRole) {
80 this.systemRole = systemRole;
81 }
82
83 }