1、使用kaptcha验证码组件,将所需要的开发包配置到项目之中;
2、需要自定义一个“org.apache.shiro.web.filter.authc.FormAuthenticationFilter”它的子类,而后覆写该类中的指定方法。
/**
* 在已有的Form认证授权器基础上扩展一个新的子类
* @author mldn
*/
public class CustomerFormAuthenticationFilter extends FormAuthenticationFilter {
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
// 1、如果要想取得在Session中出现的验证码,则必须取得HttpSession接口对象
HttpServletRequest req = (HttpServletRequest) request ;
HttpSession session = req.getSession() ; // 取得当前的session对象
String rand = (String) session.getAttribute("rand") ; // 取得生成的验证码
// 2、取得用户提交表单过来的验证码数据
String code = request.getParameter("code") ;
if (rand == null || code == null || "".equals(rand) || "".equals(code)) {
request.setAttribute("code", "验证码不允许为空!");
return true ; // 拒绝访问,不再进行用户名或密码的检测
} else {
if (!code.equalsIgnoreCase(rand)) { // 验证码输入错误
request.setAttribute("code", "验证码输入错误!");
return true ;
}
}
return super.onAccessDenied(request, response) ; // 操作继续向后执行
}
}
3、修改已有的表单登录检测器:
<!-- 此处表示使用内置的表单登录控制验证 -->
<bean id="formAuthenticationFilter" class="cn.filter.CustomerFormAuthenticationFilter">
<!-- 定义出需要使用的参数,此参数与表单一一对应 -->
<property name="usernameParam" value="mid"/>
<property name="passwordParam" value="password"/>
<property name="rememberMeParam" value="rememberMe"/>
<property name="loginUrl" value="/loginUrl.action"/>
</bean>