權限說的是不一樣的用戶對同一個系統有不一樣訪問權限,其設計的本質是:給先給用戶分配好URL,而後在訪問的時候判斷該用戶是否有當前訪問的URL.java
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 = 1web
在用戶登陸完成後,根據該用的id,查詢出所用資源,並放入緩存中.spring
登陸完成後放入緩存代碼:sql
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);express
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存放方法與後面要用的獲取用戶資源方法session
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 }
權限斷定管理類:mybatis
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配置文件app
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>
重啓項目權限管理搞定,權限的資源配置角色分配等工做請在站務管理系統中完成!