01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.core.security;
17:
18: import org.apache.openejb.core.security.jaas.UsernamePasswordCallbackHandler;
19: import org.apache.openejb.util.ConfUtils;
20: import org.apache.openejb.util.URLs;
21:
22: import javax.security.auth.Subject;
23: import javax.security.auth.login.LoginContext;
24: import javax.security.auth.login.LoginException;
25: import java.net.URL;
26: import java.net.URLDecoder;
27: import java.util.UUID;
28:
29: /**
30: * @version $Rev: 634198 $ $Date: 2008-03-06 01:23:13 -0800 $
31: */
32: public class SecurityServiceImpl extends AbstractSecurityService {
33: public SecurityServiceImpl() {
34: installJaas();
35:
36: try {
37: // Perform a login attempt (which should fail)
38: // simply to excercise the initialize code of any
39: // LoginModules that are configured.
40: // They should have a chance to perform any special
41: // boot-time code that they may need.
42: login("", "");
43: } catch (Throwable e) {
44: }
45: }
46:
47: protected static void installJaas() {
48: String path = System
49: .getProperty("java.security.auth.login.config");
50:
51: if (path != null) {
52: return;
53: }
54:
55: URL loginConfig = ConfUtils.getConfResource("login.config");
56:
57: System.setProperty("java.security.auth.login.config",
58: URLDecoder.decode(loginConfig.toExternalForm()));
59: }
60:
61: public UUID login(String realmName, String username, String password)
62: throws LoginException {
63: if (realmName == null) {
64: realmName = getRealmName();
65: }
66: LoginContext context = new LoginContext(realmName,
67: new UsernamePasswordCallbackHandler(username, password));
68: context.login();
69:
70: Subject subject = context.getSubject();
71:
72: return registerSubject(subject);
73: }
74: }
|