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 ...
随机推荐
- AC自动机及其模板
模板 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ; ; ; ...
- 「树形结构 / 树形DP」总结
Codeforces 686 D. Kay and Snowflake 要求$O(n)$求出以每个节点为根的重心. 考虑对于一个根节点$u$,其重心一定在[各个子树的重心到$u$]这条链上.这样就能够 ...
- sh_01_判断年龄
sh_01_判断年龄 # 1. 定义一个整数变量记录年龄 age = 15 # 2. 判断是否满了18岁 if age >= 18: # 3. 如果满了18岁,可以进网吧嗨皮 print(&qu ...
- WPF Microsoft.Practices.Unity 注入大法简单示例
最近新入职了公司,做WPF方向的项目,进来后看到这边大量运用了依赖注入来解耦,采用的是Microsoft.Practices.Unity. WPF的话,目前主要有两个技术来实现IOC,unity和ME ...
- [CSP-S模拟测试]:梦境(贪心+小根堆)
题目描述 智者奥尔曼曾说过:有缘的人即使相隔海角天涯,也会在梦境中相遇. $IcePrince\text{_}1968$和$IcePrincess\text{_}1968$便是如此.有一天$IcePr ...
- [CSP-S模拟测试]:ants(回滚莫队)
题目描述 然而贪玩的$dirty$又开始了他的第三个游戏. $dirty$抓来了$n$只蚂蚁,并且赋予每只蚂蚁不同的编号,编号从$1$到$n$.最开始,它们按某个顺序排成一列.现在$dirty$想要进 ...
- Java数据结构与算法(1):线性表
线性表是一种简单的数据类型,它是具有相同类型的n个数据元素组成的有限序列.形如如A0,A1,...,An-1.大小为0的表为空表,称Ai后继Ai-1,并称Ai-1前驱Ai. printList打印出表 ...
- 从数据库、页面加载速度角度思考 id设计 sku asin
(已对数据进行字符串替换,去身份识别.隐私跟踪) 12-13-14-15-16-18岁20女孩夏装初中高中学生韩版上衣服短袖T恤衫-tmall.com天猫 https://detail.tmall.c ...
- Redis hash 是一个 string 类型的 field 和 value 的映射表.它的添加、删除操作都是 O(1)(平均)。
2.3 hashes 类型及操作 Redis hash 是一个 string 类型的 field 和 value 的映射表.它的添加.删除操作都是 O(1)(平均).hash 特别适合用于存储对象.相 ...
- (转)flexpaper 参数
本文转载自:http://blog.csdn.net/z69183787/article/details/18659913 Flexpaper可能用到如下参数 SwfFile (String) 需 ...