拦截器调用feign接口

Utils

package com.qyc.cfg;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtils implements ApplicationContextAware {
        /**
         * 上下文对象实例
         */
        private static ApplicationContext applicationContext;

        @Override
        @Autowired
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringContextUtils.applicationContext = applicationContext;
        }

        /**
         * 获取applicationContext
         * @return
         */
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }

        public static Object getBean(String name){
            return getApplicationContext().getBean(name);
        }

        public static Object getBeanByClass(Class clazz){
            return getApplicationContext().getBean(clazz);
        }




}

拦截器

package com.qyc.interceptor;

import com.qyc.cfg.SpringContextUtils;
import com.qyc.service.Varifcation;
import org.apache.ibatis.plugin.Intercepts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;


public class AuthInterceptors implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //参数
        Varifcation varifcation = (Varifcation) SpringContextUtils.getBeanByClass(Varifcation.class);
        String token = request.getHeader("x-token");
        boolean mess = varifcation.verifyToken(token);
        return mess;
    }
}

Feign接口

package com.qyc.service;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Service
@FeignClient(value = "login-safety")
public interface Varifcation {

    @GetMapping("/sec/log/verifyToken/{token}")
    public boolean verifyToken(@PathVariable("token") String token);
}

猜你喜欢

转载自blog.csdn.net/qq_41835813/article/details/113177475
今日推荐