学无先后,达者为师

网站首页 编程语言 正文

spring boot 实现token拦截

作者:郭俊强 更新时间: 2023-07-16 编程语言
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-----");

    }
}

原文链接:https://blog.csdn.net/qq_37061571/article/details/122056024

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新