CAS如何跟普通的Web系統融合認證和授權
CAS的作用是負責單點登錄,登錄細節當然要自己寫,CAS3有一個這樣的AuthenticationHandler 接口,繼承關系如下
1,AbstractAuthenticationHandler implements AuthenticationHandler
2,AbstractUsernamePasswordAuthenticationHandler extends AbstractAuthenticationHandler
AbstractUsernamePasswordAuthenticationHandler 正是你認證管理的著手點,你寫一個類,如WeblogicAuthenticanHandler去擴展它。
你先看看下面的接口:
public interface AuthenticationHandler {
/**
* Method to determine if the credentials supplied can be authenticated.
*
* @param credentials The credentials to authenticate
* @return true if authenticated and false they are not
* @throws AuthenticationException An AuthenticationException can contain details about why a particular authentication request failed.
* AuthenticationExceptions contain code/desc.
*/
boolean authenticate(Credentials credentials) throws AuthenticationException;
}
authenticate這個接口是每個Hander都必須實現,當然,AbstractHandler將它轉交給 authenticateInternal 方法去實現。
認證有兩種情況,成功或者失敗,true or false。
我使用Weblogic的LoginModule
loginContext = new LoginContext("WeblogicUsernamePasswordModule", new WeblogicCallbackHandler(username, password, url));
它拋出個各種不同的認證異常讓我輕松判斷認證過程中發生了什么事情,
/**
* Attempt authentication
*/
try
{
// If we return without an exception, authentication succeeded
loginContext.login();
}
catch(FailedLoginException fle)
{
System.out.println("Authentication Failed, " + fle.getMessage());
loginsccess=false;
}
catch(AccountExpiredException aee)
{
System.out.println("Authentication Failed: Account Expired");
loginsccess=false;
}
catch(CredentialExpiredException cee)
{
System.out.println("Authentication Failed: Credentials Expired");
loginsccess=false;
}
catch(Exception e)
{
System.out.println("Authentication Failed: Unexpected Exception, " + e.getMessage());
loginsccess=false;
}
如果一切正常,授權開始了。
if(loginsccess==true)
{
/**
* Retrieve authenticated subject, perform SampleAction as Subject
*/
subject = loginContext.getSubject();
System.out.println("User["+ username+"]["+ password+"] Login Success, Subject is"+subject.toString());
return true;
}
else
{
System.out.println("User["+ username+"]["+ password+"] Login Fail, Check!!!!!");
return false;
}
OK,獲得了Subject,那你就可以獲得principal,編程式授權便有了依據。
同時,你還可以用Weblogic的聲明式授權,直接在web.xml中定義資源的授權規則。
更多關于認證授權,請看[Weblogic Security In Action]
http://dev2dev.bea.com.cn/bbs/servlet/D2DServlet/download/81-26770-158358-1697/WeblogicSecurityInAction(1).swf
posted on 2006-02-09 13:50 david.turing 閱讀(4177) 評論(4) 編輯 收藏 所屬分類: Security領域