参考博文

https://blog.csdn.net/u011277123/article/details/68940939

1.Listener加载权限信息

2.interceptor验证权限

测试代码

springmvc-servlet.xml

<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/login/**"/>
<mvc:exclude-mapping path="/**/*.css"/>
<mvc:exclude-mapping path="/**/*.js"/>
<mvc:exclude-mapping path="/**/*.js"/>
<mvc:exclude-mapping path="/**/*.png"/>
<mvc:exclude-mapping path="/**/*.gif"/>
<mvc:exclude-mapping path="/**/*.jpg"/>
<mvc:exclude-mapping path="/**/*.jpeg"/>
<bean class="*****.UserControllerInterceptor"></bean>
</mvc:interceptor>

web.xml

<listener-class>
****.DictionaryCacheListener
</listener-class>

UserControllerInterceptor.java

public class UserControllerInterceptor extends HandlerInterceptorAdapter {

    @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession();
String contextPath = request.getContextPath();
User user = (User)session.getAttribute("user");
if (user == null) {
response.sendRedirect(contextPath+"/login/index");
return false;
}
if ("post".equals(request.getMethod().toLowerCase())) {
System.out.println("preHandle----------------post");
} List<Integer> permissions = user.getPermissions(); String url = request.getRequestURI();
int pos = url.indexOf("?");
String matchUrl = url;
if (pos != -1) {
matchUrl = matchUrl.substring(0, pos);
}
Map<String,Set<Integer>> urlMap= (Map<String,Set<Integer>>)request.getServletContext().getAttribute("urlsMap");
Set<Integer> permissionSet = urlMap.get(matchUrl);
if (permissionSet == null || permissionSet.size() < 1) {
// 无需权限,直接通过
return true;
} else {
for(Integer per : permissions) {
if (permissionSet.contains(per)) {
// 匹配成功
return true;
}
}
// 提示权限不足
// 非ajax提交
if (request.getHeader("x-requested-with") == null) {
response.sendRedirect(contextPath+"/login/unauthorized");
// ajax提交
} else {
response.getWriter().write("{\"msg\":\"noPadding\"}");
}
return false;
}
}
}

  DictionaryCacheListener.java

package com.ryuantech.mp.controll;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; public class DictionaryCacheListener implements javax.servlet.ServletContextListener { @Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("++++++++++++++++++  contextInitialized 开始  +++++++++++++++++++++");
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
// DictionaryService dc = (DictionaryService) webApplicationContext.getBean("dictionaryService");
// dc.getCacheDic(); // 调用数据字典Manager的一个方法来缓存
ServletContext servletContext= webApplicationContext.getServletContext();
Map<String,Set<Integer>> urlMap= new HashMap<String,Set<Integer>>();
Set<Integer> set12 = new HashSet<Integer>();
set12.add(1);
set12.add(2);
Set<Integer> set1 = new HashSet<Integer>();
set1.add(1);
String contextPath = servletContext.getContextPath();
urlMap.put(contextPath+"/blacklist/toSelectBlacklist", set12);
urlMap.put(contextPath+"/blacklist/selectBlacklist", set12);
urlMap.put(contextPath+"/blacklist/delete", set1);
urlMap.put(contextPath+"/blacklist/insert", set1);
servletContext.setAttribute("urlsMap", urlMap);
System.out.println("++++++++++++++++++  数据字典已缓存  +++++++++++++++++++++");
System.out.println("++++++++++++++++++  contextInitialized 结束  +++++++++++++++++++++");
} }

  

springmvc 权限 测试版的更多相关文章

  1. 一种基于annotation的Spring-mvc权限控制方法

    简介 本文介绍一种采用annotation来对spring-mvc进行权限控制的方法. 通过枚举类来定义权限项. 将annotation标注到需要控制权限的spring-mvc方法上. 然后,在spr ...

  2. Spring MVC学习总结(4)——SpringMVC权限管理

    1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet.     DispatcherServl ...

  3. springmvc权限过滤器

    package com.zbb.cn.filter; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; ...

  4. springmvc权限拦截器

    配置文件spring-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  5. SpringMVC+Shiro权限管理【转】

    1.权限的简单描述 2.实例表结构及内容及POJO 3.Shiro-pom.xml 4.Shiro-web.xml 5.Shiro-MyShiro-权限认证,登录认证层 6.Shiro-applica ...

  6. springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】

    项目结构:   1.maven项目的pom中引入shiro所需的jar包依赖关系 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  7. SpringMVC拦截器(资源和权限管理)

    1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet.    DispatcherServle ...

  8. SpringMVC+Shiro权限管理

    什么是权限呢?举个简单的例子: 我有一个论坛,注册的用户分为normal用户,manager用户.对论坛的帖子的操作有这些:添加,删除,更新,查看,回复我们规定:normal用户只能:添加,查看,回复 ...

  9. SpringMVC下的Shiro权限框架的使用

    SpringMVC+Shiro权限管理 博文目录 权限的简单描述 实例表结构及内容及POJO Shiro-pom.xml Shiro-web.xml Shiro-MyShiro-权限认证,登录认证层 ...

随机推荐

  1. C#基础知识入门概要(自我回顾用)

    一,C#是什么? 人与人之间可以用语言进行交流,我们和计算机交流也是通过语言.我们可以通过语言让一个人做一件我们想让他做事情(他愿意的话~),我们能不能让计算机按照我们的意愿来做事情呢?比如我们让计算 ...

  2. C#大数据循环

    随笔 - 记录 在数据量超大的情况下使用多线程循环List,而且List不异常,一般是上锁. 不过今天发现其实内置了List的循环操作 一般情况下 用List.Foreach 数据量超大的情况下 推荐 ...

  3. [UWP]xaml中自定义附加属性使用方法的注意项

    ---恢复内容开始--- 随笔小记,欢迎指正 在UWP平台上做WVVM的时候,想针对ListBox的SelectionChanged事件定义一个自定义的命令,于是使用自定义附加属性的方式.可是最后自定 ...

  4. java使用memcached2--集群部署

    一.集群安装 1.编译安装magent-0.5到/usr/local/下 # cd /usr/local # mkdir magent # cd magent/ # wget http://memag ...

  5. mxonline实战8,机构列表分页功能,以及按条件筛选功能

    对应github地址:列表分页和按条件筛选     一. 列表分页   1. pip install django-pure-pagination   2. settings.py中 install ...

  6. socketserver模块解析

    socketserver模块是基于socket而来的模块,它是在socket的基础上进行了一层封装,并且实现并发等功能. 看看具体用法:       ​x         import sockets ...

  7. win7 下如何安装 Microsoft Web Application Stress Tool

    Microsoft Web Application Stress Tool是一个简单易用的性能压力测试工具,但因为其诞生的年代较早,在目前的win7系统下运行,会存在一些兼容性的问题,以下是我在实际使 ...

  8. eclipse远程debug服务器上的项目(Tomcat),打开、关闭及常见错误汇总

    我们工作中,有时候测试/生产环境,出现的结果会与我们预计的不一样,只看代码又看不出去问题所在,这个时候就需要远程debug下服务器上的项目. 注意:(1)需要debug的代码,本机代码需与服务器上一致 ...

  9. 图的最短路径---迪杰斯特拉(Dijkstra)算法浅析

    什么是最短路径 在网图和非网图中,最短路径的含义是不一样的.对于非网图没有边上的权值,所谓的最短路径,其实就是指两顶点之间经过的边数最少的路径. 对于网图,最短路径就是指两顶点之间经过的边上权值之和最 ...

  10. date命令的基本用法

    date设置时间 设置时间:-s参数 date -s "20190426 15:22:33" date查看时间差  (-d参数多用于脚本) 查看时间差:-d参数date -d &q ...