您的位置:首頁技術文章
文章詳情頁

關于Spring Boot動態權限變更問題的實現方案

瀏覽:2日期:2023-07-05 09:24:02
1、前言

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

關于Spring Boot動態權限變更問題的實現方案

​  現在,如果管理員修改了用戶的角色,或修改了角色的權限,都會導致用戶權限發生變化,此時如何實現動態權限變更,使得前端能夠更新用戶的權限樹,后端訪問鑒權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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/281.0.0sheng.zheng初版 * */public interface ChangeNotifyService {/** * * @methodName: getChangeNotifyInfo * @description: 獲取指定用戶ID的變更通知信息 * @param userId: 用戶ID * @return: 返回0表示無變更通知信息,其它值按照bitmap編碼。目前定義如下: * bit0:: 修改用戶的角色組合值,從而導致權限變更; * bit1:: 修改角色的功能項,從而導致權限變更; * bit2:: 用戶禁用,從而導致權限變更; * bit3:: 用戶調整部門,從而導致數據權限變更; * @history: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/281.0.0sheng.zheng初版 * */public Integer getChangeNotifyInfo(Integer userId);/** * * @methodName: setChangeNotifyInfo * @description: 設置變更通知信息 * @param userId: 用戶ID * @param changeNotifyInfo: 變更通知值 * bit0:: 修改用戶的角色組合值,從而導致權限變更; * bit1:: 修改角色的功能項,從而導致權限變更; * bit2:: 用戶禁用,從而導致權限變更; * bit3:: 用戶調整部門,從而導致數據權限變更; * @history: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/281.0.0sheng.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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/281.0.0sheng.zheng初版 * */@Servicepublic 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/281.0.0sheng.zheng初版 * */@Overridepublic 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/281.0.0sheng.zheng初版 * */@Overridepublic 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/021.0.0sheng.zheng初版 * */public interface GlobalConfigService {/** * * @methodName: loadData * @description: 加載數據 * @return: 成功返回true,否則返回false * @history: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/021.0.0sheng.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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/021.0.0sheng.zheng初版 * */@Servicepublic class GlobalConfigServiceImpl implements GlobalConfigService{//ID編碼配置表數據服務@Autowiredprivate TableCodeConfigService tableCodeConfigService;//系統參數表數據服務@Autowiredprivate SysParameterService sysParameterService;//功能樹表數據服務@Autowiredprivate FunctionTreeService functionTreeService;//角色權限表數據服務@Autowiredprivate RoleFuncRightsService roleFuncRightsService;//變更通知服務@Autowiredprivate ChangeNotifyService changeNotifyService;/** * * @methodName: loadData * @description: 加載數據 * @return: 成功返回true,否則返回false * @history: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/021.0.0sheng.zheng初版 * */@Overridepublic 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對象@Overridepublic TableCodeConfigService getTableCodeConfigService() {return tableCodeConfigService;}//獲取SysParameterService對象@Overridepublic SysParameterService getSysParameterService() {return sysParameterService;}//獲取FunctionTreeService對象@Overridepublic FunctionTreeService getFunctionTreeService() {return functionTreeService;}//獲取RoleFuncRightsService對象@Overridepublic RoleFuncRightsService getRoleFuncRightsService() {return roleFuncRightsService;}//獲取ChangeNotifyService對象@Overridepublic 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: 應用偵聽器 * */@Componentpublic 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/081.0.0sheng.zheng初版 * 2021/06/281.0.1sheng.zheng增加變更通知的處理 * */@Overridepublic 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()) {//角色組合有變化,bit0changeFlag |= 0x01;}}if (userInfo.getDeptId() != null) {if (oldUserInfo.getDeptId() != userInfo.getDeptId()) {//部門ID有變化,bit3changeFlag |= 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/081.0.0sheng.zheng初版 * 2021/06/281.0.1sheng.zheng增加變更通知的處理 * */@Overridepublic 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');//禁用用戶:bit2globalConfigService.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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/05/311.0.0sheng.zheng初版 * 2021/06/281.0.1sheng.zheng增加變更通知的附加信息 * */@Datapublic 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/281.0.0sheng.zheng初版 * */@Datapublic 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks * ------------------------------------------------------------------------------ * 2021/06/061.0.0sheng.zheng初版 * 2021/06/281.0.1sheng.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);} //比較tokenif(!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);}// ============================================================================// 接口調用權限//獲取用戶IDInteger userId = (Integer)request.getSession().getAttribute('userId'); //獲取全局變量ServletContext servletContext = request.getServletContext();GlobalConfigService globalConfigService = (GlobalConfigService)servletContext.getAttribute('GLOBAL_CONFIG_SERVICE');//===================變更通知處理開始==============================================//檢查有無變更通知信息Integer changeNotifyInfo = globalConfigService.getChangeNotifyService().getChangeNotifyInfo(userId);//通知前端權限變更的標記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='verify()' ,returning='result') public void afterReturning(BaseResponse result) { //限制必須是BaseResponse類型,其它類型的返回值忽略 //獲取SessionServletRequestAttributes 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: * ------------------------------------------------------------------------------ * dateversionmodifierremarks* ------------------------------------------------------------------------------ * 2021/06/241.0.0sheng.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通用樹結構數據管理》。

到此這篇關于Spring Boot動態權限變更實現的整體方案的文章就介紹到這了,更多相關Spring Boot動態權限內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
国产综合久久一区二区三区