主頁 > 後端開發 > Spring Boot動態權限變更實作的整體方案

Spring Boot動態權限變更實作的整體方案

2021-06-30 06:13:34 後端開發

1、前言

???在Web專案中,權限管理即權限訪問控制為網站訪問安全提供了保障,并且很多專案使用了Session作為快取,結合AOP技術進行token認證和權限控制,權限控制流程大致如下圖所示:

???現在,如果管理員修改了用戶的角色,或修改了角色的權限,都會導致用戶權限發生變化,此時如何實作動態權限變更,使得前端能夠更新用戶的權限樹,后端訪問鑒權AOP模塊能夠知悉這種變更呢?

2、問題及解決方案

????現在的問題是,管理員沒法訪問用戶Session,因此沒法將變更通知此用戶,而用戶如果已經登錄,或直接關閉瀏覽器頁面而不是登出操作,Session沒有過期前,用戶訪問介面時,訪問鑒權AOP模塊仍然是根據之前快取的Session資訊進行處理,沒法做到動態權限變更,

????使用Security+WebSocket是一個方案,但沒法處理不在線用戶,

?????解決方案的核心思想是利用ServletContext物件的共享特性,來實作用戶權限變更的資訊傳遞,然后在AOP類中查詢用戶是否有變更通知記錄需要處理,如果權限發生變化,則修改response訊息體,添加附加通知資訊給前端,前端收到附加的通知資訊,可更新功能權限樹,并進行相關處理,

?????這樣,利用的變更通知服務,不僅后端的用戶url訪問介面可第一時間獲悉變更,還可以通知到前端,從而實作了動態權限變更,

3、方案實作

3.1、開發變更通知類

?????服務介面類ChangeNotifyService,代碼如下:

package com.abc.questInvest.service;

/**
 * @className		: ChangeNotifyService
 * @description		: 變更通知服務
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2021/06/28	1.0.0		sheng.zheng		初版
 *
 */
public interface ChangeNotifyService {

	/**
	 * 
	 * @methodName		: getChangeNotifyInfo
	 * @description		: 獲取指定用戶ID的變更通知資訊 
	 * @param userId	: 用戶ID
	 * @return		: 回傳0表示無變更通知資訊,其它值按照bitmap編碼,目前定義如下:
	 * 		bit0:	: 修改用戶的角色組合值,從而導致權限變更;
	 * 		bit1:	: 修改角色的功能項,從而導致權限變更;
	 * 		bit2:	: 用戶禁用,從而導致權限變更;
	 * 		bit3:	: 用戶調整部門,從而導致資料權限變更;
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/28	1.0.0		sheng.zheng		初版
	 *
	 */
	public Integer getChangeNotifyInfo(Integer userId);
	
	/**
	 * 
	 * @methodName		: setChangeNotifyInfo
	 * @description		: 設定變更通知資訊
	 * @param userId	: 用戶ID
	 * @param changeNotifyInfo	: 變更通知值
	 * 		bit0:	: 修改用戶的角色組合值,從而導致權限變更;
	 * 		bit1:	: 修改角色的功能項,從而導致權限變更;
	 * 		bit2:	: 用戶禁用,從而導致權限變更;
	 * 		bit3:	: 用戶調整部門,從而導致資料權限變更;
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/28	1.0.0		sheng.zheng		初版
	 *
	 */
	public void setChangeNotifyInfo(Integer userId,Integer changeNotifyInfo); 	
}

?????服務實作類ChangeNotifyServiceImpl,代碼如下:

package com.abc.questInvest.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.abc.questInvest.service.ChangeNotifyService;

/**
 * @className		: ChangeNotifyServiceImpl
 * @description		: ChangeNotifyService實作類
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2021/06/28	1.0.0		sheng.zheng		初版
 *
 */
@Service
public class ChangeNotifyServiceImpl implements ChangeNotifyService {
	
	//用戶ID與變更過通知資訊映射表
	private Map<Integer,Integer> changeNotifyMap = new HashMap<Integer,Integer>();
	
	/**
	 * 
	 * @methodName		: getChangeNotifyInfo
	 * @description		: 獲取指定用戶ID的變更通知資訊 
	 * @param userId	: 用戶ID
	 * @return		: 回傳0表示無變更通知資訊,其它值按照bitmap編碼,目前定義如下:
	 * 		bit0:	: 修改用戶的角色組合值,從而導致權限變更;
	 * 		bit1:	: 修改角色的功能項,從而導致權限變更;
	 * 		bit2:	: 用戶禁用,從而導致權限變更;
	 * 		bit3:	: 用戶調整部門,從而導致資料權限變更;
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/28	1.0.0		sheng.zheng		初版
	 *
	 */
	@Override
	public Integer getChangeNotifyInfo(Integer userId) {
		Integer changeNotifyInfo = 0;
		//檢查該用戶是否有變更通知資訊
		if (changeNotifyMap.containsKey(userId)) {
			changeNotifyInfo = changeNotifyMap.get(userId);
			//移除資料,加鎖保護
			synchronized(changeNotifyMap) {
				changeNotifyMap.remove(userId);
			}
		}
		return changeNotifyInfo;
	}
	
	/**
	 * 
	 * @methodName		: setChangeNotifyInfo
	 * @description		: 設定變更通知資訊,該功能一般由管理員觸發呼叫
	 * @param userId	: 用戶ID
	 * @param changeNotifyInfo	: 變更通知值
	 * 		bit0:	: 修改用戶的角色組合值,從而導致權限變更;
	 * 		bit1:	: 修改角色的功能項,從而導致權限變更;
	 * 		bit2:	: 用戶禁用,從而導致權限變更;
	 * 		bit3:	: 用戶調整部門,從而導致資料權限變更;
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/28	1.0.0		sheng.zheng		初版
	 *
	 */
	@Override
	public void setChangeNotifyInfo(Integer userId,Integer changeNotifyInfo) {
		//檢查該用戶是否有變更通知資訊
		if (changeNotifyMap.containsKey(userId)) {
			//如果有,表示之前變更通知未處理
			//獲取之前的值
			Integer oldChangeNotifyInfo = changeNotifyMap.get(userId);
			//計算新值,bitmap編碼,或操作
			Integer newChangeNotifyInfo = oldChangeNotifyInfo | changeNotifyInfo;
			//設定資料,加鎖保護
			synchronized(changeNotifyMap) {
				changeNotifyMap.put(userId,newChangeNotifyInfo);
			}
		}else {
			//如果沒有,設定一條
			changeNotifyMap.put(userId,changeNotifyInfo);
		}
	}
}

????此處,變更通知型別,與使用的demo專案有關,目前定義了4種變更通知型別,實際上,除了權限相關的變更,還有與Session快取欄位相關的變更,也需要通知,否則用戶還是在使用舊資料,

3.2、將變更通知類物件,納入全域配置服務物件中進行管理

?????全域配置服務類GlobalConfigService,負責管理全域的配置服務物件,服務介面類代碼如下:

package com.abc.questInvest.service;

/**
 * @className		: GlobalConfigService
 * @description		: 全域變數管理類
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2021/06/02	1.0.0		sheng.zheng		初版
 *
 */
public interface GlobalConfigService {
	
	/**
	 * 
	 * @methodName		: loadData
	 * @description		: 加載資料 
	 * @return		: 成功回傳true,否則回傳false
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/02	1.0.0		sheng.zheng		初版
	 *
	 */
	public boolean loadData();
	
	//獲取TableCodeConfigService物件
	public TableCodeConfigService getTableCodeConfigService();	
	
	//獲取SysParameterService物件
	public SysParameterService getSysParameterService();
	
	//獲取FunctionTreeService物件
	public FunctionTreeService getFunctionTreeService();

	//獲取RoleFuncRightsService物件
	public RoleFuncRightsService getRoleFuncRightsService();
	
	//獲取ChangeNotifyService物件
	public ChangeNotifyService getChangeNotifyService();
	
}

?????服務實作類GlobalConfigServiceImpl,代碼如下:

package com.abc.questInvest.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.abc.questInvest.service.ChangeNotifyService;
import com.abc.questInvest.service.FunctionTreeService;
import com.abc.questInvest.service.GlobalConfigService;
import com.abc.questInvest.service.RoleFuncRightsService;
import com.abc.questInvest.service.SysParameterService;
import com.abc.questInvest.service.TableCodeConfigService;

/**
 * @className		: GlobalConfigServiceImpl
 * @description		: GlobalConfigService實作類
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2021/06/02	1.0.0		sheng.zheng		初版
 *
 */
@Service
public class GlobalConfigServiceImpl implements GlobalConfigService{
	
	//ID編碼配置表資料服務
	@Autowired
	private TableCodeConfigService tableCodeConfigService;
	
	//系統引數表資料服務
	@Autowired
	private SysParameterService sysParameterService;
	
	//功能樹表資料服務
	@Autowired
	private FunctionTreeService functionTreeService;
	
	//角色權限表資料服務
	@Autowired	
	private RoleFuncRightsService roleFuncRightsService;
	
	//變更通知服務
	@Autowired	
	private ChangeNotifyService changeNotifyService;
	
	
	/**
	 * 
	 * @methodName		: loadData
	 * @description		: 加載資料 
	 * @return		: 成功回傳true,否則回傳false
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/02	1.0.0		sheng.zheng		初版
	 *
	 */
	@Override
	public boolean loadData() {
		boolean bRet = false;
		
		//加載table_code_config表記錄
		bRet = tableCodeConfigService.loadData();
		if (!bRet) {
			return bRet;
		}
		
		//加載sys_parameters表記錄
		bRet = sysParameterService.loadData();
		if (!bRet) {
			return bRet;
		}
		
		//changeNotifyService目前沒有持久層,無需加載
		//如果服務重啟,資訊丟失,也沒關系,因為此時Session也會失效
		
		//加載function_tree表記錄
		bRet = functionTreeService.loadData();
		if (!bRet) {
			return bRet;
		}
		
		//加載role_func_rights表記錄
		//先設定完整功能樹
		roleFuncRightsService.setFunctionTree(functionTreeService.getFunctionTree());
		//然后加載資料
		bRet = roleFuncRightsService.loadData();
		if (!bRet) {
			return bRet;
		}
		
		return bRet;
	}
	
	//獲取TableCodeConfigService物件
	@Override
	public TableCodeConfigService getTableCodeConfigService() {
		return tableCodeConfigService;
	}
	
	//獲取SysParameterService物件
	@Override
	public SysParameterService getSysParameterService() {
		return sysParameterService;
	}
	
	//獲取FunctionTreeService物件
	@Override
	public FunctionTreeService getFunctionTreeService() {
		return functionTreeService;
	}	
	
	//獲取RoleFuncRightsService物件
	@Override
	public RoleFuncRightsService getRoleFuncRightsService() {
		return roleFuncRightsService;
	}
	
	//獲取ChangeNotifyService物件
	@Override
	public ChangeNotifyService getChangeNotifyService() {
		return changeNotifyService;
	}

}

????GlobalConfigServiceImpl類,管理了很多配置服務類,此處主要關注ChangeNotifyService類物件,

3.3、使用ServletContext,管理全域配置服務類物件

?????全域配置服務類在應用啟動時加載到Spring容器中,這樣可實作共享,減少對資料庫的訪問壓力,

?????實作一個ApplicationListener類,代碼如下:

package com.abc.questInvest;

import javax.servlet.ServletContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.abc.questInvest.service.GlobalConfigService;

/**
 * @className	: ApplicationStartup
 * @description	: 應用偵聽器
 *
 */
@Component
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{
    //全域變數管理物件,此處不能自動注入
    private GlobalConfigService globalConfigService = null;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        try {
    	    if(contextRefreshedEvent.getApplicationContext().getParent() == null){ 
    	    	//root application context 沒有parent.
				
    	    	System.out.println("========定義全域變數==================");
    	    	// 將 ApplicationContext 轉化為 WebApplicationContext
    	        WebApplicationContext webApplicationContext =
    	                (WebApplicationContext)contextRefreshedEvent.getApplicationContext();
    	        // 從 webApplicationContext 中獲取  servletContext
    	        ServletContext servletContext = webApplicationContext.getServletContext();
    	        
    	        //加載全域變數管理物件
    	        globalConfigService = (GlobalConfigService)webApplicationContext.getBean(GlobalConfigService.class);
    	        //加載資料
    	        boolean bRet = globalConfigService.loadData();
    	        if (false == bRet) {
    	        	System.out.println("加載全域變數失敗");
    	        	return;
    	        }        
    	        //======================================================================
    	        // servletContext設定值
    	        servletContext.setAttribute("GLOBAL_CONFIG_SERVICE", globalConfigService);  
    	        
    	    }
    	} catch (Exception e) {
    	    e.printStackTrace();
    	}        
    }
}

?????在啟動類中,加入該應用偵聽器ApplicationStartup,

	public static void main(String[] args) {
    	SpringApplication springApplication = new SpringApplication(QuestInvestApplication.class);
        springApplication.addListeners(new ApplicationStartup());
        springApplication.run(args);  
	}

????現在,有了一個GlobalConfigService型別的全域變數globalConfigService,

3.4、發出變更通知

?????此處舉2個例子,說明發出變更通知的例子,這兩個例子,都在用戶管理模塊,UserManServiceImpl類中,

?????1)管理員修改用戶資訊,可能導致權限相關項發生變動,2)禁用用戶,發出變更過通知,

?????發出通知的相關代碼如下:

	/**
	 * 
	 * @methodName		: editUser
	 * @description		: 修改用戶資訊
	 * @param userInfo	: 用戶資訊物件
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/08	1.0.0		sheng.zheng		初版
	 * 2021/06/28	1.0.1		sheng.zheng		增加變更通知的處理
	 *
	 */
	@Override
	public void editUser(HttpServletRequest request,UserInfo userInfo) {
		//輸入引數校驗
		checkValidForParams("editUser",userInfo);
		
		//獲取操作人賬號
		String operatorName = (String) request.getSession().getAttribute("username");
		userInfo.setOperatorName(operatorName);		

		//登錄名和密碼不修改
		userInfo.setLoginName(null);
		userInfo.setSalt(null);
		userInfo.setPasswd(null);
		
		//獲取修改之前的用戶資訊
		Integer userId = userInfo.getUserId();
		UserInfo oldUserInfo = userManDao.selectUserByKey(userId);

		//修改用戶記錄
		try {
			userManDao.updateSelective(userInfo);			
		}catch(Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			throw new BaseException(ExceptionCodes.USERS_EDIT_USER_FAILED);
		}
		
		//檢查是否有需要通知的變更
		Integer changeFlag = 0;
		if (userInfo.getRoles() != null) {
			if(oldUserInfo.getRoles() != userInfo.getRoles()) {
				//角色組合有變化,bit0
				changeFlag |= 0x01;
			}
		}
		if (userInfo.getDeptId() != null) {
			if (oldUserInfo.getDeptId() != userInfo.getDeptId()) {
				//部門ID有變化,bit3
				changeFlag |= 0x08;
			}
		}
		if (changeFlag > 0) {
			//如果有變更過通知項
			//獲取全域變數
			ServletContext servletContext = request.getServletContext();
			GlobalConfigService globalConfigService = (GlobalConfigService)servletContext.getAttribute("GLOBAL_CONFIG_SERVICE");
			globalConfigService.getChangeNotifyService().setChangeNotifyInfo(userId, changeFlag);			
		}
	}

	/**
	 * 
	 * @methodName		: disableUser
	 * @description		: 禁用用戶
	 * @param params	: map物件,形式如下:
	 * 	{
	 * 		"userId"	: 1
	 * 	}
	 * @history		:
	 * ------------------------------------------------------------------------------
	 * date			version		modifier		remarks                   
	 * ------------------------------------------------------------------------------
	 * 2021/06/08	1.0.0		sheng.zheng		初版
	 * 2021/06/28	1.0.1		sheng.zheng		增加變更通知的處理
	 *
	 */
	@Override
	public void disableUser(HttpServletRequest request,Map<String,Object> params) {
		//輸入引數校驗
		checkValidForParams("disableUser",params);
		
		UserInfo userInfo = new UserInfo();
		
		//獲取操作人賬號
		String operatorName = (String) request.getSession().getAttribute("username");
		
		//設定userInfo資訊
		Integer userId = (Integer)params.get("userId");
		userInfo.setUserId(userId);
		userInfo.setOperatorName(operatorName);
		//設定禁用標記
		userInfo.setDeleteFlag((byte)1);
		
		//修改密碼
		try {
			userManDao.updateEnable(userInfo);			
		}catch(Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			throw new BaseException(ExceptionCodes.USERS_EDIT_USER_FAILED);
		}		
		
		//禁用用戶,發出變更通知
		//獲取全域變數
		ServletContext servletContext = request.getServletContext();
		GlobalConfigService globalConfigService = (GlobalConfigService)servletContext.getAttribute("GLOBAL_CONFIG_SERVICE");
		//禁用用戶:bit2
		globalConfigService.getChangeNotifyService().setChangeNotifyInfo(userId, 0x04);				
	}

????本demo專案的角色相對較少,沒有使用用戶角色關系表,而是使用了bitmap編碼,角色ID取值為2^n,用戶角色組合roles欄位為一個Integer值,如roles=7,表示角色ID組合=[1,2,4],
????另外,如果修改了角色的功能權限集合,則需要查詢受影響的用戶ID串列,依次發出通知,可類似處理,

3.5、修改Response回應訊息體

?????Response回應訊息體,為BaseResponse,代碼如下:

package com.abc.questInvest.vo.common;

import lombok.Data;

/**
 * @className		: BaseResponse
 * @description		: 基本回應訊息體物件
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2021/05/31	1.0.0		sheng.zheng		初版
 * 2021/06/28	1.0.1		sheng.zheng		增加變更通知的附加資訊
 *
 */
@Data
public class BaseResponse<T> {
    //回應碼
    private int code;

    //回應訊息
    private String message;
        
    //回應物體資訊
    private T data;

    //分頁資訊
    private Page page;

    //附加通知資訊
    private Additional additional;
}

????BaseResponse類增加了Additional型別的additional屬性欄位,用于輸出附加資訊,

????Additional類的定義如下:

package com.abc.questInvest.vo.common;

import lombok.Data;

/**
 * @className		: Additional
 * @description		: 附加資訊
 * @summary		:
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2021/06/28	1.0.0		sheng.zheng		初版
 *
 */
@Data
public class Additional {
    //通知碼,附加資訊
    private int notifycode;

    //通知碼對應的訊息
    private String notification;
    
    //更新的token
    private String token;
    
    //更新的功能權限樹
    private String rights;

}

????附加資訊類Additional中,各屬性欄位的說明:

  • notifycode,為通知碼,即可對應通知訊息的型別,目前只有一種,可擴展,
  • notification,為通知碼對應的訊息,

????通知碼,在ExceptionCodes列舉檔案中定義:

    //變更通知資訊
    USER_RIGHTS_CHANGED(51, "message.USER_RIGHTS_CHANGED", "用戶權限發生變更"),
	;  //end enum

    ExceptionCodes(int code, String messageId, String message) {
        this.code = code;
        this.messageId = messageId;
        this.message = message;
    }
  • token,用于要求前端更新token,更新token的目的是確認前端已經收到權限變更通知,因為下次url請求將使用新的token,如果前端未收到或未處理,仍然用舊的token訪問,就要跳到登錄頁了,
  • rights,功能樹的字串輸出,是樹型結構的JSON字串,

3.6、AOP鑒權處理

?????AuthorizationAspect為鑒權認證的切面類,代碼如下:

package com.abc.questInvest.aop;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.abc.questInvest.common.constants.Constants;
import com.abc.questInvest.common.utils.Utility;
import com.abc.questInvest.dao.UserManDao;
import com.abc.questInvest.entity.FunctionInfo;
import com.abc.questInvest.entity.UserInfo;
import com.abc.questInvest.exception.BaseException;
import com.abc.questInvest.exception.ExceptionCodes;
import com.abc.questInvest.service.GlobalConfigService;
import com.abc.questInvest.service.LoginService;
import com.abc.questInvest.vo.TreeNode;
import com.abc.questInvest.vo.common.Additional;
import com.abc.questInvest.vo.common.BaseResponse;

/**
 * @className		: AuthorizationAspect
 * @description		: 介面訪問鑒權切面類
 * @summary		: 使用AOP,進行token認證以及用戶對介面的訪問權限鑒權
 * @history		:
 * ------------------------------------------------------------------------------
 * date			version		modifier		remarks                   
 * ------------------------------------------------------------------------------
 * 2021/06/06	1.0.0		sheng.zheng		初版
 * 2021/06/28	1.0.1		sheng.zheng		增加變更通知的處理,增加了afterReturning增強
 *
 */
@Aspect
@Component
@Order(2)
public class AuthorizationAspect {
	@Autowired
    private UserManDao userManDao;
	
	//設定切點
    @Pointcut("execution(public * com.abc.questInvest.controller..*.*(..))" +
    "&& !execution(public * com.abc.questInvest.controller.LoginController.*(..))" + 
    "&& !execution(public * com.abc.questInvest.controller.QuestInvestController.*(..))")    
    public void verify(){}
    
    @Before("verify()") 
    public void doVerify(){ 
		ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

		HttpServletRequest request=attributes.getRequest(); 
		
		// ================================================================================
		// token認證
		
		//從header中獲取token值
		String token = request.getHeader("Authorization");
		if (null == token || token.equals("")){ 
			//return;
			throw new BaseException(ExceptionCodes.TOKEN_IS_NULL); 
		} 
    	
		//從session中獲取token和過期時間
		String sessionToken = (String)request.getSession().getAttribute("token");
		
		//判斷session中是否有資訊,可能是非登錄用戶
		if (null == sessionToken || sessionToken.equals("")) {
			throw new BaseException(ExceptionCodes.TOKEN_WRONG);
		}
    	
		//比較token
		if(!token.equals(sessionToken)) {
			//如果請求頭中的token與存在session中token兩者不一致
			throw new BaseException(ExceptionCodes.TOKEN_WRONG);			
		}
		
		long expireTime = (long)request.getSession().getAttribute("expireTime");
		//檢查過期時間
		long time = System.currentTimeMillis();
		if (time > expireTime) {
			//如果token過期
			throw new BaseException(ExceptionCodes.TOKEN_EXPIRED);
		}else {
			//token未過期,更新過期時間
			long newExpiredTime = time + Constants.TOKEN_EXPIRE_TIME * 1000;
			request.getSession().setAttribute("expireTime", newExpiredTime);
		}
		
		// ============================================================================
		// 介面呼叫權限
		//獲取用戶ID
		Integer userId = (Integer)request.getSession().getAttribute("userId"); 
		//獲取全域變數
		ServletContext servletContext = request.getServletContext();
		GlobalConfigService globalConfigService = (GlobalConfigService)servletContext.getAttribute("GLOBAL_CONFIG_SERVICE");
		
		//===================變更通知處理開始==============================================
		//檢查有無變更通知資訊
		Integer changeNotifyInfo = globalConfigService.getChangeNotifyService().getChangeNotifyInfo(userId);
		//設定成員屬性為false
		boolean rightsChangedFlag = false;		
		if (changeNotifyInfo > 0) {
			//有通知資訊
			if ((changeNotifyInfo & 0x09) > 0) {
				//bit0:修改用戶的角色組合值,從而導致權限變更
				//bit3:用戶調整部門,從而導致資料權限變更
				//mask 0b1001 = 0x09 
				//都需要查詢用戶表,并更新資訊;合在一起查詢,
				UserInfo userInfo = userManDao.selectUserByKey(userId);
				//更新Session
		    	        request.getSession().setAttribute("roles", userInfo.getRoles());
		    	        request.getSession().setAttribute("deptId", userInfo.getDeptId());	
  		    	        if ((changeNotifyInfo & 0x01) > 0) {
  		    		        //權限變更標志置位
  		    		        rightsChangedFlag = true;
  		    	        }
			}else if((changeNotifyInfo & 0x02) > 0) {
				//bit1:修改角色的功能值,從而導致權限變更
	    		        //權限變更標志置位
	    		      rightsChangedFlag = true;
			}else if((changeNotifyInfo & 0x04) > 0) {
				//bit2:用戶禁用,從而導致權限變更
				//設定無效token,可阻止該用戶訪問系統
				request.getSession().setAttribute("token", "");
				//直接拋出例外,由前端顯示:Forbidden頁面
				throw new BaseException(ExceptionCodes.ACCESS_FORBIDDEN);
			}
			if (rightsChangedFlag == true) {
				//寫Session,用于將資訊傳遞到afterReturning方法中
				request.getSession().setAttribute("rightsChanged", 1);
			}
		}
		//===================變更通知處理結束==============================================
				
		//從session中獲取用戶權限值
		Integer roles = (Integer)request.getSession().getAttribute("roles");
		//獲取當前介面url值
		String servletPath = request.getServletPath();
				
		//獲取該角色對url的訪問權限
		Integer rights = globalConfigService.getRoleFuncRightsService().getRoleUrlRights(Utility.parseRoles(roles), servletPath);
		if (rights == 0) {
			//如果無權限訪問此介面,拋出例外,由前端顯示:Forbidden頁面
			throw new BaseException(ExceptionCodes.ACCESS_FORBIDDEN);
		}		
    }    
    
    @AfterReturning(value="https://www.cnblogs.com/alabo1999/archive/2021/06/29/verify()" ,returning="result")
    public void afterReturning(BaseResponse result) {
    	//限制必須是BaseResponse型別,其它型別的回傳值忽略
    	//獲取Session
        ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
    	Integer rightsChanged = (Integer)request.getSession().getAttribute("rightsChanged");
    	if (rightsChanged != null && rightsChanged == 1) {
    		//如果有用戶權限變更,通知前端來重繪該用戶的功能權限樹
    		//構造附加資訊
    		Additional additional = new Additional();
    		additional.setNotifycode(ExceptionCodes.USER_RIGHTS_CHANGED.getCode());
    		additional.setNotification(ExceptionCodes.USER_RIGHTS_CHANGED.getMessage());
    		//更新token
    		String loginName = (String)request.getSession().getAttribute("username");
    		String token = LoginService.generateToken(loginName);
    		additional.setToken(token);
    		//更新token,要求下次url訪問使用新的token
    		request.getSession().setAttribute("token", token);
    		//獲取用戶的功能權限樹
    		Integer roles = (Integer)request.getSession().getAttribute("roles");
    		ServletContext servletContext = request.getServletContext();
    		GlobalConfigService globalConfigService = (GlobalConfigService)servletContext.getAttribute("GLOBAL_CONFIG_SERVICE");
        	//獲取用戶權限的角色功能數
    		List<Integer> roleList = Utility.parseRoles(roles);
        	TreeNode<FunctionInfo> rolesFunctionTree = 
        			globalConfigService.getRoleFuncRightsService().
        			getRoleRights(roleList);
        	additional.setRights(rolesFunctionTree.toString());
    		//修改response資訊
        	result.setAdditional(additional);
    		//移除Session的rightsChanged項
    		request.getSession().removeAttribute("rightsChanged");
    	}
    }
}

????AuthorizationAspect類定義了切點verify(),@Before增強用于鑒權驗證,增加了對變更通知資訊的處理,并利用Session,用rightsChanged屬性欄位記錄需要通知前端的標志,在@AfterReturning后置增強中根據該屬性欄位的值,進行一步的處理,

????@Before增強的doVerify方法中,如果發現角色組合有改變,但仍有訪問此url權限時,會繼續后續處理,這樣不會中斷業務;如果沒有訪問此url權限,則回傳訪問受限例外資訊,由前端顯示訪問受限頁碼(類似403 Forbidden 頁碼),

????在后置增強@AfterReturning中,限定了回傳值型別,如果該請求回應的型別是BaseResponse型別,則修改reponse訊息體,附加通知資訊;如果不是,則不處理,會等待下一個url請求,直到回傳型別是BaseResponse型別,也可以采用自定義response的header的方式,這樣,就無需等待了,

????generateToken方法,是LoginService類的靜態方法,用于生成用戶token,

????至于Utility的parseRoles方法,是將bitmap編碼的roles決議為角色ID的串列,代碼如下:

	//========================= 權限組合值決議 ======================================    	
    /**
     * 
     * @methodName		: parseRoles
     * @description		: 決議角色組合值
     * @param roles		: 按位設定的角色組合值
     * @return			: 角色ID串列
     * @history			:
     * ------------------------------------------------------------------------------
     * date			version		modifier		remarks                   
     * ------------------------------------------------------------------------------
     * 2021/06/24	1.0.0		sheng.zheng		初版
     *
     */
    public static List<Integer> parseRoles(int roles){
    	List<Integer> roleList = new ArrayList<Integer>();

    	int newRoles = roles;
    	int bit0 = 0;
    	int roleId = 0;
    	for (int i = 0; i < 32; i++) {
    		//如果組合值的余位都為0,則跳出
    		if (newRoles == 0) {
    			break;
    		}
    		
    		//取得最后一位
    		bit0 = newRoles & 0x01;
    		if (bit0 == 1) {
    			//如果該位為1,左移i位
    			roleId = 1 << i;
    			roleList.add(roleId);
    		}
    		
    		//右移一位
    		newRoles = newRoles >> 1;
    	}
    	return roleList;
    }	

????getRoleRights方法,是角色功能權限服務類RoleFuncRightsService的方法,它提供了根據List型別的角色ID串列,快速獲取功能權限樹的功能,
????關于功能權限樹TreeNode型別,請參閱:《Java通用樹結構資料管理》,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288680.html

標籤:其他

上一篇:Leetcode No.66 Plus One(c++實作)

下一篇:Dubbo 的設計思想,真優秀!

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more