Path: blob/master/test/jdk/javax/management/security/TestJMXAuthenticator.java
41149 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.security.Principal;2425import javax.management.Attribute;26import javax.management.MBeanServer;27import javax.management.ObjectName;28import javax.management.remote.JMXAuthenticator;29import javax.management.remote.JMXPrincipal;30import javax.security.auth.Subject;3132public final class TestJMXAuthenticator implements JMXAuthenticator {3334private String protocol = "";35private MBeanServer mbs = null;3637public TestJMXAuthenticator() {38}3940public TestJMXAuthenticator(String protocol) {41this.protocol = protocol;42}4344public TestJMXAuthenticator(String protocol, MBeanServer mbs) {45this.protocol = protocol;46this.mbs = mbs;47}4849public Subject authenticate(Object credentials) {5051String credentials_username = "";52String credentials_password = "";53Principal aPrincipal = null;5455credentials_username = ((String[]) credentials)[0];56credentials_password = ((String[]) credentials)[1];5758String authenticated_username = System.getProperty("susername");59String authenticated_password = System.getProperty("spassword");60String principal = System.getProperty("principal");6162System.out.println("TestJMXAuthenticator::authenticate: Start");63System.out.println("TestJMXAuthenticator::authenticate: credentials username = " +64credentials_username);65System.out.println("TestJMXAuthenticator::authenticate: credentials password = " +66credentials_password);67System.out.println("TestJMXAuthenticator::authenticate: authenticated username = " +68authenticated_username);69System.out.println("TestJMXAuthenticator::authenticate: authenticated password = " +70authenticated_password);71System.out.println("TestJMXAuthenticator::authenticate: principal used for " +72"authorization = " + principal);7374if (credentials_username.equals(authenticated_username) &&75credentials_password.equals(authenticated_password)) {76System.out.println("TestJMXAuthenticator::authenticate: " +77"Authenticator should succeed");78} else {79System.out.println("TestJMXAuthenticator::authenticate: " +80"Authenticator should reject");81throw new SecurityException("TestJMXAuthenticator throws EXCEPTION");82}8384// At this point, authentication has succeeded85// (no SecurityException thrown).86//87// If no authorization is required, the returned subject (empty or not)88// is useless.89// Otherwise, the returned subject must define a principal90// and authorization will be performed against this principal.91//92// Note that this custom JMXAuthenticator is used for test purpose and93// the username used to perform authentication may be different from the94// username used to perform authorization.95//96Subject subject = new Subject();9798if (principal != null) {99System.out.println("TestJMXAuthenticator::authenticate: " +100"Add " + principal + " principal to the returned subject");101subject.getPrincipals().add(new JMXPrincipal(principal));102}103104return subject;105}106}107108109