SpringBoot中设置自定义拦截器
SpringBoot中设置自动以拦截器需要写一个类继承HandlerInterceptorAdapter并重写preHandle方法
例子
public class AuthorityIntercept extends HandlerInterceptorAdapter {
// 放行的URL列表
private List<String> allowList = Arrays.asList("/front/**","/label/**");
private static final PathMatcher PATH_MATCHER = new AntPathMatcher();
@Value("#{configProperties['upload_path']}")
private String upload_path;
private boolean isSetApplication = false;
@Autowired
private RedisService redisService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if(!isSetApplication) {
isSetApplication = true;
ServletContext application = request.getSession().getServletContext();
application.setAttribute(Constants.FILE_PATH, upload_path);
}
if (!checkAllowAccess(request.getRequestURI())) {
String token = request.getHeader("token");
String userInfo = null;
if(token != null){
userInfo = this.redisService.get(token);
}
if (userInfo == null) {
/*//判断是否是ajax请求
if(isAjaxRequest(request)) {
response.setStatus(ResultCode.USER_SESSION_INVALID.getCode());
Result result = new Result(ResultCode.USER_SESSION_INVALID);
result.setData(request.getContextPath() + "/front/smallLogin");
response.getWriter().print(ResponseHelper.createResponse(result));
} else {
//session为空,跳到登录页
response.sendRedirect(request.getContextPath() + "/front/login");
}*/
response.getWriter().write("{\"code\":4023,msg:\"please login\"}");
return false;
}
}
return super.preHandle(request, response, handler);
}
/**
* 检查URI是否放行
*
* @param URI
* @return 返回检查结果
*/
private boolean checkAllowAccess(String URI) {
if (!URI.startsWith("/")) {
URI = "/" + URI;
}
for (String allow : allowList) {
if (PATH_MATCHER.match(allow, URI)) {
return true;
}
}
return false;
}
/**
* 判断是否是ajax请求
*
* @param request
* @return
*/
private boolean isAjaxRequest(HttpServletRequest request) {
// 判断是否为ajax请求,默认不是
boolean isAjaxRequest = false;
if (StringUtils.isNotBlank(request.getHeader("x-requested-with"))
&& request.getHeader("x-requested-with").equals("XMLHttpRequest")) {
isAjaxRequest = true;
}
return isAjaxRequest;
}
public List<String> getAllowList() {
return allowList;
}
public void setAllowList(List<String> allowList) {
this.allowList = allowList;
}
}
并需要些一个类来继承WebMvcConfigurerAdapter,并重写addInterceptors方法来定义自定义的拦截器
/**
* 静态资源处理
* @author maming
* @date 2018年5月14日
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{ @Value("${web.upload-path}")
private String path; @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/ueditor/**").addResourceLocations("file:" + path + "ueditor/"); //虚拟路径设置
super.addResourceHandlers(registry);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthorityIntercept()).addPathPatterns("/**");
} }
SpringBoot中设置自定义拦截器的更多相关文章
- springboot 2.0+ 自定义拦截器
之前项目的springboot自定义拦截器使用的是继承WebMvcConfigurerAdapter重写常用方法的方式来实现的. 以下WebMvcConfigurerAdapter 比较常用的重写接口 ...
- nodejs+express中设置登录拦截器
在nodejs+express中,采用nodejs后端路由控制用户登录后,为了加强前端的安全性控制,阻止用户通过在浏览器地址栏中输入地址访问后台接口,在app.js中需要加入拦截器进行拦截: /*** ...
- JavaEE开发之SpringMVC中的自定义拦截器及异常处理
上篇博客我们聊了<JavaEE开发之SpringMVC中的路由配置及参数传递详解>,本篇博客我们就聊一下自定义拦截器的实现.以及使用ModelAndView对象将Controller的值加 ...
- 在struts2中配置自定义拦截器放行多个方法
源码: 自定义的拦截器类: //自定义拦截器类:LoginInterceptor ; package com.java.action.interceptor; import javax.servlet ...
- 关于springboot中过滤器和拦截器
在解决跨域问题中,发现拦截器和过滤器用得不是熟练.就参考了下一下两个作者的文档.希望大家也可以汲取精华 文档1 https://blog.csdn.net/moonpure/article/det ...
- SpringBoot 2.x 自定义拦截器并解决静态资源访问被拦截问题
自定义拦截器 /** * UserSecurityInterceptor * Created with IntelliJ IDEA. * Author: yangyongkang * Date: ...
- Struts2中一个自定义拦截器的使用
1.自定义的拦截器的类: package it.web.interceptor; import com.opensymphony.xwork2.ActionContext; import com.op ...
- springboot中配置了拦截器后,拦截器无效的解决方案之一
springboot的启动类xxxApplication不能扫描到拦截器配置类,可加上@ComponentScan(basePackages={"com.maya.common"} ...
- springboot中过滤器、拦截器、切片使用
直接贴代码:采用maven工程 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project ...
随机推荐
- win10如何设置软件开机启动
想要实现应用程序在所有的用户登录系统后都能自动启动,就把该应用程序的快捷方式放到“系统启动文件夹”里C:\ProgramData\Microsoft\Windows\Start Menu\Progra ...
- (12)QT中搭建opencv开发环境
只需要在项目工程文件pro中添加上: INCLUDEPATH += G:/opencv/build/include \ G:/opencv/build/include/opencv \ G:/open ...
- [BZOJ1964]hull 三维凸包:计算几何
分析 发现自己并不会计算几何. 所以先引用一下这位dalao的博客. 二维平面四个点求凸包面积->任选三个点面积之和/2 三维平面五个点求凸包体积->任选四个点体积之和/2 二维平面三个点 ...
- All-one Matrices
All-one Matrices 单调栈 最大全一矩阵计数 #include<bits/stdc++.h> #define maxn 3005 using namespace std; # ...
- [CSP-S模拟测试]:matrix(DP)
题目描述 求出满足以下条件的$n\times m$的$01$矩阵个数:(1)第$i$行第$1~l_i$列恰好有$1$个$1$.(2)第$i$行第$r_i~m$列恰好有$1$个$1$.(3)每列至多有$ ...
- 原生JS获取元素宽高实践详解
开篇的话 任何不是亲身实践中求得的知识,都不是属于你的.任何求得的知识不去时常温习运用,也不是属于你的. 记录由来 在做个上拉广告功能中遇到了一个"理所当然"觉得对的用法,慢慢才排 ...
- hibernate class cast exception from object to ...
http://stackoverflow.com/questions/22548325/java-lang-classcastexception-cannot-be-cast-to-java-lang ...
- kubernetes-helm程序包管理器(二十)
helm概述 Helm是Kubernetes的包管理器,Helm 让我们能够像 yum 管理 rpm 包那样安装.部署.升级和删除容器化应用. Helm的核心术语: Chart:一个helm程序包,是 ...
- Python编程:从入门到实践—类
创建类 #!/usr/bin/env python# --*-- encoding:utf-8 --*-- class Dog(): """一次模拟小狗的简单尝试&quo ...
- code_action
w https://raw.githubusercontent.com/laravel/laravel/master/config/database.php <?php return [ /* ...