KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > example > swing > JGuardSwingExample


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.example.swing;
29
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.security.Principal JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.security.auth.Subject JavaDoc;
37 import javax.security.auth.login.LoginContext JavaDoc;
38 import javax.security.auth.login.LoginException JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JFrame JavaDoc;
41 import javax.swing.JOptionPane JavaDoc;
42
43 import net.sf.jguard.core.principals.UserPrincipal;
44 import net.sf.jguard.example.swing.dialog.CheckPermissionDialog;
45
46 import org.apache.log4j.Logger;
47
48 /**
49  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
50  * @author <a HREF="mailto:vberetti@users.sourceforge.net">Vincent Beretti</a>
51  */

52 public class JGuardSwingExample extends JFrame JavaDoc {
53
54     private static final Logger logger = Logger
55             .getLogger(JGuardSwingExample.class);
56     private static final long serialVersionUID = 417348431355013808L;
57
58     private Subject JavaDoc loggedSubject;
59
60
61     public JGuardSwingExample(){
62         super("jGuard Swing Example");
63
64         JButton JavaDoc loginButton = new JButton JavaDoc("Log in");
65         loginButton.addActionListener(new ActionListener JavaDoc(){
66             public void actionPerformed(ActionEvent JavaDoc event){
67                 handleLogin();
68             }
69         });
70         this.getContentPane().add(loginButton);
71
72         this.pack();
73         this.setLocation(400,200);
74         this.setVisible(true);
75     }
76
77     private void handleLogin(){
78
79         LoginContext JavaDoc lc = null;
80         try {
81             lc = new LoginContext JavaDoc("jGuardSwingExample",new SwingCallbackHandler(this));
82             lc.login();
83         } catch (LoginException JavaDoc e) {
84             logger.error("LoginException : "+e.getMessage());
85             return;
86         }
87         lc.getSubject().getPrincipals().add(new UserPrincipal(lc.getSubject()));
88
89
90         loggedSubject=lc.getSubject();
91         if(logger.isInfoEnabled()){
92             Set JavaDoc ppals=lc.getSubject().getPrincipals();
93             logger.info("user logged with "+ppals.size()+" principals");
94             Iterator JavaDoc itPpals=ppals.iterator();
95             while(itPpals.hasNext()){
96                 Principal JavaDoc ppal=(Principal JavaDoc)itPpals.next();
97                 logger.info(ppal.getName()+" "+ppal.getClass());
98             }
99         }
100
101         Set JavaDoc principals = lc.getSubject().getPrincipals();
102         Iterator JavaDoc itPrincipals = principals.iterator();
103         StringBuffer JavaDoc strPpal = new StringBuffer JavaDoc();
104         while (itPrincipals.hasNext()){
105             strPpal.append(((Principal JavaDoc)itPrincipals.next()).getName()+", ");
106         }
107
108         JOptionPane.showMessageDialog(this, "You are now logged as "+ strPpal.toString(), "Logged as", JOptionPane.INFORMATION_MESSAGE);
109
110
111         new CheckPermissionDialog(this,loggedSubject);
112     }
113
114     public static void main(String JavaDoc args[]){
115         new JGuardSwingExample();
116     }
117
118 }
119
Popular Tags