大型運輸行業實戰_day12_1_權限管理實現

1.業務分析

??? 權限說的是不同的用戶對同一個系統有不同訪問權限,其設計的本質是:給先給用戶分配好URL,然后在訪問的時候判斷該用戶是否有當前訪問的URL.

?2.實現

????? 2.1數據庫設計標準5表權限結構

?????

???? 2.2.sql語句實現,根據用戶id查詢該用戶所有的資源

???????

????? ? sql語句: ? SELECT ur.user_id, r.url FROM user_role ur LEFT JOIN role_resource rr ON (ur.role_id = rr.role_id) LEFT JOIN resource r ON (rr.resource_id = r.id) WHERE ur.user_id = 1

??

?? 2.3. 存放資源

??????? 在用戶登錄完成后,根據該用的id,查詢出所用資源,并放入緩存中.

??????? 登錄完成后放入緩存代碼:

1                   //密碼正確 登錄成功
2                      //存放資源信息
3                           //放memcache  key= 業務前綴_userId   value  list
4                  String key="resource_"+loginUserByName.getId();//準備key
5                  //調用 到查詢 到
6                  List<String> resource = resourceDao.getResource(loginUserByName.getId()); //根據用戶id獲取該用戶的所用資源
7                  DicMemcache.putResource(key,resource); //存放到memcache緩存中

?

???? 用到的resourceDao代碼:

????? 接口: List<String> getResource(Integer id);

???? mapper映射文件

 1 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 2         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <!--
 4 對應的接口地址: namespace="com.day02.sation.dao.ITicketDao"
 5 -->
 6 <mapper namespace="com.day02.sation.dao.IResourceDao">
 7 
 8     <select id="getResource" parameterType="int" resultType="String">
 9 SELECT  r.url FROM user_role ur LEFT JOIN role_resource rr ON (ur.role_id = rr.role_id)
10 LEFT JOIN resource r ON (rr.resource_id = r.id) WHERE ur.user_id = #{id}
11    </select>
12 </mapper>

?

?? 用到的DicMemcache存放方法與后面要用的獲取用戶資源方法

 1  /**
 2      * 存放用戶資源
 3      * @param key
 4      * @param value
 5      */
 6     public static void putResource(String key,Object value) {
 7         memcachedAccess.put(key,value);
 8     }
 9 
10     /**
11      * 獲取用戶資源
12      * @param key
13      */
14     public static List<String> getResource(String key) {
15         List<String> obj =(List<String>) memcachedAccess.getObj(key);
16         return obj;
17 
18     }

2.4使用aop實現權限判定

????? 權限判定管理類:

 1 package com.day02.sation.aop;
 2 
 3 import com.day02.sation.map.DicMemcache;
 4 import com.day02.sation.model.LoginUser;
 5 import org.slf4j.Logger;
 6 import org.slf4j.LoggerFactory;
 7 import org.springframework.web.context.request.RequestContextHolder;
 8 import org.springframework.web.context.request.ServletRequestAttributes;
 9 
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13 import java.io.IOException;
14 import java.util.List;
15 
16 /**
17  * Created by Administrator on 1/9.
18  */
19 public class resourceAop {
20     private static final Logger logger = LoggerFactory.getLogger(resourceAop.class);
21 
22     /**
23      * 方法執行前輸出
24      */
25     public void beforeResource() throws IOException {
26         logger.info("-----------beforeResource----------------");
27         ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
28         //獲取請求對象
29         HttpServletRequest request = requestAttributes.getRequest();
30         //獲取響應對象
31         HttpServletResponse response = requestAttributes.getResponse();
32         //查看是否已經登錄   做權限必須是在登錄的情況下
33         HttpSession session = request.getSession();
34         LoginUser loginUser = (LoginUser) session.getAttribute("LOGIN_IN_SESSION");
35         if (loginUser != null) {//說明已經登錄
36             //權限判定
37             //1.獲取 當前訪問的資源
38             String requestURI = request.getRequestURI();
39             System.out.println("requestURI=" + requestURI);
40             //2.獲取用戶擁有的資源   緩存中取
41             String key = "resource_" + loginUser.getId();//拼接權限資源key
42             List<String> resource = DicMemcache.getResource(key);//根據key獲取對應的資源列表
43             //3.比較是否有該資源
44             boolean isResource = false;//給定默認的值為沒有改權限
45             for (int i = 0; i < resource.size(); i++) {
46                 String valueResource = resource.get(i);
47                 if (requestURI.equals(valueResource)) {
48                     //擁有在資源的權限
49                     isResource = true;
50                     logger.info("有該權限:=" + valueResource);
51                     break;
52                 }
53             }
54             //沒有該資源權限
55             if (!isResource) {
56                 response.sendRedirect("/noResource.jsp");
57             }
58         } else {
59             //用戶沒用登錄不做權限判定
60         }
61     }
62 }

?

??? aop配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop
 6          http://www.springframework.org/schema/aop/spring-aop.xsd">
 7     <!--引入日志管理類-->
 8     <bean id="webAspectLog" class="com.day02.sation.aop.WebAspectLog"/>
 9     <!--引入權限管理類-->
10     <bean id="resourceAop" class="com.day02.sation.aop.resourceAop"/>
11     <!--配置切面-->
12     <aop:config>
13         <!--日志aop-->
14         <aop:aspect ref="webAspectLog">
15             <aop:pointcut id="pointcut" expression="execution(* com.day02.sation.controller.*Controller.*(..))"/>
16             <aop:before method="beforeLog" pointcut-ref="pointcut"/>
17            <!-- 注意如果要獲取執行后的結果 必須配置參數 returning="對象為afterLog方法的參數對象名稱"-->
18             <aop:after-returning method="afterLog" pointcut-ref="pointcut" returning="returnObj"/>
19         </aop:aspect>
20         <!--權限aop-->
21         <aop:aspect ref="resourceAop">
22             <aop:pointcut id="pointcut" expression="execution(* com.day02.sation.controller.*Controller.*(..))"/>
23             <aop:before method="beforeResource" pointcut-ref="pointcut"/>
24         </aop:aspect>
25     </aop:config>
26 </beans>

?重啟項目權限管理搞定,權限的資源配置角色分配等工作請在站務管理系統中完成!

轉載于:https://www.cnblogs.com/newAndHui/p/8258918.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390379.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390379.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390379.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

aws python庫_如何使用Python,AWS和IEX Cloud創建自動更新股市數據的Excel電子表格

aws python庫Many Python developers in the financial world are tasked with creating Excel documents for analysis by non-technical users.金融界的許多Python開發人員的任務是創建Excel文檔&#xff0c;以供非技術用戶進行分析。 This is actually a lot harder than i…

37)智能指針(就是自動delete空間)

1&#xff09;問題引入&#xff1a; 在java或者在C中&#xff0c;一旦你new一個東西&#xff0c;那么必然有一個delete與之對應&#xff0c;比如&#xff1a; 1 int main&#xff08;&#xff09;2 {3 int* p new int&#xff08;&#xff09;&#xff1b;4 5 *…

linux 安裝maven

2019獨角獸企業重金招聘Python工程師標準>>> 目錄:/usr/local/maven 1.下載 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz 2.解壓 tar -zxvf apache-maven-3.5.3-bin.tar.gz 3.配置 vi /etc/profile #講下面…

自由開發者怎么生存_如何作為自由開發者生存

自由開發者怎么生存It’s been 8 weeks since we started experiencing the dramatic impact of the COVID-19 pandemic. In that time, we’ve all borne witness to how this virus can impact our families, our communities, and our livelihood. 自我們開始體驗COVID-19大…

UUID生成字符串

在向數據庫插入新數據時&#xff0c;可能需要插入字符串形式的ID&#xff0c;這時使用UUID可以生成隨機字符串&#xff1a; String str UUID.randomUUID().toString(); 轉載于:https://www.cnblogs.com/suhfj-825/p/8260861.html

如何在React Native中使用react-navigation 5處理導航

React-navigation is the navigation library that comes to my mind when we talk about navigation in React Native. 當我們談論React Native中的導航時&#xff0c;React-navigation是我想到的導航庫。 Im a big fan of this library and its always the first solution I…

flask內置session原理

內置session原理 請求到來 當請求進來之后&#xff0c;先執行Flask對象的 __call__ 方法 def wsgi_app(self, environ, start_response):# 獲取請求相關數據&#xff0c;并進行封裝和加工ctx self.request_context(environ)# 將請求消息推送到堆棧中&#xff0c;并執行 open_s…

指針3

#include <stdio.h>/* 2018-05-28 如何通過被調函數修改主調函數普通變量的值1&#xff0c;實參必須為該普通變量的地址2,形參必須為指針變量3&#xff0c;在背調函數中通過*形參名 。。。。。的方式就可以修改主調函數相關變量的值*/f(int *i,int *j) {*i 4;*j 5;ret…

面試系統設計_系統設計面試問題–您應該知道的概念

面試系統設計You may have heard the terms "Architecture" or "System Design." These come up a lot during developer job interviews – especially at big tech companies.您可能已經聽說過“架構”或“系統設計”這兩個術語。 在開發人員工作面試中&…

8597 石子劃分問題 dpdp,只考慮第一次即可

8597 石子劃分問題 時間限制:500MS 內存限制:1000K提交次數:155 通過次數:53 題型: 編程題 語言: G;GCC;VC Description 給定n個石子&#xff0c;其重量分別為a1,a2,a3,...,an。 要求將其劃分為m份&#xff0c;每一份的劃分費用定義為這份石子中最大重量與最小重量差的平方。…

文章中嵌入代碼塊_如何在您的文章中嵌入多項選擇測驗問題

文章中嵌入代碼塊In my experience, supplementing study with practical exercises greatly improves my understanding of a topic. This is especially true when I can test my knowledge as I go and receive instant feedback for each question.以我的經驗&#xff0c;通…

mysql免安裝版配置

1.官網下載https://dev.mysql.com/downloads/mysql/ 2.將下載好的壓縮包mysql-5.7.20-winx64.zip解壓。 3.mysql解壓后&#xff0c;設置.ini文件&#xff0c;在加壓后的路徑中加一個my.ini文件 配置如下內容&#xff1a; # 設置mysql客戶端默認字符集 default-character-setutf…

各種IE(IE6-IE10)兼容問題一行代碼搞定

x-ua-compatible 用來指定IE瀏覽器解析編譯頁面的model x-ua-compatible 頭標簽大小寫不敏感&#xff0c;必須用在 head 中&#xff0c;必須在除 title 外的其他 meta 之前使用。 1、使用一行代碼來指定瀏覽器使用特定的文檔模式。 <meta http-equiv"x-ua-compatible&q…

802. 找到最終的安全狀態

在有向圖中&#xff0c;以某個節點為起始節點&#xff0c;從該點出發&#xff0c;每一步沿著圖中的一條有向邊行走。如果到達的節點是終點&#xff08;即它沒有連出的有向邊&#xff09;&#xff0c;則停止。 對于一個起始節點&#xff0c;如果從該節點出發&#xff0c;無論每…

元類型與類型的區別

元類型是指所有類型的類型。 元類型只能類型出現在類型標示位&#xff1b; 類型即能作為類型存在&#xff0c;出現在類型標示位&#xff1b; 也能作為變量存在&#xff0c;出現在元類型的變量位。 http://www.swift51.com/swift2.0/chapter3/03_Types.html#type_inheritance_cl…

css 動畫使用_如何在CSS中使用動畫

css 動畫使用使用CSS動畫 (Using CSS Animations) CSS animations add beauty to the webpages and make transitions from one CSS style to the other beautiful.CSS動畫可以使網頁更加美觀&#xff0c;并可以從一種CSS樣式過渡到另一種CSS樣式。 To create a CSS animation…

第01章—快速構建

spring boot 系列學習記錄&#xff1a;http://www.cnblogs.com/jinxiaohang/p/8111057.html 碼云源碼地址&#xff1a;https://gitee.com/jinxiaohang/springboot 一、Spring Initializr 使用教程 &#xff08;IntelliJ IDEA&#xff09; 具體步驟&#xff1a; 1、打開IDEA &am…

看板和scrum_看板VS Scrum-如何變得敏捷

看板和scrumScrum and Kanban are the two most popular project management techniques today in business. As a developer, I think its important to understand these processes as you will likely be heavily involved in them if you are part of a team. By understan…

JS之Promise

開胃菜&#xff0c;先做如下思考&#xff1a; Promise 有幾種狀態&#xff1f;Promise 狀態之間可以轉化嗎&#xff1f;Promise 中的異常可以被 try...catch 捕獲嗎&#xff1f;Promise前世 callback hell 大家都知道JS是異步操作&#xff08;執行&#xff09;的&#xff0c;在…

魚眼鏡頭的distortion校正【matlab】

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 作者&#xff1a;WWC %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 功能&#xff1a;畸變矯正 clc; clear; close all; %% 讀取圖像 Aimread(D:\文件及下載相關\圖片\distortion2.jpg)…