CAS 单点登录实战 (2) 服务器端

登录认证

源码下载什么的就不写了这个是基本生活自理能力啊,相信大家有的。这里介绍的是用http的,https相关的配置随便度娘一大把这里就不做介绍了直入正题,代码用的是Cas Server4.0, Client3.2.1

网上介绍的都是用默认的DAO,写一条sql进去啥的,这里不介绍这种,我想大部分场景是自己写的验证逻辑,自己根据service查询用户信息啥的,涉及到SERVER修改的地方有如下:

1. 去掉https

(1)修改deployerConfigContext.xml文件, 在proxyAuthenticationHandler 的定义中改属性p:requireSecure=”false”

 <bean id="proxyAuthenticationHandler"        class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
      p:httpClient-ref="httpClient"
      p:requireSecure="false"/>

(2)修改ticketGrantingTicketCookieGenerator.xml 中ticketGrantingTicketCookieGenerator的定义改属性p:cookieSecure=”false”

 <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
    p:cookieSecure="false"
    p:cookieMaxAge="-1"
    p:cookieName="CASTGC"
    p:cookiePath="/cas" />

(3)修改warnCookieGenerator.xml中warnCookieGenerator的定义改属性p:cookieSecure=”false”

<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
    p:cookieSecure="false"
    p:cookieMaxAge="-1"
    p:cookieName="CASPRIVACY"
    p:cookiePath="/cas" />

2. 定义验证器

这里的验证逻辑很简单只要用户名是admin,密码是123就通过,实际应用中,我们写一个自己的service去查询用户信息做比较复杂的校验。

public class MySSOUsersAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {

@Override
protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential) throws GeneralSecurityException, PreventedException {
    String username = credential.getUsername();
    String password = credential.getPassword();
    checkAccount(username, password);
    return createHandlerResult(credential, new SimplePrincipal(username), null);
}

private void checkAccount(String username, String password) throws GeneralSecurityException{
    if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
        throw new CredentialCheckFailException();
    }
    if (!username.equals("admin") || !password.equals("123")) {
        throw new CredentialCheckFailException();
    }
}

}

定义好之后在deployerConfigContext.xml里面找到primaryAuthenticationHandler,改成我们自己的实现

 <bean id="primaryAuthenticationHandler" class="com.ben.sso.MySSOUsersAuthenticationHandler">

3. 自定义异常信息
cas内置了一些校验失败的信息,但是很笼统,我们通常需要显示一些我想要的提示,比如“去你大爷,你输错密码了”诸如此类的(开玩笑,这种提示第二天就被炒鱿鱼了,呵呵…)

(1)定义一个自己的异常类CredentialCheckFailException 只要是GeneralSecurityException, PreventedException之一的子类就OK,
(2)在message.properties文件总加入一行:
authenticationFailure.CredentialCheckFailException=Credentials check fail oye~.

(3)在login-webflow.xml文件中找到id是handleAuthenticationFailure 这个的定义,增加CredentialCheckFailException的定义:

 <action-state id="handleAuthenticationFailure">
    <evaluate expression="authenticationExceptionHandler.handle(currentEvent.attributes.error, messageContext)"/>
    <transition on="AccountDisabledException" to="casAccountDisabledView"/>
    <transition on="AccountLockedException" to="casAccountLockedView"/>
    <transition on="CredentialExpiredException" to="casExpiredPassView"/>
    <transition on="InvalidLoginLocationException" to="casBadWorkstationView"/>
    <transition on="InvalidLoginTimeException" to="casBadHoursView"/>
    <transition on="FailedLoginException" to="generateLoginTicket"/>
    <transition on="AccountNotFoundException" to="generateLoginTicket"/>
    <transition on="UNKNOWN" to="generateLoginTicket"/>
    <!--加入自己的异常定义 -->
    <transition on="CredentialCheckFailException" to="generateLoginTicket"/> 
</action-state>

4. 添加验证码

自定义一个action类,把AuthenticationViaFormAction 这个类的源码copy过来,修改submit方法,加入校验逻辑,验证失败在第一个try的部分抛出自定义异常(模仿3中的做法)就可以达到效果,当然啦如果熟悉spring web flow就随便玩随便改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值