业务分析之--权限管理
生活随笔
收集整理的這篇文章主要介紹了
业务分析之--权限管理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.業(yè)務(wù)分析
??? 權(quán)限說的是不同的用戶對同一個系統(tǒng)有不同訪問權(quán)限,其設(shè)計的本質(zhì)是:給先給用戶分配好URL,然后在訪問的時候判斷該用戶是否有當前訪問的URL.
?2.實現(xiàn)
????? 2.1數(shù)據(jù)庫設(shè)計標準5表權(quán)限結(jié)構(gòu)
??????
???? 2.2.sql語句實現(xiàn),根據(jù)用戶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. 存放資源
??????? 在用戶登錄完成后,根據(jù)該用的id,查詢出所用資源,并放入緩存中.
??????? 登錄完成后放入緩存代碼:
1 //密碼正確 登錄成功 2 //存放資源信息 3 //放memcache key= 業(yè)務(wù)前綴_userId value list 4 String key="resource_"+loginUserByName.getId();//準備key 5 //調(diào)用 到查詢 到 6 List<String> resource = resourceDao.getResource(loginUserByName.getId()); //根據(jù)用戶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 對應(yīng)的接口地址: 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實現(xiàn)權(quán)限判定
????? 權(quán)限判定管理類:
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 * 方法執(zhí)行前輸出 24 */ 25 public void beforeResource() throws IOException { 26 logger.info("-----------beforeResource----------------"); 27 ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 28 //獲取請求對象 29 HttpServletRequest request = requestAttributes.getRequest(); 30 //獲取響應(yīng)對象 31 HttpServletResponse response = requestAttributes.getResponse(); 32 //查看是否已經(jīng)登錄 做權(quán)限必須是在登錄的情況下 33 HttpSession session = request.getSession(); 34 LoginUser loginUser = (LoginUser) session.getAttribute("LOGIN_IN_SESSION"); 35 if (loginUser != null) {//說明已經(jīng)登錄 36 //權(quán)限判定 37 //1.獲取 當前訪問的資源 38 String requestURI = request.getRequestURI(); 39 System.out.println("requestURI=" + requestURI); 40 //2.獲取用戶擁有的資源 緩存中取 41 String key = "resource_" + loginUser.getId();//拼接權(quán)限資源key 42 List<String> resource = DicMemcache.getResource(key);//根據(jù)key獲取對應(yīng)的資源列表 43 //3.比較是否有該資源 44 boolean isResource = false;//給定默認的值為沒有改權(quán)限 45 for (int i = 0; i < resource.size(); i++) { 46 String valueResource = resource.get(i); 47 if (requestURI.equals(valueResource)) { 48 //擁有在資源的權(quán)限 49 isResource = true; 50 logger.info("有該權(quán)限:=" + valueResource); 51 break; 52 } 53 } 54 //沒有該資源權(quán)限 55 if (!isResource) { 56 response.sendRedirect("/noResource.jsp"); 57 } 58 } else { 59 //用戶沒用登錄不做權(quán)限判定 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 <!--引入權(quán)限管理類--> 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 <!-- 注意如果要獲取執(zhí)行后的結(jié)果 必須配置參數(shù) returning="對象為afterLog方法的參數(shù)對象名稱"--> 18 <aop:after-returning method="afterLog" pointcut-ref="pointcut" returning="returnObj"/> 19 </aop:aspect> 20 <!--權(quán)限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>轉(zhuǎn)載于:https://www.cnblogs.com/dw3306/p/9360372.html
總結(jié)
以上是生活随笔為你收集整理的业务分析之--权限管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OCP换题库了,052新加的考题及答案整
- 下一篇: QPS/TPS/并发量/系统吞吐量概念和