本來,使用j_security_check是最簡單的Build-in認證方式,但CAS有自己的登錄入口,即login servlet,如果用該servlet,必須自己動手完成JAAS的登錄。于是,開始擴展CAS的edu.yale.its.tp.cas.auth.provider,在該包中的provider都擴展自authHandler接口,而CAS是在web.xml中定義了最終使用哪一個authHandler。
edu.yale.its.tp.cas.authHandler
edu.yale.its.tp.cas.auth.provider.WeblogicHandler
我自己寫了一個WeblogicHandler(edu.yale.its.tp.cas.auth.provider包中),專門讓CAS登錄到Weblogic Server,事實上,將來如果不用WLS,還可能使用Websphere,Jboss,AD之類。
后來發(fā)現(xiàn),雖然能loginContext拿到Subject,但該Subject的Principal不能被頁面的request.getPrincipal()所取得,醒悟自己在做JAAS Login,查看weblogic文檔,原來Weblogic提供了
weblogic.servlet.security.ServletAuthentication
用于在Servlet端調(diào)用JAAS接口進行登錄,通過該接口登錄后,就如同User使用了標準的登錄機制登入了Weblogic。
于是,立即修改了login servlet測試一下,加入
try {
CallbackHandler handler = new SimpleCallbackHandler(
request.getParameter("username"),
request.getParameter("password"));
Subject mySubject = weblogic.security.services.Authentication
.login(handler);
weblogic.servlet.security.ServletAuthentication.runAs(
mySubject, request);
System.out.println("mySubject[" +mySubject.toString()+"]"+
"寫入Session");
} catch (LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后,頁面果然就能拿到Pincipal了。