CAS集成Weblogic的ServletAuthentication调用

CAS集成Weblogic的ServletAuthentication调用

本来,使用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之类。

后来发现,虽然能loginContext拿到Subject,但该Subject的Principal不能被页面的request.getPrincipal()所取得,醒悟自己在做JAAS Login,查看weblogic文档,原来Weblogic提供了
weblogic.servlet.security.ServletAuthentication
用于在Servlet端调用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了。

你可能感兴趣的:(CAS集成Weblogic的ServletAuthentication调用)