KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > ejb > CustomPrincipalLoginModule


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.ejb;
23
24 import java.security.acl.Group JavaDoc;
25 import java.security.Principal JavaDoc;
26 import javax.security.auth.login.LoginException JavaDoc;
27 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
28 import org.jboss.security.SimpleGroup;
29 import org.jboss.security.SimplePrincipal;
30
31 /** Test of installing a custom principal via a login module.
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 37406 $
35  */

36 public class CustomPrincipalLoginModule extends UsernamePasswordLoginModule
37 {
38    private CustomPrincipalImpl caller;
39
40    public boolean login() throws LoginException JavaDoc
41    {
42       if (super.login())
43       {
44          caller = new CustomPrincipalImpl(getUsername());
45          return true;
46       }
47       return false;
48    }
49
50    protected Principal JavaDoc getIdentity()
51    {
52       Principal JavaDoc identity = caller;
53       if( identity == null )
54          identity = super.getIdentity();
55       return identity;
56    }
57
58    protected Group JavaDoc[] getRoleSets() throws LoginException JavaDoc
59    {
60       try
61       {
62          // The declarative permissions
63
Group JavaDoc roles = new SimpleGroup("Roles");
64          // The caller identity
65
Group JavaDoc callerPrincipal = new SimpleGroup("CallerPrincipal");
66          Group JavaDoc[] groups = {roles, callerPrincipal};
67          log.info("Getting roles for user=" + getUsername());
68          // Add the Echo role
69
roles.addMember(new SimplePrincipal("Echo"));
70          // Add the custom principal for the caller
71
callerPrincipal.addMember(caller);
72          return groups;
73       }
74       catch (Exception JavaDoc e)
75       {
76          log.error("Failed to obtain groups for user=" + getUsername(), e);
77          throw new LoginException JavaDoc(e.toString());
78       }
79    }
80
81    protected String JavaDoc getUsersPassword()
82    {
83       return "theduke";
84    }
85
86 }
Popular Tags