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中定义资源的授权规则。
|