JAAS login authentication involves the interface \ class:
LoginContext: The application logs on to authentication by invoking the login method of the class.
Loginmodule: An abstract login interface. Users need to implement the interface to define their own login module.
Subject: An entity that requires authentication, such as a person, a program.
PRINCIPAL: Certified entity flags, such as numbers, roles, etc.
Custom Login Authentication class:
Package Root.tgview;import Java.io.ioexception;import Java.util.map;import javax.security.auth.subject;import Javax.security.auth.callback.callback;import Javax.security.auth.callback.callbackhandler;import Javax.security.auth.callback.namecallback;import Javax.security.auth.callback.passwordcallback;import Javax.security.auth.callback.unsupportedcallbackexception;import javax.security.auth.login.LoginException; Import Javax.security.auth.spi.loginmodule;import Sun.security.acl.principalimpl;public class RdbmsJaasLoginModuel Implements Loginmodule{private Subject subject;private callbackhandler callbackhandler;private map<string,?> Sharedstate;private map<string,?> options;private string Url;private string Driverclass;private boolean debug;@ Overridepublic Boolean abort () throws loginexception {This.subject=null;return false;} @Overridepublic Boolean commit () throws Loginexception {Subject.getprincipals (). Add (New Principalimpl ("Alice")); return true;} @Overridepublic void InitIalize (Subject Subject, CallbackHandler callbackhandler,map<string,?> sharedstate, map<string,?> options {this.subject = subject; this.callbackhandler = CallbackHandler; this.sharedstate = sharedstate; this.options = options; url = (String) options.get ("url"); Driverclass = (String) options.get ("Driver"); debug = "true". Equalsignorecase (String) options.get ("Debug"); } @Overridepublic Boolean login () throws Loginexception {if (CallbackHandler = = null) throw new Loginexception ("No handler" ); NameCallback NAMECB = new NameCallback ("User:"); PasswordCallback PASSCB = new PasswordCallback ("Password:", true); Callback[] callbacks = new callback[] {NAMECB, PASSCB}; try {callbackhandler.handle (callbacks);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Unsupportedcallbackexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} String username = namecb.getname (); String password = new string (Passcb.getpassword ()); BooleaN success = rdbmsvalidate (username, password); return success; }private boolean rdbmsvalidate (string username, string password) {return username.equals (password);} @Overridepublic Boolean logout () throws Loginexception {//TODO auto-generated method Stubreturn false;}}
Configuration file:
myexample {root.tgview.RdbmsJaasLoginModuel Required debug=true;};
Test method:
LoginContext Lc=null; try {lc= new LoginContext ("Myexample", New Textcallbackhandler ()); Lc.login (); Subject sub = Lc.getsubject (); Sub.doas (Sub, New Testprivilegedaction ());} catch (Loginexception e) {e.printstacktrace ();}
JAAS Custom Login Module