技术分享 CAS单点登录 服务端配置 查看内容

cas 入门之二十一:用户错误登录次数限制

老高 | 发布于 2017-05-05 14:16| 浏览()| 评论() | 收藏() | 点赞() | 打印

摘要: 用户错误登录限制简而言之就是限制单个用户单位时间内登录系统的错误次数。当用户在单位时间内登录系统的次数达到上限,就对这个 用户采取相应的措施。

用户错误登录限制简而言之就是限制单个用户单位时间内登录系统的错误次数。当用户在单位时间内登录系统的次数达到上限,就对这个

用户采取相应的措施。

对于单节点CAS情况下,用户登录限制有两个方法:一是按IP地址限制,另一个通过IP地址+用户名组合限制。 对于多节点CAS情况下,只能结合Inspektr的审计功能来限制用户登录。

首先配置了限制用户登录的拦截器。当一个用户在单位时间内登录的错误次数达到设定的上限,则再次登录cas服务器,则直接会被拦截。则该用户只有等待,随着时间的推移用户错误登录的频率低于一个值时或者错误记录被系统清理之后便可登录。

单节点 cas配置:

1.在 cas/WEB-INF/cas-servlet.xml中

配置用户登录拦截器:

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping" p:flowRegistry-ref="flowRegistry"
        p:order="2">
    <property name="interceptors">
      <ref local="localeChangeInterceptor"/>
    </property>
</bean>

改为

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"
    p:flowRegistry-ref="flowRegistry" p:order="2">
    <property name="interceptors">
        <list>
            <ref local="localeChangeInterceptor"/>
            <ref bean="throttleInterceptor"/>
        </list>
    </property>
</bean>

2.cas/WEB-INF/spring-configuration/中新增

throttleInterceptorTrigger.xml文件(内容如下):

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--  这个类的工作原理 
即在一秒内用户错误登录的频率大于failureThreshold/failureRangeInSeconds时候,就会被阻止
-->
<bean id="throttleInterceptor"
    class="org.jasig.cas.web.support.InMemoryThrottledSubmissionByIpAddressAndUsernameHandlerInterceptorAdapter"
    p:failureRangeInSeconds="100"
    p:failureThreshold="120"/> 

<!-- 工作原理同上,根据ip限制
<bean id="throttleInterceptor"
    class="org.jasig.cas.web.support.InMemoryThrottledSubmissionByIpAddressHandlerInterceptorAdapter"
    p:failureRangeInSeconds="100"
    p:failureThreshold="120"/>

-->

<bean id="throttleInterceptorJobDetail"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
    p:targetObject-ref="throttleInterceptor"
    p:targetMethod="decrementCounts" />
    <bean id="periodicThrottleCleanerTrigger"
    class="org.springframework.scheduling.quartz.SimpleTriggerBean"
    p:jobDetail-ref="throttleInterceptorJobDetail"
    p:startDelay="0"
    p:repeatInterval="1000" />
</beans>

3.配置over;


cas 多节点配置:

1.在 cas/WEB-INF/cas-servlet.xml中

配置用户登录拦截器:

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping" p:flowRegistry-ref="flowRegistry"
        p:order="2">
    <property name="interceptors">
      <ref local="localeChangeInterceptor"/>
    </property>
</bean>

改为

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"
    p:flowRegistry-ref="flowRegistry" p:order="2">
    <property name="interceptors">
        <list>
            <ref local="localeChangeInterceptor"/>
            <ref bean="throttleInterceptor"/>
        </list>
    </property>
</bean>

2.cas/WEB-INF/spring-configuration/中新增

throttleInterceptorTrigger.xml文件(内容如下):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!--  
    所用的数据库表COM_AUDIT_TRAIL,且不能更改,并且必须配置cas的Inspektr的数据库审记功能
    -->
    <bean  id="throttleInterceptor"
        class="org.jasig.cas.web.support.InspektrThrottledSubmissionByIpAddressAndUsernameHandlerInterceptorAdapter"
        p:failureRangeInSeconds="100"
        p:failureThreshold="120">
        <constructor-arg index="0" ref="auditTrailManager" />
        <constructor-arg index="1" ref="dataSource" />
    </bean>
</beans>

通过Inspektr的数据库记录审计日志功能,则进行用户错误登录限制。你可以统一将用户的日志审计与用户错误登录限制都统一用Inspektr的jdbc形式记录,当然也可以分开,这个要配置是随意的。关于cas的Inspektr的JdbcAuditTrailManager配置, 请参看 cas入门之十九:cas审计日志Inspektr(下)。

3.配置over;

发表评论(对文章涉及的知识点还有疑问,可以在这里留言,老高看到后会及时回复的。)

表情