参考博文

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. .NET中的异常处理机制(一)

    1.异常处理的总体指导思想 学习C#中的异常处理机制,大概要了解以下几点: 首先,我们需要知道的事所有具体异常都是继承自System.Exception基类的. 其次,要熟悉FCL类库内置好的一些异常 ...

  2. 再编写代码中报错:CS8107 C# 7.0 中不支持功能“xxxxxx”。请使用 7.1 或更高的语言版本。

    解决方法:项目右键属性 ---> 生成 ---> 找到最下面的高级按钮,点击高级按钮 ---> 常规 ---> 语言版本 ---> 选择 C#最新次要版本,或者比当前版本 ...

  3. CodeChef March Challenge 2019题解

    传送门 \(CHNUM\) 显然正数一组,负数一组 for(int T=read();T;--T){ n=read(),c=d=0; fp(i,1,n)x=read(),x>0?++c:++d; ...

  4. 修改linux的文件时,如何快速找到要修改的内容

    ♦ 在linux系统下,找到需要修改的文件.使用cd+目录的命令进行文件所在的目录,使用ls命令查看是否有该文件. ♦ 使用vim+文件名,打开该文件 ♦ 快速在文件中找到需要修改的地方.如我们需要修 ...

  5. day 09 课后作业

    # -*- coding: utf-8 -*-# @Time : 2018/12/28 14:25# @Author : Endless-cloud# @Site : # @File : 08 课后作 ...

  6. 编程开发之--java多线程学习总结(6)

    5.测试 package com.lfy.ThreadsSynchronize; public class Test { public static void main(String[] args) ...

  7. windows任务管理器所查的网站

    添加menu   https://blog.csdn.net/u012273127/article/details/71293088 点击菜单打开对话框  https://blog.csdn.net/ ...

  8. string容器的简单实现

    1.业务使用main.cpp #include "pch.h" #include <iostream> int main() { mystring s1;//无参构造函 ...

  9. java 实用Util汇总

    1.Util介绍 Util是工具的意思,一般来说,常常用来描述和业务逻辑没有关系的数据处理. Util一般要和私有方法对比:私有方法一般来说是只是在特地场景下使用的,私有方法越多,代码结构越乱.常见的 ...

  10. 基础概念——理解IP地址和域名

    从程序员角度,可以把因特网看做是世界范围内的主机集合: 1)主机集合被映射为一组32位的IP地址. 2)这个IP地址被映射为一组称为因特网域名的标识符. 3)因特网主机上的进程能够通过连接和任何其他因 ...