⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j-sec2-3-12.html

📁 java安全-论证授权
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<TD bgcolor="#ffffff" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="2"></TD>
</TR>
</TABLE>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><font face="Verdana, sans-serif" size="-1"><a href="http://www.ibm.com/">IBM</a> : <a href="/developerWorks/cn/index.shtml">developerWorks 中国网站</a> : <a href="/developerWorks/cn/java/index.shtml">java</a> : <a href="/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle">教学 - 在线教程</a></font>
<br>
<img alt="Java 安全性,第 2 部分:认证与授权" src="imagemaster/masthead.jpg"></td><td width="*" valign="bottom" align="right"><a border="0" href="j-sec2.zip"><img alt="下载 ZIP 文件" border="0" src="../i/icon-zip.gif"></a><a target="_blank" href="../tutorial_eng/index.html" border="0"><img alt="英文原文" border="0" src="../i/icon-source.gif"></a>
					&nbsp;&nbsp;&nbsp;
				</td>
</tr>
<tr>
<td height="2" colspan="2"><img alt="" height="2" width="1" src="../i/c.gif"></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index3.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-sec2-6-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img alt="" src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-sec2-3-11.html"><img alt="上页" border="0" src="../i/previous.gif" name="topprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-sec2-3-13.html"><img alt="下页" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="3.JAAS 中的认证" src="imagemaster/titlebar3.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15">&nbsp;</td><td bgcolor="ffffff" width="12">&nbsp;</td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>AlwaysLoginModule</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr>   第 12 页(共13 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p> 
<code>AlwaysLoginModule</code> 认证将始终成功,
所以实际上它仅用于通过 <code>NameCallback</code> 函数获取用户名。
假设其它登录模块都成功,<code>AlwaysLoginModule</code> 的 <code>commit()</code>
方法将创建一个带用户名的新 <code>PrincipalImpl</code> 对象并将它添加到 <code>Subject</code> 的 <code>Principal</code> 集中。注销将除去 <code>Subject</code> 的 <code>Principal</code> 集
中的 <code>PrincipalImpl</code>。</p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
import java.security.*;
import javax.security.auth.*;
import javax.security.auth.spi.*;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
import java.io.*;
import java.util.*;

// This is a JAAS Login Module that always succeeds.  While not realistic, 
// it is designed to illustrate the bare bones structure of a Login Module 
// and is used in examples that show the login configuration file 
// operation.

public class AlwaysLoginModule implements LoginModule {

     private <code>Subject</code> subject;
     private <code>Principal</code> principal;
     private CallbackHandler callbackHandler;
     private String username;
     private boolean loginSuccess;
     //
     // Initialize sets up the login module.  sharedState and options are
     // advanced features not used here
     public void initialize(<code>Subject</code> sub, CallbackHandler cbh,
       Map sharedState, Map options) {

       subject = sub;
       callbackHandler = cbh;
       loginSuccess = false;
     }
     //
     // The login phase gets the userid from the user
     public boolean login() throws LoginException {
       //
       // Since we need input from a user, we need a callback handler
       if (callbackHandler == null) {
         throw new LoginException( "No CallbackHandler defined");

       }
       Callback[] callbacks = new Callback[1];
       callbacks[0] = new NameCallback("Username");
       //
       // Call the callback handler to get the username
       try {
         System.out.println( "\nAlwaysLoginModule Login" );
         callbackHandler.handle(callbacks);
         username = ((NameCallback)callbacks[0]).getName();
       } catch (IOException ioe) {
         throw new LoginException(ioe.toString());
       } catch (UnsupportedCallbackException uce) {
         throw new LoginException(uce.toString());
       }
       loginSuccess = true;
       System.out.println();
       System.out.println( "Login: AlwaysLoginModule SUCCESS" );
       return true;
     }
     //
     // The commit phase adds the principal if both the overall authentication
     // succeeds (which is why commit was called) as well as this particular
     // login module
     public boolean commit() throws LoginException {
       //
       // Check to see if this login module succeeded (which it always will

       // in this example)
       if (loginSuccess == false) {
         System.out.println( "Commit: AlwaysLoginModule FAIL" );
         return false;
       }
       //
       // If this login module succeeded too, then add the new principal
       // to the subject (if it does not already exist)
       principal = new PrincipalImpl(username);
       if (!(subject.getPrincipals().contains(principal))) {
         subject.getPrincipals().add(principal);
       }
       System.out.println( "Commit: AlwaysLoginModule SUCCESS" );
       return true;
     }
     //
     // The abort phase is called if the overall authentication fails, so
     // we have to clean up the internal state
     public boolean abort() throws LoginException {

       if (loginSuccess == false) {
         System.out.println( "Abort: AlwaysLoginModule FAIL" );
         principal = null;
         return false;
       }
       System.out.println( "Abort: AlwaysLoginModule SUCCESS" );
       logout();

       return true;
     }
     //
     // The logout phase cleans up the state
     public boolean logout() throws LoginException {

       subject.getPrincipals().remove(principal);
       loginSuccess = false;
       principal = null;
       System.out.println( "Logout: AlwaysLoginModule SUCCESS" );
       return true;
      }
}
</code>
</pre>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index3.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-sec2-6-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img alt="" src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-sec2-3-11.html"><img alt="上页" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-sec2-3-13.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -