大型运输行业实战_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.mzph.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)…