JAAS 基本概念 (1/3)

来源:互联网 发布:淘宝树林名妆 假货 编辑:程序博客网 时间:2024/06/05 18:53
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

JAAS ( Java Authentication and Authorization Services )簡單來說就是專門處理 身份驗證 ( authentication ) 及 權限管控 ( authorization ) 的標準服務目前已經納入 j2sdk 1.4 正式成為標準的安全性模組

主要的幾個元件有分為 通用性 ( common ), 身份驗證 ( authentication ) 及 權限管控 ( authorziation ).

  • 通用性
    • Subject
    • Principal
    • Credential
  • 身份驗證
    • LoginContext
    • LoginModule
    • CallbackHandler
    • Callback
  • 權限管控
    • Policy
    • AuthPermission
    • PrivateCredentialPermission

而 Subject 是整個 JAAS Framework 的核心

他的宣告方式是

---------------------------------

    public Subject();    public Subject(boolean readOnly, Set principals,                   Set pubCredentials, Set privCredentials);---------------------------------

可以看到 Subject 之中包含了數個 principals, pubCredentials, 及 privCredentials.

在身份驗證通過的同時, LoginContext 就會存在著 Subject

 LoginContext lc = new LoginContext("SL", new SLCallbackHandler());  lc.login();  PrivilegedExceptionAction action = new PrivilegedExceptionAction() {   public Object run() throws Exception{       // do something    }  };  Subject.doAs(lc.getSubject(), action);

 

我們可以使用 lc.getSubject() 取得目前的 subject

及透過 Subject.doAs 或 Subject.doAsPrivileged 處理是否執行該 action.

這兩個 method 差異是是否為同一個 thread 需要參考同一份 AccessControlContext .

而 Principal 是需要實作 java.security.Principal 及 java.io.Seriallizable.

簡單的範例如下

public class SamplePrincipal implements java.security.Principal,java.io.Serializable {

    private String name;    public SamplePrincipal(String name) {     if (name == null)         throw new NullPointerException("illegal null input");     this.name = name;    }    public String getName() {       return name;    }    public String toString() {       return("SamplePrincipal:  " + name);    }    public boolean equals(Object o) {        if (o == null)          return false;        if (this == o)            return true;         if (!(o instanceof SamplePrincipal))            return false;                SamplePrincipal that = (SamplePrincipal)o;        if (this.getName().equals(that.getName()))            return true;       

        return false;    }     public int hashCode() {        return name.hashCode();    }}

> next one : JAAS 身份驗證 (2/3)

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击