CAS学习之认证扩展使用(1)

关于认证方面,CAS是非常灵活的。在CAS服务端的配置文件deployerConfigContext.xml中可以定义任何自己想要的认证方式,例如JDBC方式认证、LDAP方式认证。并且,可以同时启用多种认证方式,形成一个认证流。当第一个认证未通过时,第二个认证启用。如果第一个认证通过,第二认证就不会启用。以此类推。


下面一个简单的例子结束添加新认证方式的步骤。

1、集成抽象类AbstractUsernamePasswordAuthenticationHandler,实现自己的逻辑,密码需要以用户名开头:

package com.feiquan16.cas.authentication.handler;
import org.apache.log4j.Logger;
import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
public class PasswordStartWithUsernameAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler{
    protected static final Logger LOG = Logger.getLogger(PasswordStartWithUsernameAuthenticationHandler.class);
    @Override
    protected boolean authenticateUsernamePasswordInternal(UsernamePasswordCredentials credential) throws AuthenticationException {
                                 
        String username = credential.getUsername();
        String password = credential.getPassword();
                                 
        if (password.startsWith(username)) {
            return true;
        }
                                 
        return false;
    }
}

2、将该类导出jar包,并放到cas/WEB-INF/lib下

3、在deployerConfigContext.xml中增加自己定义的认证方式:

<property name="authenticationHandlers">
            <list>
                <!--
                    | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating
                    | a server side SSL certificate.
                    +-->
                <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
                    p:httpClient-ref="httpClient" />
                <!--
                    | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS
                    | into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials
                    | where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your
                    | local authentication strategy.  You might accomplish this by coding a new such handler and declaring
                    | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.
                    +-->
                <bean
                    class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
                      
                <bean
                    class="com.feiquan16.cas.authentication.handler.PasswordStartWithUsernameAuthenticationHandler" />
            </list>
        </property>

4、重启tomcat

你可能感兴趣的:(CAS认证扩展)