01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.examples.remote.security;
10:
11: import java.util.Collections;
12: import java.util.HashMap;
13: import java.util.HashSet;
14: import java.util.Map;
15: import java.util.Set;
16: import javax.management.MBeanServerConnection;
17: import javax.management.ObjectName;
18: import javax.management.remote.JMXConnector;
19: import javax.management.remote.JMXConnectorFactory;
20: import javax.management.remote.JMXPrincipal;
21: import javax.management.remote.JMXServiceURL;
22: import javax.security.auth.Subject;
23:
24: /**
25: * This example shows how to setup a JSR 160 connector client that connects to
26: * a secured JSR 160 connector server, and that uses the subject delegation features
27: * defined by JSR 160.
28: * Refer to the MX4J documentation on how to run this example and on how it
29: * works: this example is described in details.
30: *
31: * @version $Revision: 1.1 $
32: * @see Server
33: */
34: public class Client {
35: public static void main(String[] args) throws Exception {
36: // The address of the connector server
37: JMXServiceURL url = new JMXServiceURL("rmi", "localhost", 0,
38: "/jndi/jmx");
39:
40: // The credentials are passed via the environment Map
41: Map environment = new HashMap();
42: String[] credentials = new String[] { "guest", "guest" };
43: environment.put(JMXConnector.CREDENTIALS, credentials);
44:
45: // Connect to the server
46: JMXConnector cntor = JMXConnectorFactory.connect(url,
47: environment);
48:
49: // Create a subject to delegate to
50: JMXPrincipal principal = new JMXPrincipal("anotherGuest");
51: Set principals = new HashSet();
52: principals.add(principal);
53: Subject delegate = new Subject(true, principals,
54: Collections.EMPTY_SET, Collections.EMPTY_SET);
55:
56: // Get two MBeanServerConnection: one that uses the 'guest' principal directly,
57: // the second that uses the 'guest' user but delegates to another principal.
58: MBeanServerConnection connection = cntor
59: .getMBeanServerConnection();
60: MBeanServerConnection delegateConnection = cntor
61: .getMBeanServerConnection(delegate);
62:
63: // The example policy file provided allows both MBeanServerConnections to call
64: // MBeanServerConnection.queryNames
65: Set mbeans = connection.queryNames(null, null);
66: System.out
67: .println("MBeans retrieved by a connection without delegate subject:");
68: System.out.println(mbeans);
69: System.out.println();
70:
71: mbeans = delegateConnection.queryNames(null, null);
72: System.out
73: .println("MBeans retrieved by a connection with a delegate subject:");
74: System.out.println(mbeans);
75: System.out.println();
76:
77: // The example policy file forbids to call MBeanServerConnection.getObjectInstance
78: try {
79: connection
80: .getObjectInstance(ObjectName
81: .getInstance("JMImplementation:type=MBeanServerDelegate"));
82: throw new Error();
83: } catch (SecurityException x) {
84: System.out
85: .println("No permission to call getObjectInstance for the MBeanServerDelegate");
86: }
87: }
88: }
|