一,编写拦截器

public class TokenInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//若不是映射方法,直接通过
if (!(handler instanceof HandlerMethod)){
return true;
} HandlerMethod handlerMethod = (HandlerMethod)handler;
Method method = handlerMethod.getMethod();
if (method.isAnnotationPresent(PassToken.class)){
return true;
} if (method.isAnnotationPresent(RequireToken.class)){
String token = null;
//String token = request.getHeader("token");
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if ("token".equals(cookie.getName())){
token = cookie.getValue();
break;
}
}
if (token == null){
throw new RuntimeException("无token信息,请重新登录");
} String username = JwtUtils.getUsernameFromToken(token);
if (username == null){
throw new RuntimeException("token信息错误");
} request.setAttribute("username", username + ",test");
return true;
} return false;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}

二,配置拦截器

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(tokenInterceptor())
.addPathPatterns("/**");
} @Bean
public TokenInterceptor tokenInterceptor(){
return new TokenInterceptor();
} }

springboot添加拦截器的更多相关文章

  1. SpringBoot如何添加拦截器

    在web开发的过程中,为了实现登录权限验证,我们往往需要添加一个拦截器在用户的的请求到达controller层的时候实现登录验证,那么SpringBoot如何添加拦截器呢? 步骤如下: 1.继承Web ...

  2. SpringBoot使用拦截器

    SpringBoot的拦截器只能拦截流经DispatcherServlet的请求,对于自定义的Servlet无法进行拦截. SpringMVC中的拦截器有两种:HandlerInterceptor和W ...

  3. springboot集成拦截器

    一.首先对HandlerInterceptor进行封装,封装为MappingInterceptor.封装的方法里添加拦截器起作用的路径addPathPatterns(),及需要排除路径的方法exclu ...

  4. SpringBoot(五):SpringBoot使用拦截器

    1.按照SpringMVC的方式编写一个拦截器: 2.配置一个类   implements WebMvcConfigurer 接口 为该类添加注解@Configuration  (等价于一个sprin ...

  5. spring boot 添加拦截器

    构建一个spring boot项目. 添加拦截器需要添加一个configuration @Configuration @ComponentScan(basePackageClasses = Appli ...

  6. javaweb添加拦截器

    js请求后台代码添加拦截器: package com.ctzj.biz.isale.deploy.controller; import java.io.IOException; import java ...

  7. SpringBoot自定义拦截器实现IP白名单功能

    SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...

  8. (七)CXF添加拦截器

    今天开始讲下拦截器,前面大家学过servlet,struts2 都有拦截器概念,主要作用是做一些权限过滤,编码处理等: webservice也可以加上拦截器,我们可以给webservice请求加权限判 ...

  9. 使用CXF为webservice添加拦截器

      拦截器分为Service端和Client端 拦截器是在发送soap消息包的某一个时机拦截soap消息包,对soap消息包的数据进行分析或处理.分为CXF自带的拦截器和自定义的拦截器 1.Servi ...

随机推荐

  1. nginx如何写日志

    写日志函数为ngx_log_error_core,位于src/core/ngx_log.c:89行核心代码如下:while (log) { if (log->log_level < lev ...

  2. hystrix源码之请求合并

    请求合并 使用HystrixObservableCollapser可以将参数不同,但执行过程相同的调用合并执行.当调用observe.toObservable方法时,会向RequestCollapse ...

  3. Golang的Context介绍及其源码分析

    简介 在Go服务中,对于每个请求,都会起一个协程去处理.在处理协程中,也会起很多协程去访问资源,比如数据库,比如RPC,这些协程还需要访问请求维度的一些信息比如说请求方的身份,授权信息等等.当一个请求 ...

  4. Unity3D 一、游戏

    3D游戏编程第一次作业 作业要求 阅读 Tracy Fullerton, *GAME DESIGN WORKSHOP* 第2-4章(游戏结构.基本元素.戏剧元素).选择一款你喜欢的中等规模游戏如&qu ...

  5. 消息队列MQ面试专题(rabbitmq)

    正文: 1.什么是 rabbitmq 采用 AMQP 高级消息队列协议的一种消息队列技术,最大的特点就是消费并不需要确保提供方存在,实现了服务之间的高度解耦 2.为什么要使用 rabbitmq 在分布 ...

  6. TypeScript实现设计模式——生成器模式

    生成器模式是一种在TypeScript/JavaScript中非常常见的创建型设计模式,它使你能够分步骤创建复杂对象.当你需要创建一个可能有许多配置选项的对象时, 该模式会特别有用. 问题 假设我们需 ...

  7. c#RSA的SHA1加密与AES加密、解密

    前言:公司项目对接了一个对数据保密性要求较高的java公司.api接口逻辑是这样的:他们提供 SHA1私钥 与 AES的秘钥.我们需要将 传递查询参数 通过SHA1 私钥加密再转换成 十六进制 字符串 ...

  8. PCA基本原理

    降维问题的优化目标:将一组N维向量降维k维(K大于0,小于N),其目标是选择K个单位(模为1)正交基,使得原始数据变换到这组基上后, 选择然数据点之间方差最大的方向作为坐标轴 各字段两两间协方差为0, ...

  9. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  10. 第三周:java面向对象部分总结(2)

    <!--此处接上周--> 3.相关接口 对对象的排序,可以通过以下两种方法: 1.实现Comparable接口,重写compareTo方法: 2.Comparator<>比较器 ...