日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

业务分析之--权限管理

發布時間:2024/9/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 业务分析之--权限管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 key4 * @param value5 */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/beans5 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop6 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/dw3306/p/9360372.html

總結

以上是生活随笔為你收集整理的业务分析之--权限管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。