使用springmvc的拦截器处理token,解决抛出异常处理

在使用拦截器处理token问题时需要用到其他的类,但是总是注入不进去

package com.example.demo.config;

import com.example.demo.util.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class InterceptorConfig implements WebMvcConfigurer {



//解决拦截器里面无法获取容器中bean对象的问题
    @Bean
    public MyInterceptor myInterceptor(){
        return new MyInterceptor();
    }

        /**
         * 在这里添加自己定义的拦截器
         * @param registry
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(myInterceptor())//这里不再new一个拦截器对象
                    // 只拦截test路径
                    .addPathPatterns("/user/**")
                    // 不拦截pass路径
//                    .excludePathPatterns("/pass/**");
                    .excludePathPatterns("/user/saveUser")
                    .excludePathPatterns("/user/login");
        }

//    解决拦截器拦截swagger请求的问题
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

使用这个注解@ControllerAdvice总是处理不了再拦截器抛出的异常,由于写了一个自定义异常,因此一般都是抛出自定义异常,执行自定义拦截器的代码

package com.example.demo.util;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: WXL
 * @date: 2021/6/17 11:25
 * Description:处理拦截器抛出的异常
 */


//@ControllerAdvice
@RestControllerAdvice
public class MyGlobalExceptionHandler {
    @ExceptionHandler({Exception.class})
    public Message handException(HttpServletRequest request , Exception e) throws Exception{


        System.out.println("这是Exception异常拦截器");
        return MessageUtil.error(StatusCodeUtil.ERROR_CODE, "系统错误2," + e.getMessage());

    }
    @ExceptionHandler(MyException.class)
    public Message customException(MyException e) {

        System.out.println("这是自定义异常拦截器");
        return MessageUtil.error(StatusCodeUtil.ERROR_CODE, "系统错误1," + e.getMessage());
    }


}


下面是经过异常处理后的返回值

{
  "code": 500,
  "description": "系统错误1,token不合法",
  "data": null
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值