BOS項目 第7天(shiro權限框架進行認證和授權)
BOS項目筆記 第7天
今天內容安排:
1、權限概述(認證、授權)
2、常見的權限控制的方式(URL攔截權限控制、方法註解權限控制)
3、權限數據模型(權限表、角色表、用戶表、角色權限關系表、用戶角色關系表)
4、shiro框架入門
5、將shiro應用到bos項目中進行認證和授權
1. 權限概述
系統提供了很多功能,並不是所有的用戶登錄系統都可以操作這些功能。我們需要對用戶的訪問進行控制。
認證:系統提供的用於識別用戶身份的功能(通常是登錄功能)-----讓系統知道你是誰??
授權:系統提供的賦予用戶訪問某個功能的能力-----讓系統知道你能做什麽??
2. 常見的權限控制的方式
2.1 URL攔截權限控制---基於過濾器或者攔截器
2.2 方法註解權限控制---基於代理技術
3. 權限模塊數據模型
用戶表:t_user
角色表:auth_role
權限表:auth_function
用戶角色關系表:user_role
角色權限關系表:role_function
4. apache shiro
官網:http://shiro.apache.org/
Apache Shiro is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.
l 提供的功能:
l shiro的程序運行流程圖:
Application code:應用程序代碼,開發人員編寫的代碼
Subject:主體,當前用戶
SecurityManager:安全管理器,shiro框架的核心對象,管理各個組件
Realm:類似於Dao,負責訪問安全數據(用戶數據、角色數據、權限數據)
5. 將shiro應用到bos項目
第一步:導入shiro-all.jar
第二步:在web.xml中配置一個spring用於整合shiro的過濾器
第三步:在spring配置文件中配置一個bean,id必須和上面的過濾器名稱相同
<!-- 配置一個工廠 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- 註入安全管理器 --> <property name="securityManager" ref="securityManager"></property> <!-- 註入當前系統的登錄頁面 --> <property name="loginUrl" value="/login.jsp"/> <!-- 註入成功頁面 --> <property name="successUrl" value="/index.jsp"/> <!-- 註入權限不足提示頁面 --> <property name="unauthorizedUrl" value="/unauthorizedUrl.jsp"/> <!-- 註入URL攔截規則 --> <property name="filterChainDefinitions"> <value> /css/** = anon /images/** = anon /js/** = anon /login.jsp* = anon /validatecode.jsp* = anon /userAction_login.action = anon /page_base_staff.action = perms["staff"] /* = authc </value> </property> </bean> |
第四步:在spring配置文件中註冊安全管理器,為安全管理器註入realm
<!-- 註冊自定義realm --> <bean id="bosRealm" class="com.itheima.bos.shiro.BOSRealm"></bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- 註入上面的realm --> <property name="realm" ref="bosRealm"/> </bean> |
第五步:自定義一個BOSRealm
public class BOSRealm extends AuthorizingRealm { @Resource private IUserDao userDao; /** * 認證方法 */ protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token) throws AuthenticationException { System.out.println("認證方法。。。"); UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername();// 從令牌中獲得用戶名
User user = userDao.findUserByUsername(username); if (user == null) { // 用戶名不存在 return null; } else { // 用戶名存在 String password = user.getPassword();// 獲得數據庫中存儲的密碼 // 創建簡單認證信息對象 /*** * 參數一:簽名,程序可以在任意位置獲取當前放入的對象 * 參數二:從數據庫中查詢出的密碼 * 參數三:當前realm的名稱 */ SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, this.getClass().getSimpleName()); return info;//返回給安全管理器,由安全管理器負責比對數據庫中查詢出的密碼和頁面提交的密碼 } } /** * 授權方法 */ protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) { return null; } } |
第六步:完善UserAction的login方法
public String login(){ //生成的驗證碼 String key = (String) ServletActionContext.getRequest().getSession().getAttribute("key"); //判斷用戶輸入的驗證碼是否正確 if(StringUtils.isNotBlank(checkcode) && checkcode.equals(key)){ //驗證碼正確 //獲得當前用戶對象 Subject subject = SecurityUtils.getSubject();//狀態為“未認證” String password = model.getPassword(); password = MD5Utils.md5(password); //構造一個用戶名密碼令牌 AuthenticationToken token = new UsernamePasswordToken(model.getUsername(), password); try{ subject.login(token); }catch (UnknownAccountException e) { e.printStackTrace(); //設置錯誤信息 this.addActionError(this.getText("usernamenotfound")); return "login"; }catch (Exception e) { e.printStackTrace(); //設置錯誤信息 this.addActionError(this.getText("loginError")); return "login"; } //獲取認證信息對象中存儲的User對象 User user = (User) subject.getPrincipal(); ServletActionContext.getRequest().getSession().setAttribute("loginUser", user); return "home"; }else{ //驗證碼錯誤,設置錯誤提示信息,跳轉到登錄頁面 this.addActionError(this.getText("validateCodeError")); return "login"; } } |
第七步:在自定義Realm中編寫授權方法
6. shiro提供的權限控制方式
6.1 URL攔截權限控制
6.2 方法註解權限控制
第一步:在spring配置文件中開啟shiro的註解支持
第二步:在Action的方法上使用shiro的註解描述執行當前方法需要具有的權限
第三步:修改BaseAction的構造方法
第四步:在struts.xml中配置全局異常捕獲,統一跳轉到權限不足的頁面
6.3 頁面標簽權限控制
第一步:在jsp頁面中引入shiro的標簽
第二步:使用shiro的標簽根據當前用戶擁有的權限動態展示頁面元素
6.4 代碼級別權限(了解)
BOS項目 第7天(shiro權限框架進行認證和授權)