SSM藥品管理系統Spring+Shiro+MyBatis+LayUI

*簡介 :本課程採用Spring+MyBatis+Shiro完成藥品庫存管理系統,項目分爲兩類用戶,數據庫設、編碼到頁面展示全手工一步一步編寫。
在以前的開發中都是採用 action 方法中編寫相應的業務,現在我們建立一個 realm 實現數據處理(包括認證、授權);
這個類不需要手工編寫了,在之前 shiro 課程中都有詳細的說明;

 範例:登錄認證 
@Override 
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws 
AuthenticationException { 
    log.info("========登錄授權認證========"); 
    // 1 、取得用戶名
 
    String username = (String) authenticationToken.getPrincipal(); 
    try { 
        Admin vo = this.adminService.show(username); 
        if (vo == null) { 
            throw new UnknownAccountException("用戶不存在!"); 
        } else { 
            
// 2 、取得登錄密碼
 
            String password = (new String((char[]) authenticationToken.getCredentials())); 
            if (vo.getPassword().equals(password)) { 
                AuthenticationInfo auth = new SimpleAuthenticationInfo(username, password, "adminRealm"); 
                return auth; 
            } else { 
                throw new IncorrectCredentialsException("密碼錯誤!"); 
            } 
        } 
    } catch (Exception e) { 
        e.printStackTrace(); `
    } 
    return null; 
} 
 範例:修改 applicationContext.xml 文件 
<! -- 配置 SecurityManager 的管理
 
-- > 
<bean id="adminRealm" class="cn.xmkeshe.realm.AdminRealm"/> 
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 
    <! -- 配置需要使用的 Realm -- > 
    <property name="realm" ref="adminRealm"/> 
</bean> 
 範例:定義控制層代碼實現登錄檢測 
@RequestMapping("login") 
public void login(Admin vo, HttpServletResponse response) throws IOException { 
    // 數據驗證
 
    if (ValidateUtils. validateEmpty (vo.getAid()) && ValidateUtils. validateEmpty (vo.getPassword())) { 
        String aid = vo.getAid(); // aid 
        String password = new MD5Code().getMD5ofStr(vo.getPassword()); 
        UsernamePasswordToken token = new UsernamePasswordToken(aid, password); 
        Subject sub = SecurityUtils. getSubject (); 
        response.getWriter().print("success"); 
        sub.login(token); 
    } else { 
        response.getWriter().print("errorNull"); 
  ![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200421172356234.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lsY3Rv,size_16,color_FFFFFF,t_70#pic_center)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章