1. 程式人生 > >國際物流雲商專案day5

國際物流雲商專案day5

1、取消我們自己寫的密碼比較器(因為我們繼承SimpleCredentialsMatcher
並重寫類他的方法public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info))
意思就是不用寫這個類也會執行密碼比較器 這Shiro已經寫好的
但必須在token之前將密碼進行加密;
MD5Hash Md5 = new MD5Hash(password,username,2)
UsernamePasswordToken token = new UsernamePasswordToken(username, password);

2、授權

使用shiro標籤 必須匯入標籤庫<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
頁面中使用一個shiro標籤 shiro:hasPermission 內部會找編寫的許可權程式碼
//意思是  是否有abc這個模組的許可權
<shiro:hasPermission name="abc"></shiro:hasPermission>
當進入這個標籤時 
會跳轉到我們寫的授權和認證的類中的方法
可以在這個方法中檢視使用者是否有abc這許可權

// 授權方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	// 1、獲取使用者資訊
	User user = (User) arg0.getPrimaryPrincipal();
	// 2、獲取使用者下所有角色
	Set<Role> roles = user.getRoles();
	for (Role role : roles) {
		// 獲取角色下的所有模組
		Set<Module> modules = role.getModules();
		for (Module module : modules) {
			// 3、授權
			info.addStringPermission(module.getCpermission());
		}
	}
	return info;
}
	
	//這就是在登入時存的密碼
	new AuthorizationInfo 的實現類  SimpleAuthorizationInfo
	呼叫方法hasPermission ("abc")//現在這個使用者具有abc這個模組的許可權了
	通過引數PrincipalCollection 獲取我們的使用者資訊
	(使用者資訊是在認證結束時存在裡面的)SimpleAuthenticationInfo(Object principal, Object credentials, String realmName)
	引數的方法:arg0.getPrimaryPrincipal() 獲取主要的結束使用者資訊
	得到使用者資訊就可以檢視具體有哪些許可權 

3、過濾器鏈加上 perms過濾器 可以指定地方跳轉

1、perms是登入後看你有沒有這個許可權請問別的模組 
		沒有強行從位址列進入 看具體怎麼設定 直接跳轉到什麼地方看業務邏輯
		沒有許可權跳轉到index.jsp
		<property name="unauthorizedUrl" value="/index.jsp"/>
2、perms的配置注意事項 
   不建議使用的配置 因為有時候攔截不了
    /XXXAction* = perms["xxxx"]
	建議加上路徑
	/xxxxx/xxxxAction* = perms["xxxx"]

4、有註解方式加入攔截器 攔截到了會丟擲異常,要處理異常,並指定跳轉頁面
註解具體加在哪裡你是
註解開發: 加在service 因為加在Action層 會與與Spring註解有衝突
配置檔案開發:加在Action

註解具體加在哪裡看你的業務邏輯
@RequiresPermissions(value="xxx管理")

5、左側選單動態載入說明

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<ul>
	  <!-- 定義一個變數  賦空值 -->
        <c:set var="aaa" value=""/>
        <!-- 遍歷當前登入使用者的角色列表   在session域獲取使用者角色   遍歷  -->
		<c:forEach items="${_CURRENT_USER.roles }" var="role">
		       <!-- 遍歷每個角色下的模組 -->
		       <c:forEach items="${role.modules }" var="module">
		            <!-- 如果該模組沒有輸出過,則要進行輸出,否則這個模組就不輸出 -->
		            <c:if test="${(moduleName eq module.remark) and module.ctype==1  }">
		           		 <!--fn:contains 函式是判斷 “aaa” 中是否包含module.cpermission    -->
			               <c:if test="${fn:contains(aaa,module.cpermission) eq false }">
			                  <c:set var="aaa" value="${aaa},${module.cpermission }"/>
		                      <li>
			                      <a href="${ctx}/${module.curl}" onclick="linkHighlighted(this)" 	target="main" id="aa_1">
			                      ${module.cpermission }
			                      </a>
		                     </li>
		                 </c:if>  
		            </c:if>
		            
		       </c:forEach>
		</c:forEach>
</ul>