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 ...
随机推荐
- [CF434D Div1] Tree
问题描述 给定一颗 n 个点的树,树边带权,试求一个排列 P,使下式的值最大 \[ \sum_{i=1}^{n-1}maxflow(P_i,P_{i+1}) \] 其中 maxflow(s, t) 表 ...
- Vuex-全局状态管理【传递参数】
src根目录 新建store文件夹,新建index.js 作为入口 在store文件夹中 新建modules文件夹 modules文件夹中,新建 a.js b.js 2个文件 a.js const s ...
- Bugku 杂项 眼见非实(ISCCCTF)
眼见非实(ISCCCTF) 下载文件后,用winhex打开 发现文件头为50 4B 03 04说明是一个压缩文件,还可以看到其中有.docx文件 更改文件后缀为 .zip 解压后发现 这个文件用wor ...
- java 8 接口默认方法
解决问题:在java8 之前的版本,在修改已有的接口的时候,需要修改实现该接口的实现类. 作用:解决接口的修改与现有的实现不兼容的问题.在不影响原有实现类的结构下修改新的功能方法 案例: 首先定义一个 ...
- 【bzoj4136】[FJOI2015]带子串包含约束LCS问题
题目描述: 带有子串包含约束的最长公共子序列问题可以具体表述如下. 给定2个长度分别为n和m的序列X和Y,以及一个子串包含约束集S. S中共有k个字符串S={S1,S2,…,Sk},其中字符串Si的长 ...
- echarts画柱状图
drawLeftHistogram(){ let Histogram = echarts.init(document.getElementById('data-left-bottom-table-wr ...
- 解决 UIAlterController 不居中问题
最后更新:2017-06-30 现象描述 新公司做的解决的第一个bug 就是 UIAlterController 不居中,莫名其妙的飞出屏幕之外 找了很久的答案,最终在苹果论坛看到了相关的描述 We ...
- 关闭掉mysql 8和mysql5.7的密码验证插件validate_password
在mysql文档中的一段话If you installed MySQL 5.7 using the MySQL Yum repository, MySQL SLES Repository, or RP ...
- base64加密小案例
python终端下: import base64 >>> dict='{"name":"tom"}' >>> dict.en ...
- 003-CHROME开发者工具的小技巧
首先调试先进入到调试模式,快键键F12 1.代码格式化 有很多css/js的代码都会被 minify 掉,你可以点击代码窗口左下角的那个 { } 标签,chrome会帮你给格式化掉. 2.强制DOM ...