]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ActiveMqSecurityBrokerPlugin.java
4e074aadc6abe5ef842de8a0d9af8945d1d69805
[lgpl/argeo-commons.git] / ActiveMqSecurityBrokerPlugin.java
1 package org.argeo.security.activemq;
2
3 import org.apache.activemq.broker.BrokerPluginSupport;
4 import org.apache.activemq.broker.ConnectionContext;
5 import org.apache.activemq.command.ConnectionInfo;
6 import org.argeo.ArgeoException;
7 import org.argeo.security.core.InternalAuthentication;
8 import org.springframework.security.Authentication;
9 import org.springframework.security.AuthenticationManager;
10 import org.springframework.security.context.SecurityContext;
11 import org.springframework.security.context.SecurityContextHolder;
12 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
13
14 public class ActiveMqSecurityBrokerPlugin extends BrokerPluginSupport {
15 // private final static Log log = LogFactory
16 // .getLog(ActiveMqSecurityBrokerPlugin.class);
17
18 private AuthenticationManager authenticationManager;
19 private String systemUsername = InternalAuthentication.DEFAULT_SYSTEM_USERNAME;
20 private String systemRole = InternalAuthentication.DEFAULT_SYSTEM_ROLE;
21
22 @Override
23 public void addConnection(ConnectionContext context, ConnectionInfo info)
24 throws Exception {
25 String username = info.getUserName();
26 if (username == null)
27 throw new ArgeoException("No user name provided");
28 String password = info.getPassword();
29 if (password == null) {
30 password = context.getConnection().getRemoteAddress().substring(1);
31 password = password.substring(0, password.lastIndexOf(':'));
32 }
33
34 SecurityContext securityContext = SecurityContextHolder.getContext();
35
36 final Authentication authRequest;
37 if (username.equals(systemUsername))
38 authRequest = new InternalAuthentication(password, username,
39 systemRole);
40 else
41 authRequest = new UsernamePasswordAuthenticationToken(username,
42 password);
43
44 final Authentication auth = authenticationManager
45 .authenticate(authRequest);
46 securityContext.setAuthentication(auth);
47 context.setSecurityContext(new ActiveMqSpringSecurityContext(
48 securityContext));
49
50 super.addConnection(context, info);
51 }
52
53 public void setAuthenticationManager(
54 AuthenticationManager authenticationManager) {
55 this.authenticationManager = authenticationManager;
56 }
57
58 public void setSystemUsername(String systemUsername) {
59 this.systemUsername = systemUsername;
60 }
61
62 public void setSystemRole(String systemRole) {
63 this.systemRole = systemRole;
64 }
65
66 }