天天看點

Shiro入門-整合驗證碼

思路

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>      
//登陸送出位址,和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";
    }      

繼續閱讀