打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CAS如何跟普通的Web系统融合认证和授权
userphoto

2006.02.20

关注

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用 CAS 在 Tomcat 中实现单点登录
单点登录学习(1)CAS服务器端配置编程
Acegi简介
春季-身份验证失败重定向,请求参数不起作用
JAAS简介及实例
初识Shiro
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服