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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值