Shiro验证码检测

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>   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值