JAAS Custom Login Module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.