之前自己通过修改源码实现了以用户名为单位限制其密码输入次数,但这样做不是很合理,如果是同一ip不同用户名呢?于是就想把也通过ip限制的功能加上。今天无意逛官网时竟然发现CAS里面有类似的功能,可以通过用户名或ip来做登录限制。没有仔细查看文档的结果就是做了一点无用功。下面介绍下开启cas限制密码输入次数的功能。有两种方法:一是把状态保存到内存中,二是通过Inspektr来管理。下面主要介绍保存到内存的方法(使用3.4.10版本的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-2.0.xsd"> <bean id="throttleInterceptor" class="org.jasig.cas.web.support.InMemoryThrottledSubmissionByIpAddressAndUsernameHandlerInterceptorAdapter" p:failureRangeInSeconds="120" p:failureThreshold="2" /> <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="20000" /> </beans>
其中failureThreshold为同一ip或同一用户允许错误次数
到cas-servlet.xml配置文件修改FlowHandlerMappingbean,如下:
<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>
完成