KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jmx > JaasAuthenticator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jmx;
18
19 import javax.management.remote.JMXAuthenticator JavaDoc;
20 import javax.security.auth.Subject JavaDoc;
21 import javax.security.auth.login.LoginException JavaDoc;
22
23 import org.apache.servicemix.jbi.security.auth.AuthenticationService;
24 import org.apache.servicemix.jbi.security.auth.impl.JAASAuthenticationService;
25
26 /**
27  *
28  * @author gnodet
29  * @org.apache.xbean.XBean element="jmxJaasAuthenticator"
30  */

31 public class JaasAuthenticator implements JMXAuthenticator JavaDoc {
32
33     private String JavaDoc domain = "servicemix-domain";
34     private AuthenticationService authenticationService = new JAASAuthenticationService();
35
36     /**
37      * The authentication service can be used to customize the authentication
38      * mechanism used by this authenticator. It defaults to a
39      * JAASAuthenticationService which delegates calls to the JAAS layer.
40      *
41      * @return the authenticationService
42      */

43     public AuthenticationService getAuthenticationService() {
44         return authenticationService;
45     }
46
47     /**
48      * @param authenticationService the authenticationService to set
49      */

50     public void setAuthenticationService(AuthenticationService authenticationService) {
51         this.authenticationService = authenticationService;
52     }
53
54     /**
55      * @return the JAAS domain to use for authentication
56      */

57     public String JavaDoc getDomain() {
58         return domain;
59     }
60
61     /**
62      * @param domain the JAAS domain to use for authentication
63      */

64     public void setDomain(String JavaDoc domain) {
65         this.domain = domain;
66     }
67
68     /* (non-Javadoc)
69      * @see javax.management.remote.JMXAuthenticator#authenticate(java.lang.Object)
70      */

71     public Subject JavaDoc authenticate(Object JavaDoc credentials) throws SecurityException JavaDoc {
72         if (credentials instanceof String JavaDoc[] == false) {
73             throw new IllegalArgumentException JavaDoc("Expected String[2], got " + (credentials != null ? credentials.getClass().getName() : null));
74         }
75         String JavaDoc[] params = (String JavaDoc[]) credentials;
76         if (params.length != 2) {
77             throw new IllegalArgumentException JavaDoc("Expected String[2] but length was " + params.length);
78         }
79         Subject JavaDoc subject = new Subject JavaDoc();
80         try {
81             authenticationService.authenticate(subject, domain, params[0], params[1]);
82         } catch (LoginException JavaDoc e) {
83             throw new SecurityException JavaDoc("Authentication failed", e);
84         } catch (Exception JavaDoc e) {
85             throw new SecurityException JavaDoc("Error occured while authenticating", e);
86         }
87         return subject;
88     }
89
90 }
91
Popular Tags