spring拦截器
本文采用拦截器来实现权限拦截。在用户访问相关url时,会检查用户是否已经登录并具有相应访问权限。
一:xml配置文件中拦截器配置
<!-- 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.jeecgframework.core.interceptors.EncodingInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="org.jeecgframework.core.interceptors.AuthInterceptor">
<property name="excludeUrls">
<list>
<value>loginController.do?goPwdInit</value>
<value>loginController.do?pwdInit</value>
<value>loginController.do?login</value>
</list>
</property>
<!-- 模糊匹配 -->
<property name="excludeContainUrls">
<list>
<value>rest/openwx</value>
<value>openDataController</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
二:拦截器实现
org.jeecgframework.core.interceptors.AuthInterceptor 实现代码
public class AuthInterceptor implements HandlerInterceptor {
private static final Logger logger = Logger.getLogger(AuthInterceptor.class);
private SystemService systemService;
private List<String> excludeUrls;
/**
* 包含匹配(请求链接包含该配置链接,就进行过滤处理)
*/
private List<String> excludeContainUrls;
public List<String> getExcludeContainUrls() {
return excludeContainUrls;
}
public void setExcludeContainUrls(List<String> excludeContainUrls) {
this.excludeContainUrls = excludeContainUrls;
}
private static List<TSFunction> functionList;
public List<String> getExcludeUrls() {
return excludeUrls;
}
public void setExcludeUrls(List<String> excludeUrls) {
this.excludeUrls = excludeUrls;
}
public SystemService getSystemService() {
return systemService;
}
@Autowired
public void setSystemService(SystemService systemService) {
this.systemService = systemService;
}
/**
* 在controller后拦截
*/
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception {
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {
}
/**
* 在controller前拦截
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
String requestPath = ResourceUtil.getRequestPath(request);// 用户访问的资源地址
HttpSession session = ContextHolderUtils.getSession();
Client client = ClientManager.getInstance().getClient(session.getId());
if(client == null){
client = ClientManager.getInstance().getClient(
request.getParameter("sessionId"));
}
if (excludeUrls.contains(requestPath)) {
return true;
}else if(moHuContain(excludeContainUrls, requestPath)){
return true;
} else {
if(client == null){
forward(request,response);
return false;
}
if (client != null && client.getUser()!=null ) {
if(!hasMenuAuth(request)){
response.sendRedirect("loginController.do?noAuth");
//request.getRequestDispatcher("webpage/common/noAuth.jsp").forward(request, response);
return false;
}
String functionId=oConvertUtils.getString(request.getParameter("clickFunctionId"));
if(!oConvertUtils.isEmpty(functionId)){
//do somethings
}
if(!oConvertUtils.isEmpty(functionId)){
//do somethings
}
return true;
} else {
return false;
}
}
}
private boolean hasMenuAuth(HttpServletRequest request){
//do somethings
return true or false;
}
/**
* 转发
*
* @param user
* @param req
* @return
*/
@RequestMapping(params = "forword")
public ModelAndView forword(HttpServletRequest request) {
return new ModelAndView(new RedirectView("loginController.do?login"));
}
private void forward(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("webpage/login/timeout.jsp").forward(request, response);
}
/**
* 模糊匹配字符串
* @param list
* @param key
* @return
*/
private boolean moHuContain(List<String> list,String key){
for(String str : list){
if(key.contains(str)){
return true;
}
}
return false;
}
}
微信公众号:破局人

spring拦截器的更多相关文章
- Spring拦截器中通过request获取到该请求对应Controller中的method对象
背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...
- spring拦截器中修改响应消息头
问题描述 前后端分离的项目,前端使用Vue,后端使用Spring MVC. 显然,需要解决浏览器跨域访问数据限制的问题,在此使用CROS协议解决. 由于该项目我在中期加入的,主要负责集成shiro框架 ...
- Spring 拦截器实现+后台原理(HandlerInterceptor)
过滤器跟拦截器的区别 spring mvc的拦截器是只拦截controller而不拦截jsp,html 页面文件的.这就用到过滤器filter了,filter是在servlet前执行的,你也可以理解成 ...
- Spring拦截器和过滤器
什么是拦截器 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对象,允许开发者 ...
- Spring 拦截器——HandlerInterceptor
采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...
- spring 拦截器简介
spring 拦截器简介 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等.2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直 ...
- Spring 拦截器配置
Spring interceptor拦截器配置 Spring mvc的拦截器是通过handlerinterceptor来实现的 实现方式: 1.自定义一个类实现Spring的handlerinterc ...
- spring拦截器的定义
(一).拦截器的定义 1.为什么需要拦截器:在做身份认证或者是进行日志的记录时,我们需要通过拦截器达到我们的目的 2.什么事拦截器:在AOP(Aspect-Oriented Programming)中 ...
- Spring 拦截器实现事物
Spring+Hibernate的实质:就是把Hibernate用到的数据源Datasource,Hibernate的SessionFactory实例,事务管理器HibernateTransactio ...
- Spring拦截器总结
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 Spring过滤器WebFilter可以配置中文过滤 拦截器实现步骤 ...
随机推荐
- 再看GOPATH
原本不打算介绍GOPATH,然而,总是有初学者问一些关于GOPATH的问题,因此在这里再介绍一下GOPATH GOPATH环境变量用于指定这样一些目录:除$GOROOT之外的包含Go项目源代码和二进制 ...
- C#创建、安装一个Windows服务
关于WIndows服务的介绍,之前写过一篇: http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务 ...
- 【开发工具 - Git】之Git版本回退
这篇博客主要记录了关于 查看记录.版本回退.添加标签.删除文件 的操作 1.查看文件修改情况: 可以通过 git diff a.java查看a.java文件自从上次提交后的修改情况,如果自从上次提交之 ...
- Shell脚本编程的常识
(这些往往是经常用到,但是各种网络上的材料都语焉不详的东西,个人认为比较有用) 七种文件类型 d 目录 ...
- HttpClientUtil
package com.uniubi.management.util; import java.io.IOException; import java.io.InterruptedIOExceptio ...
- 【系列】Matei Zaharia(Spark系统作者)博士论文-0 摘要
随着处理器提升速度下降和数据量的不断增长,非常多公司和组织(既有互联网公司也有传统的企业另一些研究机构)都要求他们的应用可以Scale out到更大的分布式系统上(比方整个数据中心). 这些应用又分为 ...
- c#中cookies的存取操作
在客户端创建一个username的cookies,其值为gjy,有效期为1天. 方法1: Response.Cookies["username"].Value="zxf& ...
- 使用ThinkPHP框架高速开发站点(多图)
使用ThinkPHP框架高速搭建站点 这一周一直忙于做实验室的站点,基本功能算是完毕了.比較有收获的是大概了解了ThinkPHP框架.写一些东西留作纪念吧.假设对于相同是Web方面新手的你有一丝丝帮助 ...
- Android AlertDialog 设置setSingleChoiceItems不显示列表的原因【setMessage和setSingleChoiceItems不能同时使用】
今日写了个如题目的简单功能,结果列表不显示 无奈重写了一次代码发现setMessage和setSingleChoiceItems不能同时使用. 正确的如下: private void mobilePh ...
- “:Choose a destination with a supported architecture in order to run on this device.”
我在编译从GitHub上clone下来的<TweeJump>时,出现如下错误:":Choose a destination with a supported architectu ...