spring boot 实现token拦截

package com.example.config.request.handler;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;

//Authorization.java,授权注解,作用于controller类及其方法上,方法优先
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorization {
}
package com.example.config.request.mvcConfigurer;

import com.example.config.request.handler.LogHandlerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 注册拦截器
     * **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogHandlerInterceptor()).addPathPatterns("/**");
    }
}
package com.example.config.request.handler;

import com.example.config.exception.handler.BusinessException;
import com.example.config.response.enums.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
@Slf4j
/***
 * 实现自定义的拦截器需要实现HandlerInterceptor接口
 * **/
public class LogHandlerInterceptor implements HandlerInterceptor {
    /**
     * controller 执行之前调用
     * 逻辑为  先去查看方法上是否有注解
     *        没有注解的话查看类上是否存在注解
     *        都没有的话可以通过
     *
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if(handler instanceof HandlerMethod){
//            方法上是否有授权注解
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Authorization authorization = handlerMethod.getMethodAnnotation(Authorization.class);
            if (authorization == null) {
                // 方法所属类上是否有授权注解
                authorization = handlerMethod.getMethod().getDeclaringClass().getAnnotation(Authorization.class);
                if (authorization == null) {
                    return true;
                }
            }
//            方法或类上面有需要登录权限的话就需要去校验该token是否存在
//            log.info(request.getHeader("sessionId").toString());
//            if("匹配错误"){
//                throw new BusinessException(ResultCode.USER_NOT_FIND);
//            }else{
//                return true;
//            }
        }
        return true;
    }

    /**
     * controller 执行之后,且页面渲染之前调用
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        System.out.println("------postHandle-----");
    }

    /**
     * 页面渲染之后调用,一般用于资源清理操作
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("------afterCompletion-----");

    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值