springmvc 权限 测试版
参考博文
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 权限 测试版的更多相关文章
- 一种基于annotation的Spring-mvc权限控制方法
简介 本文介绍一种采用annotation来对spring-mvc进行权限控制的方法. 通过枚举类来定义权限项. 将annotation标注到需要控制权限的spring-mvc方法上. 然后,在spr ...
- Spring MVC学习总结(4)——SpringMVC权限管理
1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet. DispatcherServl ...
- springmvc权限过滤器
package com.zbb.cn.filter; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; ...
- springmvc权限拦截器
配置文件spring-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- SpringMVC+Shiro权限管理【转】
1.权限的简单描述 2.实例表结构及内容及POJO 3.Shiro-pom.xml 4.Shiro-web.xml 5.Shiro-MyShiro-权限认证,登录认证层 6.Shiro-applica ...
- 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 ...
- SpringMVC拦截器(资源和权限管理)
1.DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet. DispatcherServle ...
- SpringMVC+Shiro权限管理
什么是权限呢?举个简单的例子: 我有一个论坛,注册的用户分为normal用户,manager用户.对论坛的帖子的操作有这些:添加,删除,更新,查看,回复我们规定:normal用户只能:添加,查看,回复 ...
- SpringMVC下的Shiro权限框架的使用
SpringMVC+Shiro权限管理 博文目录 权限的简单描述 实例表结构及内容及POJO Shiro-pom.xml Shiro-web.xml Shiro-MyShiro-权限认证,登录认证层 ...
随机推荐
- Android 载入 HTML
Android 中载入 HTML 有两种方式: 1. 用 TextView.setText(Html.fromHtml("<html></html>")); ...
- django系列6--Ajax04 请求设置(设置csrf_token)
Ajax请求设置csrf_token 官方文档 csrf_token: https://docs.djangoproject.com/en/1.11/ref/csrf/ CSRF 跨站请求攻击,简单地 ...
- python--类的约束,异常处理,MD5加密,日志处理logging模块
1.类的约束 在开发中,如果项目经理需要对类进行约束,可以有两种方式 1. 对子类进行约束 Base: #对子类进行约束,必须重写这个方法 # 在工作中发现了NotImplementedError之后 ...
- Python(简单计算器)
参考:https://www.cnblogs.com/alex3714/articles/5169958.html import re ret = re.search('\([^()]+\)','(1 ...
- Requests Header | Http Header
Requests Header | Http Header Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accep ...
- 【awk】按小时切割日志
需求: 把日志按日志内容中的小时数做切割 {hostname=ali-beijing-msync-3512} 2017-05-17 23:17:52.694 [info] <0.27292.70 ...
- idea 新建New Module时Maven的列表加载不出来
Under Build → Build Tools → Maven → Importing, set VM options for importer to -Xmx1024m (the default ...
- mxonline实战9,我要学习功能块,机构详情展示,收藏功能
对应github地址:第9天 一. 实现我要学习功能
- Flink--Streaming Connectors
原网址:https://ci.apache.org/projects/flink/flink-docs-release-1.7/dev/connectors/ Predefined Sources a ...
- java程序向hdfs中追加数据,异常以及解决方案
今天在学习hdfs时,遇到问题,就是在向hdfs中追加数据总是报错,在经过好几个小时的努力之下终于将他搞定 解决方案如下:在hadoop的hdfs-sit.xml中添加一下三项 <propert ...