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 ...
随机推荐
- Spring IOC 实现方式
Spring 中的 org.springframework.beans 包和 org.springframework.context 包构成了 Spring 框架 IoC 容器的基础. BeanFac ...
- 进阶:python3实现 插入排序
一图胜千言,插入排序的核心逻辑如下: 将数据分成两半,前一半是已排好序的,后一半是待排序的 每次取后一半中的第一个数,在已排序的一半中,逆序依次比较,找到要插入的位置 记录插入的位置,在最后判断是否需 ...
- Oracle的优化
Oracle优化:物理优化和逻辑优化.物理优化:1):Oracle的运行环境.2):合理的使用优化器.3):合理配置Oracle实例参数4):建立合适的索引(减少IO)5):将索引数据和表数据分开在不 ...
- Java——容器(Comparable)
[Comparable]
- 20180912-Java实例02
Java 实例 – 删除字符串中的一个字符 以下实例中我们通过字符串函数 substring() 函数来删除字符串中的一个字符,我们将功能封装在 removeCharAt 函数中. // Main.j ...
- UOJ428. 【集训队作业2018】普通的计数题
http://uoj.ac/problem/428 题解 神仙题. 考虑最后一定是放了一个\(1\),然后把其他位置都删掉了. 再考虑到对于序列中的每个位置都对应了一次操作. 我们可以对于每个放\(1 ...
- SpringBoot属性配置-第三章
1.application.yml配置#自定义参数对象book: name: A id: 1 page: 100 2.创建实体类: /** * @Auther: youqc * @Date: 2018 ...
- 《SQL Server 2012 T-SQL基础》读书笔记 - 1.背景
几个缩写的全称:Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language ...
- (转)IDataGridViewEditingControl 接口 作用
本文转载自:http://blog.csdn.net/zx13525079024/article/details/4814575 IDataGridViewEditingControl 接口 定义承载 ...
- VMware 虚拟化编程(10) — VMware 数据块修改跟踪技术 CBT
目录 目录 前文列表 数据块修改跟踪技术 CBT 为虚拟机开启 CBT CBT 修改数据块偏移量获取函数 QueryChangedDiskAreas changeId 一个 QueryChangedD ...