The Java Authentication and Authorization Service (JAAS) is a standard extension to the security in the J2SE Development Kit 5.0. JAAS provides the ability to enforce access controls based on user identity. JAAS is provided in WebLogic Server as an alternative to the JNDI authentication mechanism.
WebLogic Server clients use the authentication portion of the standard JAAS only. The JAAS LoginContext provides support for the ordered execution of all configured authentication provider LoginModule instances and is responsible for the management of the completion status of each configured provider.

//JAASLogin.javaimport javax.security.auth.Subject;importjavax.security.auth.callback.*;importjavax.security.auth.login.*;import weblogic.security.auth.callback.URLCallback;publicclass JAASLogin
{privatestaticfinalclass ConfigInfo
{staticString USERID = “weblogic”;staticString PASSWORD = “weblogic”;staticString URL = “t3://10.10.71.79:7001?;staticString JAAS_STRING = “other”;
}publicstaticvoid main(String[] args)
{try{
System.out.println(“Server is at ” + ConfigInfo.URL);
System.out.println(“Userid: ” + ConfigInfo.USERID);
System.out.println(“Password: ” + ConfigInfo.PASSWORD);
LoginContext lc =newLoginContext(ConfigInfo.JAAS_STRING,new JAASLogin.CallbackHandler(ConfigInfo.USERID, ConfigInfo.PASSWORD, ConfigInfo.URL));
System.out.println(“LoginContext:: “);
lc.login();
System.out.println(“lc.login():: “);
Subject subject = lc.getSubject();
System.out.println(“Subject: ” + subject);
}catch(AccountExpiredException ae){
ae.printStackTrace();
}catch(CredentialExpiredException ce){
ce.printStackTrace();
}catch(FailedLoginException fe){
fe.printStackTrace();
}catch(LoginException le){
le.printStackTrace();
}
}privatestaticfinalclassCallbackHandlerimplements javax.security.auth.callback.CallbackHandler
{private String userid;private String password;private String url;public CallbackHandler(String userid, String password, String url)
{this.userid = userid;this.password = password;this.url = url;
}publicvoidhandle(Callback[] callbacks)throws UnsupportedCallbackException
{for(inti = 0; i < callbacks.length; i++)
{if(callbacks[i]instanceof TextOutputCallback)
{
TextOutputCallback toc = (TextOutputCallback)callbacks[i];
System.err.println(“JAAS callback: ” + toc.getMessage());
}elseif(callbacks[i]instanceof NameCallback)
{
NameCallback nc = (NameCallback)callbacks[i];
nc.setName(userid);
}elseif(callbacks[i]instanceof PasswordCallback)
{
PasswordCallback pc = (PasswordCallback)callbacks[i];
pc.setPassword(password.toCharArray());
}elseif(callbacks[i]instanceof weblogic.security.auth.callback.URLCallback)
{
URLCallback uc = (URLCallback)callbacks[i];
uc.setURL(url);
}else{
System.out.println(callbacks[i] + ” Unrecognized Callback”);thrownew UnsupportedCallbackException(callbacks[i], “Unrecognized Callback”);
}
}
}
}
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3129309/ ,如需转载,请注明出处,否则将追究法律责任。