Cas(06)——基于数据库的认证

本文详细介绍了Cas Server中基于数据库的三种认证方式:BindModeSearchDatabaseAuthenticationHandler、QueryDatabaseAuthenticationHandler和SearchModeSearchDatabaseAuthenticationHandler。每种方式的实现原理、配置示例以及涉及的PrincipalNameTransformer和PasswordEncoder进行了讲解,帮助理解如何实现基于JDBC的用户认证。
摘要由CSDN通过智能技术生成

基于数据库的认证

  

       Cas Server自身已经为我们实现了几种基于JDBC的AuthenticationHandler实现,但它们不包含在Cas Server的核心包里面,而是包含在cas-server-support-jdbc中,如果我们要使用Cas Server已经实现好的基于JDBC的AuthenticationHandler,我们必须先将cas-server-support-jdbc对应的jar包、相关数据库的驱动,以及所需要使用的数据源实现等jar包加入Cas Server的类路径中。如果是基于Maven的war覆盖机制来修改Cas Server的配置文件,则我们可以在自己的Maven项目的依赖中加入如下项(对应的驱动就没贴出来了)。

      <dependency>

         <groupId>org.jasig.cas</groupId>

         <artifactId>cas-server-support-jdbc</artifactId>

         <version>${cas.version}</version>

         <scope>runtime</scope>

      </dependency>

 

       Cas Server默认已经实现好的基于JDBC的AuthenticationHandler有三个,它们都继承自AbstractJdbcUsernamePasswordAuthenticationHandler,而且在认证过程中都需要一个DataSource。下面来对它们做一个简要的介绍。

 

1.1     BindModeSearchDatabaseAuthenticationHandler

       BindModeSearchDatabaseAuthenticationHandler将试图以传入的用户名和密码从配置的DataSource中建立一个连接,如果连接成功,则表示认证成功,否则就是认证失败。以下是BindModeSearchDatabaseAuthenticationHandler源码的一段主要代码,通过它我们可以明显的看清其逻辑:

    protected final boolean authenticateUsernamePasswordInternal(

        final UsernamePasswordCredentials credentials)

        throws AuthenticationException {

        final String username = credentials.getUsername();

        final String password = credentials.getPassword();

 

        try {

            final Connection c = this.getDataSource()

                .getConnection(username, password);

            DataSourceUtils.releaseConnection(c, this.getDataSource());

            returntrue;

        } catch (final SQLException e) {

            returnfalse;

        }

    }

 

       当然,这种实现也需要你的DataSource支持getConnection(user,password)才行,否则将返回false。dbcp的BasicDataSource的不支持的,而c3p0的ComboPooledDataSource支持。

       以下是一个使用BindModeSearchDatabaseAuthenticationHandler的配置示例:

   <bean id="authenticationManager"

      class="org.jasig.cas.authentication.AuthenticationManagerImpl">

      ...

      <property name="authenticationHandlers">

         <list>

            ...

            <beanclass="org.jasig.cas.adaptors.jdbc.BindModeSearchDatabaseAuthenticationHandler">

                <property name="dataSource" ref="dataSource"/>

            </bean

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值