Shiro入门-整合验证码

来源:互联网 发布:晚上醒了就睡不着 知乎 编辑:程序博客网 时间:2024/06/01 09:57

思路
shiro使用FormAuthenticationFilter进行表单认证,验证校验的功能应该加在FormAuthenticationFilter中,在认证之前进行验证码校验。

需要写FormAuthenticationFilter的子类,继承FormAuthenticationFilter,改写它的认证方法,在认证之前进行验证码校验。

自定义FormAuthenticationFilter

public class CustomFormAuthenticationFilter extends FormAuthenticationFilter {    //原FormAuthenticationFilter的认证方法    @Override    protected boolean onAccessDenied(ServletRequest request,            ServletResponse response) throws Exception {        //在这里进行验证码的校验        //从session获取正确验证码        HttpServletRequest httpServletRequest = (HttpServletRequest) request;        HttpSession session =httpServletRequest.getSession();        //取出session的验证码(正确的验证码)        String validateCode = (String) session.getAttribute("validateCode");        //取出页面的验证码        //输入的验证和session中的验证进行对比         String randomcode = httpServletRequest.getParameter("randomcode");        if(randomcode!=null && validateCode!=null && !randomcode.equals(validateCode)){            //如果校验失败,将验证码错误失败信息,通过shiroLoginFailure设置到request中            httpServletRequest.setAttribute("shiroLoginFailure", "randomCodeError");            //拒绝访问,不再校验账号和密码             return true;         }        return super.onAccessDenied(request, response);    }}

配置自定义FormAuthenticationFilter spring-shiro.xml

<!-- Shiro 的Web过滤器 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">    <property name="securityManager" ref="securityManager" />    <!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,请求此地址将由formAuthenticationFilter进行表单认证 -->    <property name="loginUrl" value="/login.action" />    <!-- 认证成功统一跳转到first.action,建议不配置,shiro认证成功自动到上一个请求路径 -->    <property name="successUrl" value="/first.action"/>    <!-- 通过unauthorizedUrl指定没有权限操作时跳转页面-->    <property name="unauthorizedUrl" value="/refuse.jsp" />    <!-- 自定义filter配置 -->    <property name="filters">        <map>            <!-- 将自定义 的FormAuthenticationFilter注入shiroFilter中-->            <entry key="authc" value-ref="formAuthenticationFilter" />        </map>    </property><!-- 自定义form认证过虑器 --><!-- 基于Form表单的身份验证过滤器,不配置将也会注册此过虑器,表单中的用户账号、密码及loginurl将采用默认值,建议配置 --><bean id="formAuthenticationFilter"     class="cn.itcast.ssm.shiro.CustomFormAuthenticationFilter ">    <!-- 表单中账号的input名称 -->    <property name="usernameParam" value="username" />    <!-- 表单中密码的input名称 -->    <property name="passwordParam" value="password" />    <!-- 记住我input的名称 -->    <property name="rememberMeParam" value="rememberMe"/></bean>

在login.action对验证错误 进行解析

//登陆提交地址,和applicationContext-shiro.xml中配置的loginurl一致    @RequestMapping("login")    public String login(HttpServletRequest request)throws Exception{        //如果登陆失败从request中获取认证异常信息,shiroLoginFailure就是shiro异常类的全限定名        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");        //根据shiro返回的异常类路径判断,抛出指定异常信息        if(exceptionClassName!=null){            if (UnknownAccountException.class.getName().equals(exceptionClassName)) {                //最终会抛给异常处理器                throw new CustomException("账号不存在");            } else if (IncorrectCredentialsException.class.getName().equals(                    exceptionClassName)) {                throw new CustomException("用户名/密码错误");            } else if("randomCodeError".equals(exceptionClassName)){                throw new CustomException("验证码错误 ");            }else {                throw new Exception();//最终在异常处理器生成未知错误            }        }        //此方法不处理登陆成功(认证成功),shiro认证成功会自动跳转到上一个请求路径        //登陆失败还到login页面        return "login";    }

在filter配置匿名访问验证码jsp

0 0
原创粉丝点击