]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/activemq/ActiveMqSecurityBrokerPlugin.java
Improve Security
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / activemq / 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 public class ActiveMqSecurityBrokerPlugin extends BrokerPluginSupport {
31 // private final static Log log = LogFactory
32 // .getLog(ActiveMqSecurityBrokerPlugin.class);
33
34 private AuthenticationManager authenticationManager;
35 private String systemUsername = InternalAuthentication.DEFAULT_SYSTEM_USERNAME;
36 private String systemRole = InternalAuthentication.DEFAULT_SYSTEM_ROLE;
37
38 @Override
39 public void addConnection(ConnectionContext context, ConnectionInfo info)
40 throws Exception {
41 String username = info.getUserName();
42 if (username == null)
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(':'));
48 }
49
50 SecurityContext securityContext = SecurityContextHolder.getContext();
51
52 final Authentication authRequest;
53 if (username.equals(systemUsername))
54 authRequest = new InternalAuthentication(password, username,
55 systemRole);
56 else
57 authRequest = new UsernamePasswordAuthenticationToken(username,
58 password);
59
60 final Authentication auth = authenticationManager
61 .authenticate(authRequest);
62 securityContext.setAuthentication(auth);
63 context.setSecurityContext(new ActiveMqSpringSecurityContext(
64 securityContext));
65
66 super.addConnection(context, info);
67 }
68
69 public void setAuthenticationManager(
70 AuthenticationManager authenticationManager) {
71 this.authenticationManager = authenticationManager;
72 }
73
74 public void setSystemUsername(String systemUsername) {
75 this.systemUsername = systemUsername;
76 }
77
78 public void setSystemRole(String systemRole) {
79 this.systemRole = systemRole;
80 }
81
82 }