参考博文

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. wpf(怎么跨线程访问wpf控件)

    在编写代码时,我们经常会碰到一些子线程中处理完的信息,需要通知另一个线程(我这边处理完了,该你了). 但是当我们通知WPF的UI线程时需要用到Dispatcher. 首先我们需要想好在UI控件上需要显 ...

  2. .Net Core下使用Ajax,并传送参数到controllers

    可以使用URL拼接方式方法传参 .cshtml部分 @section Scripts{ @{ await Html.RenderPartialAsync("_ValidationScript ...

  3. win10下安装配置iis,发布iis

    老有朋友不会配置iis跟发布iis,今天整理一下,欢迎参考借鉴 打开控制面板 找到 程序 点击程序  找到启用或关闭windows功能 在windows服务中找到 Internet Informati ...

  4. java—不同的用户登录以后可以看到不同的菜单(后台可以实现对用户菜单的管理) 1 (55)

    实现不同的用户登录以后可以看到不同的菜单.(后台可以实现对用户菜单的管理.) 第一步:分析数据结构        1:用户表 表名:users 列名 类型 说明 id Varchar(32) 主键 n ...

  5. 关于Mybatis 反向生成后 查询结果全部为null 解决办法

    今天遇到了一个问题,就是mybatis通过反向生成工具 生成的pojo类(实体类) xml文件 以及Mapper之后查询时结果为null 我写的代码怎么看都没有错 就是没有结果 后来在排除错误的时候发 ...

  6. IntelliJ IDEA 配置JSP & Servlet开发环境

    首先我们要安装和配置好JAVA和TOMCAT,这里不再详细描述 下载地址: JAVA:https://www.oracle.com/technetwork/java/javase/downloads/ ...

  7. 64位虚拟机中安装CentOS_6.7

    虚拟机VirtualBox-4.3.24-98716-Win.1425444683.exe,操作系统选用CentOS-6.7-x86_64-LiveDVD .iso. 1) 启动VirtualBox, ...

  8. 深入理解Scala的隐式转换

    摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码.   使用方式: 1. ...

  9. Linux之解决命令行cat命令中文乱码

    临时解决cat中文乱码 cat test.txt | iconv -f GBK -t UTF-8

  10. Hadoop虚拟机的jdk版本和本地eclipse的版本不一致怎么办

    在本周学习Hadoop遇到了一个问题,困扰了半天,本人在安装Hadoop时是按照视频来的,结果发现Hadoop上的jdk版本和本地eclipse的版本不一致,导致本地的程序到处jar包传到虚拟机上运用 ...