Shiro在web的基礎驗證(登錄驗證)

1、建立Servlet程序來進行具體的登錄操作處理。

@WebServlet("/shiroLogin")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mid = request.getParameter("mid") ;
        String password = request.getParameter("password") ;
        Subject subject = SecurityUtils.getSubject() ;
        UsernamePasswordToken token = new UsernamePasswordToken(mid,password) ;
        subject.login(token);
        request.getSession().setAttribute("mid", mid);
        request.getRequestDispatcher("/pages/welcome.jsp").forward(request, response);
    } 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

}

2、在項目中建立前臺表單界面

<form action="shiroLogin" method="post">
        用戶名:<input type="text" name="mid" id="mid"><br>
        密&nbsp;碼:<input type="password" name="password" id="password"><br>
        <input type="submit" value="登錄">
        <input type="reset" value="重置">
</form> 

3、設置驗證的Realm

public class MyRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("********** 2、用戶角色與權限:doGetAuthorizationInfo **********");
        String username = (String) principals.getPrimaryPrincipal() ;   // 取得用戶登錄名
        SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo() ;  // 定義授權信息的返回數據
        MemberLoginService service = new MemberLoginService() ; // 進行業務層處理
        auth.setRoles(service.listRolesByMember(username));// 所有的角色必須以Set集合的形式出現
        auth.setStringPermissions(service.listActionsByMember(username));   // 所有的權限必須以Set集合的形式出現
        service.close();
        return auth;
    }
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("********** 1、用戶登錄認證:doGetAuthenticationInfo() **********");
        // 1、登錄認證的方法需要先執行,需要用他來判斷登錄的用戶信息是否合法
        String username = (String) token.getPrincipal() ;   // 取得用戶名
        // 需要通過用戶名取得用戶的完整信息,利用業務層操作
        MemberLoginService service = new MemberLoginService() ;
        Member vo = service.get(username) ; // 需要取得的是用戶的信息
        service.close(); 
        if (vo == null) {
            throw new UnknownAccountException("該用戶名稱不存在!") ;
        } else {    // 進行密碼的驗證處理
            String password = new String((char []) token.getCredentials()) ;
            // 將數據庫中的密碼與輸入的密碼進行比較,這樣就可以確定當前用戶是否可以正常登錄
            if (vo.getPassword().equals(password)) {    // 密碼正確
                AuthenticationInfo auth = new SimpleAuthenticationInfo(username, password, "memberRealm") ;
                return auth ;
            } else {
                throw new IncorrectCredentialsException("密碼錯誤!") ;
            }
        }
    } 

4、設置shiro.ini文件

[main]
# 如果現在認證失敗,則跳轉到loginUrl配置的路徑
authc.loginUrl=/login.jsp
# 需要配置上當角色認證失敗之後的跳轉頁面
roles.unauthorizedUrl=/role.jsp
jdbcRealm=MyRealm
securityManager.realms=$jdbcRealm
# 配置所有需要進行路徑檢測的頁面
[urls]
# 登錄的頁面是不需要進行檢測處理的
/shiroLogin=anon
# 指定的頁面需要進行登錄檢測,此時表示需要先進行身份認證,而後再進行角色認證
/pages/welcome.jsp=authc

以上,就完成了web的前臺登錄驗證

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章