日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新