最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot中動(dòng)態(tài)權(quán)限實(shí)時(shí)管理的實(shí)現(xiàn)詳解

 更新時(shí)間:2024年10月31日 09:28:33   作者:一只愛擼貓的程序猿  
這篇文章主要為大家詳細(xì)介紹了如何簡(jiǎn)單實(shí)現(xiàn)一個(gè)在springboot中動(dòng)態(tài)權(quán)限的實(shí)時(shí)管理,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

以下這個(gè)案例將涉及到一個(gè)權(quán)限管理場(chǎng)景,假設(shè)我們有一個(gè)內(nèi)部管理系統(tǒng),管理員可以動(dòng)態(tài)變更用戶的權(quán)限。我們將演示在用戶訪問(wèn)某個(gè)資源時(shí),權(quán)限發(fā)生變更后,系統(tǒng)自動(dòng)響應(yīng)并及時(shí)反饋權(quán)限的變化。

場(chǎng)景描述

在這個(gè)場(chǎng)景中,用戶有一個(gè)可以管理資源的頁(yè)面,比如一個(gè)“訂單管理系統(tǒng)”,用戶可以創(chuàng)建、編輯、刪除訂單。系統(tǒng)管理員可以動(dòng)態(tài)更改用戶的權(quán)限,比如撤銷其“刪除訂單”的權(quán)限。當(dāng)管理員更改用戶權(quán)限后,前端頁(yè)面會(huì)監(jiān)聽這個(gè)權(quán)限的變化。通過(guò)瀏覽器端訂閱消息隊(duì)列(MQ)的方式,實(shí)時(shí)地收到權(quán)限變化通知,并根據(jù)權(quán)限變動(dòng)做出相應(yīng)操作。

如果用戶的權(quán)限被撤銷,前端會(huì)立即更新顯示。若用戶嘗試進(jìn)行不允許的操作(例如刪除操作),系統(tǒng)會(huì)彈出權(quán)限不足的提示。

技術(shù)要點(diǎn)

Spring Boot作為后端框架。

RabbitMQ作為消息隊(duì)列,實(shí)現(xiàn)權(quán)限變更的通知。

前端使用WebSocket訂閱RabbitMQ隊(duì)列,監(jiān)聽權(quán)限變更。

使用Spring Security進(jìn)行權(quán)限控制。

復(fù)雜場(chǎng)景:權(quán)限動(dòng)態(tài)變更后,實(shí)時(shí)限制用戶操作(刪除按鈕隱藏,彈出權(quán)限變更通知)。

解決方案

Spring Boot后端處理權(quán)限變更: 當(dāng)管理員修改了某個(gè)用戶的權(quán)限,系統(tǒng)會(huì)將權(quán)限變更的消息發(fā)送到RabbitMQ隊(duì)列,前端會(huì)通過(guò)WebSocket接收并實(shí)時(shí)更新頁(yè)面。

前端處理權(quán)限變更: 用戶頁(yè)面通過(guò)WebSocket連接到后端,監(jiān)聽權(quán)限的變更。一旦收到權(quán)限變更消息,前端立即更新用戶界面。

MQ訂閱與前端動(dòng)態(tài)變動(dòng): WebSocket與RabbitMQ的結(jié)合使得權(quán)限變更可以及時(shí)反映在前端,無(wú)需手動(dòng)刷新。若用戶嘗試操作失去權(quán)限的功能,會(huì)出現(xiàn)提示框告知用戶權(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ì)列配置類

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配置類

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)聽權(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)聽

假設(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)撤銷刪除權(quán)限
    },
    deleteOrder() {
      if (this.canDelete) {
        // 發(fā)送刪除請(qǐng)求
      } else {
        alert('You do not have permission to delete this order.');
      }
    }
  }
};
</script>

運(yùn)行流程

用戶登錄系統(tǒng),進(jìn)入“訂單管理”頁(yè)面。

管理員在后臺(tái)更改用戶權(quán)限,撤銷其“刪除訂單”的權(quán)限。

后端通過(guò)RabbitMQ發(fā)送權(quán)限變更的消息,消息通過(guò)WebSocket通知前端用戶。

前端頁(yè)面接收到消息后,自動(dòng)禁用“刪除訂單”按鈕,用戶再也無(wú)法點(diǎn)擊該按鈕。

若用戶試圖刪除訂單,系統(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)聽權(quán)限變更并及時(shí)在前端做出反饋,可以確保用戶操作權(quán)限的準(zhǔn)確性,同時(shí)提高用戶體驗(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)文章

  • SpringBoot整合RestTemplate用法的實(shí)現(xiàn)

    SpringBoot整合RestTemplate用法的實(shí)現(xiàn)

    本篇主要介紹了RestTemplate中的GET,POST,PUT,DELETE、文件上傳和文件下載6大常用的功能,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-08-08
  • 基于Spring Security的動(dòng)態(tài)權(quán)限系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)

    基于Spring Security的動(dòng)態(tài)權(quán)限系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)

    本文介紹一個(gè)基于Spring Boot 2.7.18和SpringSecurity實(shí)現(xiàn)的權(quán)限系統(tǒng),支持接口級(jí)權(quán)限控制,支持權(quán)限,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-07-07
  • 最新評(píng)論

    石渠县| 中卫市| 如东县| 九龙城区| 江达县| 南汇区| 垫江县| 嘉峪关市| 双牌县| 高雄市| 民县| 淳安县| 民和| 清镇市| 保靖县| 客服| 河间市| 瑞昌市| 上林县| 梅河口市| 调兵山市| 商城县| 清水县| 高台县| 平乡县| 嘉鱼县| 龙井市| 太湖县| 苍山县| 阿荣旗| 扶沟县| 寻乌县| 安远县| 塔城市| 土默特右旗| 砚山县| 洛阳市| 郎溪县| 闽侯县| 侯马市| 石楼县|