zoukankan      html  css  js  c++  java
  • CAS 服务端数据库认证

    CAS-服务端数据库认证

    数据认证需要相关的jar包:
    cas-server-support-jdbc-x.x.x.jar
    MySQL-connector-Java-x.x.x-bin.jar

    修改CAS Server的配置

      通过数据库查询来验证用户名密码
      修改cas server的配置
      在tomcat_cas/webapps/cas/WEB_INF/deployerConfigContext.xml找到如下信息
    
         deployerConfigContext.xml将如下信息
        <bean class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />  
         替换为
        <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">  
        <property name="dataSource" ref="dataSource" ></property>  
        <property name="sql" value="select password from t_user where login_name=?" ></property>  
        <!--<property name="passwordEncoder" ref="MD5PasswordEncoder" ></property>-->  
        </bean>  
    
         <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName"><value>com.MySQL.jdbc.Driver</value></property>
           <property name="url"><value>jdbc:mysql://localhost:3306/smtp</value></property>
           <property name="username"><value>root</value></property>
           <property name="password"><value>.,mlkjoiu987</value></property>
        </bean>
        
        <bean id="MD5SHA1PasswordEncoder" class="oddtech.core.password.MD5SHAPassWordEncoder">
        </bean>
        <bean id="primaryAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
           <property name="dataSource" ref="dataSource"></property>
           <property name="sql" value="select password from operator where account=?"></property>
         <!--<property name="passwordEncoder" ref="MD5PasswordEncoder" ></property>-->
        </bean>
        
        如果你用了普通的加密,那么请加入一下标签
        <bean id="primaryAuthenticationHandler" class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
          <property name="dataSource" ref="dataSource"></property>
          <property name="sql" value="select password from operator where account=?"></property>
          <property name="passwordEncoder" ref="passwordEncoder"></property>
        </bean>
        <bean id="passwordEncoder"  class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">      
            <constructor-arg value="MD5"/>  
        </bean>
        ##使用SHA1加密
        <bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">
          <constructor-arg value="SHA1" />
        </bean>
        至于参数值(Encoder采用的加密方式)
        MessageDigest messageDigest=MessageDigest.getInstance(this.encodingAlgorithm)
        如果密码加密特别复杂,例如 SHA256(MD5(password))
        我们可以写一个实现类,实现org.jasig.cas.authentication.handler.PasswordEncoder接口
    

    注意

        1) 密码加密过程,如果不用,注释掉即可
        2) QueryDatabaseAuthenticationHandler是cas-server-support-jdbc提供的查询接口,其中一个是通过配置一个SQL语句来查出密码,与所给密码匹配
        3) sql语句就是查询哪一张表,本例根据t_user表的login-name字段查询密码,CAS会匹配用户用户输入的密码,如果匹配则通过。
        4) passwordEncoder这个是处理密码的加密,如果想要你的应用中数据库保存的是加密过的,比如本例是使用MD5加密的。所以配置了MD5PasswordEncoder这个Handler,cas内置了MD5的功能所以只需要配置一下就可以了。如果在实际应用中使用的是公司自己的加密算法,那么就需要自己写一个handler来处理密码。实现方式也比较简单,创建一个类继承org.jasig.cas.authentication.handler.PasswordEncoder然后在encode方法中加密用户输入的密码然后返回即可。
    
    
  • 相关阅读:
    WSP部署错误—SharePoint管理框架中的对象“SPSolutionLanguagePack Name=0”依赖其他不存在的对象
    Elevate Permissions To Modify User Profile
    Error with Stsadm CommandObject reference not set to an instance of an object
    ASP.NET MVC3添加Controller时没有Scaffolding options
    测试使用Windows Live Writer写日志
    配置TFS 2010出现错误—SQL Server 登录的安全标识符(SID)与某个指定的域或工作组帐户冲突
    使用ADO.NET DbContext Generator出现错误—Unable to locate file
    CSS
    HTML DIV标签
    数据库
  • 原文地址:https://www.cnblogs.com/ssgao/p/8817029.html
Copyright © 2011-2022 走看看