| 1 28 package net.sf.jguard.example.swing; 29 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.security.Principal ; 33 import java.util.Iterator ; 34 import java.util.Set ; 35 36 import javax.security.auth.Subject ; 37 import javax.security.auth.login.LoginContext ; 38 import javax.security.auth.login.LoginException ; 39 import javax.swing.JButton ; 40 import javax.swing.JFrame ; 41 import javax.swing.JOptionPane ; 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 52 public class JGuardSwingExample extends JFrame { 53 54 private static final Logger logger = Logger 55 .getLogger(JGuardSwingExample.class); 56 private static final long serialVersionUID = 417348431355013808L; 57 58 private Subject loggedSubject; 59 60 61 public JGuardSwingExample(){ 62 super("jGuard Swing Example"); 63 64 JButton loginButton = new JButton ("Log in"); 65 loginButton.addActionListener(new ActionListener (){ 66 public void actionPerformed(ActionEvent 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 lc = null; 80 try { 81 lc = new LoginContext ("jGuardSwingExample",new SwingCallbackHandler(this)); 82 lc.login(); 83 } catch (LoginException 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 ppals=lc.getSubject().getPrincipals(); 93 logger.info("user logged with "+ppals.size()+" principals"); 94 Iterator itPpals=ppals.iterator(); 95 while(itPpals.hasNext()){ 96 Principal ppal=(Principal )itPpals.next(); 97 logger.info(ppal.getName()+" "+ppal.getClass()); 98 } 99 } 100 101 Set principals = lc.getSubject().getPrincipals(); 102 Iterator itPrincipals = principals.iterator(); 103 StringBuffer strPpal = new StringBuffer (); 104 while (itPrincipals.hasNext()){ 105 strPpal.append(((Principal )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 args[]){ 115 new JGuardSwingExample(); 116 } 117 118 } 119 | Popular Tags |