Java Authentication Authorization Service(JAAS,Java驗證和授權API)提供了靈活和可伸縮的機制來保證客戶端或伺服器端的Java程式。Java早期的安全框架強調的是通過驗證代碼的來源和作者,保護用戶避免受到下載下來的代碼的攻擊。JAAS強調的是通過驗證誰在運行代碼以及他/她的許可權來保護系統免受用戶的攻擊。它讓你能夠將一些標準的安全機制,例如Solaris NIS(網路信息服務)、Windows NT、LDAP(輕量目錄存取協定),Kerberos等通過一種通用的,可配置的方式集成到系統當中去。
基本介紹
- 中文名:Java驗證和授權API
- 外文名:Java Authentication Authorization Service
- 簡寫:JAAS
- 目的:保證客戶端或伺服器端的Java程式
基本介紹
用戶認證框架
可插入式認證
package samples;import javax.security.auth.Subject;import javax.security.auth.login.LoginContext;import javax.security.auth.login.LoginException;import com.sun.security.auth.callback.TextCallbackHandler;public class CountFiles { static LoginContext lc = null; public static void main(String[] args) { //使用配置檔案中名字為“CountFiles”的條目 try { lc = new LoginContext("CountFiles", new TextCallbackHandler()); } catch (LoginException le) { le.printStackTrace(); System.exit(-1); } try { lc.login(); //如果沒有異常拋出,則表示認證成功 } catch (Exception e) { System.out.println("Login failed: " + e); System.exit(-1); } //以認證用戶的身份執行代碼 Object o = Subject.doAs(lc.getSubject(), new CountFilesAction()); System.out.println("User " + lc.getSubject( ) + " found " + o + " files."); System.exit(0); }}
package samples;import java.io.File;import java.security.PrivilegedAction;class CountFilesAction implements PrivilegedAction { public Object run() { File f = new File("."); File[] files = f.listFiles(); return new Integer(files.length); }}
<name used by application to refer to this entry> { <LoginModule> <flag> <LoginModule options>; <optional additional LoginModules, flags and options>;};
CountFiles { com.apusic.security.auth.login.ClientPasswordLoginModule required;};
grant <signer(s) field>, <codeBase URL><Principal field(s)> { permission perm_class_name "target_name", "action"; .... permission perm_class_name "target_name", "action";};
grant codeBase "file:./build" { permission java.security.AllPermission;};grant codeBase "file:/${apusic.home}/lib/apusic.jar" { permission java.security.AllPermission;};grant codeBase "file:./build/actions" Principal com.apusic.security.PrincipalImpl "admin" { permission java.io.FilePermission "<<ALL FILES>>", "read";};
java -classpath %APUSIC_HOME%/lib/apusic.jar;./build;./build/actions-Djava.security.manager-Djava.security.policy==policy.jaas-Djava.security.auth.login.config==login.conf-Dapusic.home=%APUSIC_HOME% samples.CountFiles