springboot中動(dòng)態(tài)權(quán)限實(shí)時(shí)管理的實(shí)現(xiàn)詳解
以下這個(gè)案例將涉及到一個(gè)權(quán)限管理場(chǎng)景,假設(shè)我們有一個(gè)內(nèi)部管理系統(tǒng),管理員可以動(dòng)態(tài)變更用戶(hù)的權(quán)限。我們將演示在用戶(hù)訪(fǎng)問(wèn)某個(gè)資源時(shí),權(quán)限發(fā)生變更后,系統(tǒng)自動(dòng)響應(yīng)并及時(shí)反饋權(quán)限的變化。
場(chǎng)景描述
在這個(gè)場(chǎng)景中,用戶(hù)有一個(gè)可以管理資源的頁(yè)面,比如一個(gè)“訂單管理系統(tǒng)”,用戶(hù)可以創(chuàng)建、編輯、刪除訂單。系統(tǒng)管理員可以動(dòng)態(tài)更改用戶(hù)的權(quán)限,比如撤銷(xiāo)其“刪除訂單”的權(quán)限。當(dāng)管理員更改用戶(hù)權(quán)限后,前端頁(yè)面會(huì)監(jiān)聽(tīng)這個(gè)權(quán)限的變化。通過(guò)瀏覽器端訂閱消息隊(duì)列(MQ)的方式,實(shí)時(shí)地收到權(quán)限變化通知,并根據(jù)權(quán)限變動(dòng)做出相應(yīng)操作。
如果用戶(hù)的權(quán)限被撤銷(xiāo),前端會(huì)立即更新顯示。若用戶(hù)嘗試進(jìn)行不允許的操作(例如刪除操作),系統(tǒng)會(huì)彈出權(quán)限不足的提示。
技術(shù)要點(diǎn)
Spring Boot作為后端框架。
RabbitMQ作為消息隊(duì)列,實(shí)現(xiàn)權(quán)限變更的通知。
前端使用WebSocket訂閱RabbitMQ隊(duì)列,監(jiān)聽(tīng)權(quán)限變更。
使用Spring Security進(jìn)行權(quán)限控制。
復(fù)雜場(chǎng)景:權(quán)限動(dòng)態(tài)變更后,實(shí)時(shí)限制用戶(hù)操作(刪除按鈕隱藏,彈出權(quán)限變更通知)。
解決方案
Spring Boot后端處理權(quán)限變更: 當(dāng)管理員修改了某個(gè)用戶(hù)的權(quán)限,系統(tǒng)會(huì)將權(quán)限變更的消息發(fā)送到RabbitMQ隊(duì)列,前端會(huì)通過(guò)WebSocket接收并實(shí)時(shí)更新頁(yè)面。
前端處理權(quán)限變更: 用戶(hù)頁(yè)面通過(guò)WebSocket連接到后端,監(jiān)聽(tīng)權(quán)限的變更。一旦收到權(quán)限變更消息,前端立即更新用戶(hù)界面。
MQ訂閱與前端動(dòng)態(tài)變動(dòng): WebSocket與RabbitMQ的結(jié)合使得權(quán)限變更可以及時(shí)反映在前端,無(wú)需手動(dòng)刷新。若用戶(hù)嘗試操作失去權(quán)限的功能,會(huì)出現(xiàn)提示框告知用戶(hù)權(quán)限不足。
實(shí)際代碼實(shí)現(xiàn)
1. Spring Boot配置
配置RabbitMQ
在Spring Boot中配置RabbitMQ的連接和隊(duì)列。
application.yml:
spring: rabbitmq: host: localhost port: 5672 username: guest password: guest
創(chuàng)建消息隊(duì)列配置類(lèi)
import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { public static final String PERMISSION_CHANGE_QUEUE = "permission_change_queue"; @Bean public Queue permissionChangeQueue() { return new Queue(PERMISSION_CHANGE_QUEUE); } }
權(quán)限變更服務(wù)
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PermissionChangeService { @Autowired private RabbitTemplate rabbitTemplate; public void notifyPermissionChange(String userId) { // 發(fā)送權(quán)限變更消息到隊(duì)列 rabbitTemplate.convertAndSend(RabbitConfig.PERMISSION_CHANGE_QUEUE, userId); } }
模擬權(quán)限變更控制器
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.RestController; @RestController public class AdminController { @Autowired private PermissionChangeService permissionChangeService; @PreAuthorize("hasRole('ADMIN')") @PostMapping("/changePermission") public String changePermission(@RequestParam String userId, @RequestParam String newPermission) { // 修改數(shù)據(jù)庫(kù)中的權(quán)限邏輯(省略) // 通知權(quán)限變更 permissionChangeService.notifyPermissionChange(userId); return "Permissions updated for user: " + userId; } }
WebSocket配置類(lèi)
import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS(); } }
WebSocket消息發(fā)送服務(wù)
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Service; @Service public class WebSocketNotificationService { private final SimpMessagingTemplate messagingTemplate; public WebSocketNotificationService(SimpMessagingTemplate messagingTemplate) { this.messagingTemplate = messagingTemplate; } public void notifyUser(String userId, String message) { messagingTemplate.convertAndSend("/topic/permission/" + userId, message); } }
消費(fèi)者監(jiān)聽(tīng)權(quán)限變更消息
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PermissionChangeListener { @Autowired private WebSocketNotificationService webSocketNotificationService; @RabbitListener(queues = RabbitConfig.PERMISSION_CHANGE_QUEUE) public void handlePermissionChange(String userId) { // 通知前端權(quán)限變更 webSocketNotificationService.notifyUser(userId, "Your permissions have been changed. Please refresh."); } }
2. 前端代碼
WebSocket連接和權(quán)限監(jiān)聽(tīng)
假設(shè)使用Vue.js前端框架。
WebSocket.js:
import SockJS from 'sockjs-client'; import Stomp from 'stompjs'; let stompClient = null; export function connect(userId, onPermissionChange) { const socket = new SockJS('/ws'); stompClient = Stomp.over(socket); stompClient.connect({}, function () { stompClient.subscribe('/topic/permission/' + userId, function (message) { onPermissionChange(JSON.parse(message.body)); }); }); } export function disconnect() { if (stompClient !== null) { stompClient.disconnect(); } }
Vue組件中的使用
<template> <div> <h1>Order Management</h1> <button v-if="canDelete" @click="deleteOrder">Delete Order</button> <p v-if="!canDelete" style="color:red">You do not have permission to delete orders</p> </div> </template> <script> import { connect, disconnect } from '@/websocket'; export default { data() { return { canDelete: true }; }, created() { const userId = this.$store.state.user.id; connect(userId, this.handlePermissionChange); }, beforeDestroy() { disconnect(); }, methods: { handlePermissionChange(message) { alert(message); this.canDelete = false; // 動(dòng)態(tài)撤銷(xiāo)刪除權(quán)限 }, deleteOrder() { if (this.canDelete) { // 發(fā)送刪除請(qǐng)求 } else { alert('You do not have permission to delete this order.'); } } } }; </script>
運(yùn)行流程
用戶(hù)登錄系統(tǒng),進(jìn)入“訂單管理”頁(yè)面。
管理員在后臺(tái)更改用戶(hù)權(quán)限,撤銷(xiāo)其“刪除訂單”的權(quán)限。
后端通過(guò)RabbitMQ發(fā)送權(quán)限變更的消息,消息通過(guò)WebSocket通知前端用戶(hù)。
前端頁(yè)面接收到消息后,自動(dòng)禁用“刪除訂單”按鈕,用戶(hù)再也無(wú)法點(diǎn)擊該按鈕。
若用戶(hù)試圖刪除訂單,系統(tǒng)會(huì)彈出權(quán)限不足的提示。
總結(jié)
這個(gè)案例展示了如何使用Spring Boot結(jié)合RabbitMQ和WebSocket處理權(quán)限動(dòng)態(tài)變更的場(chǎng)景。通過(guò)實(shí)時(shí)監(jiān)聽(tīng)權(quán)限變更并及時(shí)在前端做出反饋,可以確保用戶(hù)操作權(quán)限的準(zhǔn)確性,同時(shí)提高用戶(hù)體驗(yàn)。
到此這篇關(guān)于springboot中動(dòng)態(tài)權(quán)限實(shí)時(shí)管理的實(shí)現(xiàn)詳解的文章就介紹到這了,更多相關(guān)springboot動(dòng)態(tài)權(quán)限實(shí)時(shí)管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java應(yīng)用程序添加或刪除 PDF 中的附件
當(dāng)我們?cè)谥谱鱌DF文件或者PPT演示文稿的時(shí)候,為了讓自己的文件更全面詳細(xì),就會(huì)在文件中添加附件,那么如何添加或刪除PDF中的附件呢,今天通過(guò)本文給大家詳細(xì)講解,需要的朋友參考下吧2023-01-01使用IDEA創(chuàng)建Java Web項(xiàng)目并部署訪(fǎng)問(wèn)的圖文教程
本文通過(guò)圖文并茂的形式給大家介紹了使用IDEA創(chuàng)建Java Web項(xiàng)目并部署訪(fǎng)問(wèn)的教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Mybatis一對(duì)多與多對(duì)一查詢(xún)處理詳解
這篇文章主要給大家介紹了關(guān)于Mybatis一對(duì)多與多對(duì)一查詢(xún)處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03通過(guò)實(shí)例了解JavaBean開(kāi)發(fā)及使用過(guò)程解析
這篇文章主要介紹了通過(guò)實(shí)例了解JavaBean開(kāi)發(fā)及使用過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Spring實(shí)戰(zhàn)之使用c:命名空間簡(jiǎn)化配置操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用c:命名空間簡(jiǎn)化配置操作,結(jié)合實(shí)例形式詳細(xì)分析了Spring使用c:命名空間簡(jiǎn)化配置的相關(guān)接口與配置操作技巧,需要的朋友可以參考下2019-12-12JAVA多線(xiàn)程之中斷機(jī)制及處理中斷的方法
這篇文章主要記錄使用 interrupt() 方法中斷線(xiàn)程,以及如何對(duì)InterruptedException進(jìn)行處理,感覺(jué)對(duì)InterruptedException異常進(jìn)行處理是一件謹(jǐn)慎且有技巧的活兒,需要的朋友可以參考下2023-02-02